Skip to content

Commit 3eeedea

Browse files
authored
avoid to using SliceHeader, more portable code (#19)
1 parent 3a4b1f0 commit 3eeedea

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

convert.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
//go:build !js
12
// +build !js
3+
24
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
35
// Licensed under the MIT license. See LICENSE file in the project root for details.
46

@@ -15,15 +17,11 @@ func ToString(b *[]byte) string {
1517
}
1618

1719
// ToBytes converts a string to a byte slice without allocating.
18-
func ToBytes(v string) (b []byte) {
20+
func ToBytes(v string) []byte {
1921
strHeader := (*reflect.StringHeader)(unsafe.Pointer(&v))
20-
byteHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b))
21-
byteHeader.Data = strHeader.Data
22+
bytesData := unsafe.Slice((*byte)(unsafe.Pointer(strHeader.Data)), len(v))
2223

23-
l := len(v)
24-
byteHeader.Len = l
25-
byteHeader.Cap = l
26-
return
24+
return bytesData
2725
}
2826

2927
func binaryToBools(b *[]byte) []bool {

0 commit comments

Comments
 (0)