diff --git a/README.md b/README.md index 2453f7d..b227c71 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A Go library for converting strings to byte slices and vice versa without memory To install the `bytestring` library, use the `go get` command: ```shell -go get github.com/AH-dark/bytestring +go get github.com/AH-dark/bs ``` ## Usage @@ -16,18 +16,18 @@ The `bytestring` library provides functions to efficiently convert strings to by ### Convert String to Bytes ```go -import "github.com/AH-dark/bytestring" +import "github.com/AH-dark/bytestring/v2" s := "Hello, World!" -b := bytestring.StringToBytes(s) +b := bs.StringToBytes(s) ``` ### Convert Bytes to String ```go -import "github.com/AH-dark/bytestring" +import "github.com/AH-dark/bytestring/v2" b := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33} -s := bytestring.BytesToString(b) +s := bs.BytesToString(b) ``` ## Running Tests diff --git a/convert_test.go b/convert_test.go index 16a1dd2..5d83b9c 100644 --- a/convert_test.go +++ b/convert_test.go @@ -1,4 +1,4 @@ -package bytestring +package bs import ( cryptoRand "crypto/rand" @@ -31,7 +31,7 @@ func TestBytesToString(t *testing.T) { _, err := cryptoRand.Read(data) asserts.NoError(err) - asserts.Equal(rawBytesToStr(data), BytesToString(data)) + asserts.Equal(rawBytesToStr(data), B2S(data)) } } @@ -68,7 +68,7 @@ func TestStringToBytes(t *testing.T) { for i := 0; i < 100; i++ { s := RandStringBytesMaskImprSrcSB(64) - asserts.Equal(rawStrToBytes(s), StringToBytes(s)) + asserts.Equal(rawStrToBytes(s), S2B(s)) } } @@ -82,7 +82,7 @@ func BenchmarkBytesConvBytesToStrRaw(b *testing.B) { func BenchmarkBytesConvBytesToStr(b *testing.B) { for i := 0; i < b.N; i++ { - BytesToString(testBytes) + B2S(testBytes) } } @@ -94,6 +94,6 @@ func BenchmarkBytesConvStrToBytesRaw(b *testing.B) { func BenchmarkBytesConvStrToBytes(b *testing.B) { for i := 0; i < b.N; i++ { - StringToBytes(testString) + S2B(testString) } } diff --git a/go.mod b/go.mod index 35c2fc9..804ab36 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/AH-dark/bytestring +module github.com/AH-dark/bytestring/v2 go 1.21 diff --git a/go_1.19.go b/go_1.19.go index e2f1b74..b803a61 100644 --- a/go_1.19.go +++ b/go_1.19.go @@ -1,12 +1,12 @@ //go:build !go1.20 && !go1.21 && !go1.22 // +build !go1.20,!go1.21,!go1.22 -package bytestring +package bs import "unsafe" -// StringToBytes converts string to byte slice without a memory allocation. -func StringToBytes(s string) []byte { +// S2B converts string to byte slice without a memory allocation. +func S2B(s string) []byte { return *(*[]byte)(unsafe.Pointer( &struct { string @@ -15,7 +15,7 @@ func StringToBytes(s string) []byte { )) } -// BytesToString converts byte slice to string without a memory allocation. -func BytesToString(b []byte) string { +// B2S converts byte slice to string without a memory allocation. +func B2S(b []byte) string { return *(*string)(unsafe.Pointer(&b)) } diff --git a/go_1.20.go b/go_1.20.go index 292e1ec..9c028f5 100644 --- a/go_1.20.go +++ b/go_1.20.go @@ -1,18 +1,18 @@ //go:build go1.20 || go1.21 || go1.22 // +build go1.20 go1.21 go1.22 -package bytestring +package bs import "unsafe" -// StringToBytes converts string to byte slice without a memory allocation. +// S2B converts string to byte slice without a memory allocation. // For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077. -func StringToBytes(s string) []byte { +func S2B(s string) []byte { return unsafe.Slice(unsafe.StringData(s), len(s)) } -// BytesToString converts byte slice to string without a memory allocation. +// B2S converts byte slice to string without a memory allocation. // For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077. -func BytesToString(b []byte) string { +func B2S(b []byte) string { return unsafe.String(unsafe.SliceData(b), len(b)) }