Skip to content

Commit

Permalink
Merge branch 'master' into utree
Browse files Browse the repository at this point in the history
  • Loading branch information
appleboy authored Jan 3, 2021
2 parents 460d346 + 3b5e861 commit 0eb2d6b
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions internal/bytesconv/bytesconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
package bytesconv

import (
"reflect"
"unsafe"
)

// StringToBytes converts string to byte slice without a memory allocation.
func StringToBytes(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
return *(*[]byte)(unsafe.Pointer(
&struct {
string
Cap int
}{s, len(s)},
))
}

// BytesToString converts byte slice to string without a memory allocation.
Expand Down

0 comments on commit 0eb2d6b

Please sign in to comment.