Skip to content

Commit

Permalink
feat: updated to v2 & simplified calling
Browse files Browse the repository at this point in the history
  • Loading branch information
AH-dark committed Jan 25, 2024
1 parent 3b06a25 commit 581b9f0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions convert_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bytestring
package bs

import (
cryptoRand "crypto/rand"
Expand Down Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/AH-dark/bytestring
module github.com/AH-dark/bytestring/v2

go 1.21

Expand Down
10 changes: 5 additions & 5 deletions go_1.19.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))
}
10 changes: 5 additions & 5 deletions go_1.20.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 581b9f0

Please sign in to comment.