Optimize TouUnsafeBytes to make it leaner, more standards-compliant and (#5880)

slightly faster.
This commit is contained in:
helen 2024-02-29 23:10:10 +08:00 committed by GitHub
parent b9b4e859be
commit 8266b77d0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,7 +2,6 @@ package bytesutil
import (
"math/bits"
"reflect"
"unsafe"
)
@ -72,10 +71,5 @@ func ToUnsafeString(b []byte) string {
//
// The returned byte slice is valid only until s is reachable and unmodified.
func ToUnsafeBytes(s string) (b []byte) {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
slh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
slh.Data = sh.Data
slh.Len = sh.Len
slh.Cap = sh.Len
return b
return unsafe.Slice(unsafe.StringData(s), len(s))
}