From 9a6bf93c59fed2c3bade8ba8be92e36684d237c4 Mon Sep 17 00:00:00 2001 From: "a.makhaev" Date: Fri, 8 Dec 2023 12:19:22 +0300 Subject: [PATCH] avoid to using SliceHeader, more portable code --- convert.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/convert.go b/convert.go index b19146b..5a23475 100644 --- a/convert.go +++ b/convert.go @@ -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. @@ -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 {