add ByteBuffer

This commit is contained in:
pyihe
2022-02-27 21:54:16 +08:00
parent d7f79e6857
commit c7ca9a850d
4 changed files with 31 additions and 12 deletions

View File

@@ -1,13 +1,28 @@
package bytes
import "unsafe"
import (
"unsafe"
"github.com/valyala/bytebufferpool"
)
type ByteBuffer = bytebufferpool.ByteBuffer
var (
Get = bytebufferpool.Get()
Put = func(b *ByteBuffer) {
if b != nil {
bytebufferpool.Put(b)
}
}
)
// String []byte转换为string
func String(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// SliceEqual
// SliceEqual 判断两个字节切片每个元素是否相等
func SliceEqual(a, b []byte) bool {
aLen, bLen := len(a), len(b)
if aLen != bLen {
@@ -27,7 +42,7 @@ func SliceEqual(a, b []byte) bool {
return true
}
// Contain
// Contain 判断字节切片b是否包含ele元素
func Contain(ele byte, b []byte) bool {
for _, v := range b {
if v == ele {
@@ -37,7 +52,7 @@ func Contain(ele byte, b []byte) bool {
return false
}
// Reverse
// Reverse 翻转字节切片
func Reverse(b []byte) {
l := len(b)
for i := l/2 - 1; i >= 0; i-- {
@@ -46,7 +61,7 @@ func Reverse(b []byte) {
}
}
// Remove
// Remove 从b中删除ele
func Remove(ele byte, b []byte) {
for i := range b {
if ele == b[i] {

1
go.mod
View File

@@ -9,6 +9,7 @@ require (
github.com/lestrrat-go/strftime v1.0.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/valyala/bytebufferpool v1.0.1-0.20201104193830-18533face0df // indirect
github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect
go.uber.org/zap v1.18.1 // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect

2
go.sum
View File

@@ -26,6 +26,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/valyala/bytebufferpool v1.0.1-0.20201104193830-18533face0df h1:lL/Oy/WWGQJ7cly/i0z/ALOprHiHRI+KjSoZLVmTwp8=
github.com/valyala/bytebufferpool v1.0.1-0.20201104193830-18533face0df/go.mod h1:7zinLbvFO6mTmV5z9I+E31lfEnEQ2a27Om5SPCg99dA=
github.com/vmihailenco/msgpack/v5 v5.3.4 h1:qMKAwOV+meBw2Y8k9cVwAy7qErtYCwBzZ2ellBfvnqc=
github.com/vmihailenco/msgpack/v5 v5.3.4/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=

View File

@@ -1,16 +1,18 @@
package strings
import (
"reflect"
"strconv"
"strings"
"unsafe"
)
// Bytes string转换为[]byte
func Bytes(s string) []byte {
sp := *(*[2]uintptr)(unsafe.Pointer(&s))
bp := [3]uintptr{sp[0], sp[1], sp[1]}
return *(*[]byte)(unsafe.Pointer(&bp))
func Bytes(s string) (b []byte) {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len
return b
}
// Uint64 string转换为uint64
@@ -108,7 +110,7 @@ func Reverse(s []string) {
}
}
// Remove
// Remove 从s中删除第一个ele
func Remove(ele string, s []string) {
for i := range s {
if s[i] == ele {
@@ -153,9 +155,8 @@ func FilterRepeatBySlice(slc []string) []string {
return result
}
//通过map转换
// FilterRepeatByMap 通过map元素去重
func FilterRepeatByMap(slc []string) []string {
//result := make([]string, 0)
var result []string
tempMap := make(map[string]byte, 0)
for _, e := range slc {