From 4c21fe700c6e807f957dd9816a621313ee47ea61 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 30 Jan 2024 10:46:55 +0800 Subject: [PATCH] refactor: update StringToBytes logic --- strutil/string.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/strutil/string.go b/strutil/string.go index 42b7e62..770baf0 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -4,7 +4,6 @@ package strutil import ( - "reflect" "regexp" "strings" "unicode" @@ -380,10 +379,7 @@ func RemoveNonPrintable(str string) string { // StringToBytes converts a string to byte slice without a memory allocation. // Play: https://go.dev/play/p/7OyFBrf9AxA func StringToBytes(str string) (b []byte) { - sh := *(*reflect.StringHeader)(unsafe.Pointer(&str)) - bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len - return b + return *(*[]byte)(unsafe.Pointer(&str)) } // BytesToString converts a byte slice to string without a memory allocation.