cosmos-sdk/internal/conv/string.go
mergify[bot] ea4408c405
refactor: using unsafe.String and unsafe.SliceData (backport #21412) (#21545)
Co-authored-by: cui <523516579@qq.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
2024-09-04 15:35:17 +00:00

20 lines
685 B
Go

package conv
import (
"unsafe"
)
// UnsafeStrToBytes uses unsafe to convert string into byte array. Returned bytes
// must not be altered after this function is called as it will cause a segmentation fault.
func UnsafeStrToBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s)) // ref https://github.com/golang/go/issues/53003#issuecomment-1140276077
}
// UnsafeBytesToStr is meant to make a zero allocation conversion
// from []byte -> string to speed up operations, it is not meant
// to be used generally, but for a specific pattern to delete keys
// from a map.
func UnsafeBytesToStr(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}