Skip to content

Commit

Permalink
byteutil/stringutil: use reflect
Browse files Browse the repository at this point in the history
xushiwei committed Feb 25, 2024
1 parent 181187e commit a134450
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion byteutil/bytes_reflect.go
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ import (

// Bytes returns a byte slice whose underlying data is s.
func Bytes(s string) (b []byte) {
bh := *(*reflect.SliceHeader)(unsafe.Pointer(&b))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len
return
2 changes: 1 addition & 1 deletion stringutil/string_reflect.go
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ import (
// Since Go strings are immutable, the bytes passed to String
// must not be modified afterwards.
func String(b []byte) (s string) {
sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := *(*reflect.SliceHeader)(unsafe.Pointer(&b))
sh.Data, sh.Len = bh.Data, bh.Len
return

0 comments on commit a134450

Please sign in to comment.