Skip to content

Commit

Permalink
avoid to using SliceHeader, more portable code (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
gggrafff committed Feb 25, 2024
1 parent 3a4b1f0 commit 3eeedea
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions convert.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//go:build !js
// +build !js

// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

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

// ToBytes converts a string to a byte slice without allocating.
func ToBytes(v string) (b []byte) {
func ToBytes(v string) []byte {
strHeader := (*reflect.StringHeader)(unsafe.Pointer(&v))
byteHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b))
byteHeader.Data = strHeader.Data
bytesData := unsafe.Slice((*byte)(unsafe.Pointer(strHeader.Data)), len(v))

l := len(v)
byteHeader.Len = l
byteHeader.Cap = l
return
return bytesData
}

func binaryToBools(b *[]byte) []bool {
Expand Down

0 comments on commit 3eeedea

Please sign in to comment.