1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-07 14:12:28 +08:00

refactor: update StringToBytes logic

This commit is contained in:
dudaodong
2024-01-30 10:46:55 +08:00
parent b7370e8ef8
commit 4c21fe700c

View File

@@ -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.