github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/types/string.go (about) 1 package types 2 3 import ( 4 "reflect" 5 "unsafe" 6 ) 7 8 // UnsafeStrToBytes uses unsafe to convert string into byte array. Returned bytes 9 // must not be altered after this function is called as it will cause a segmentation fault. 10 func UnsafeStrToBytes(s string) []byte { 11 var buf []byte 12 sHdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) 13 bufHdr := (*reflect.SliceHeader)(unsafe.Pointer(&buf)) 14 bufHdr.Data = sHdr.Data 15 bufHdr.Cap = sHdr.Len 16 bufHdr.Len = sHdr.Len 17 return buf 18 } 19 20 // UnsafeBytesToStr is meant to make a zero allocation conversion 21 // from []byte -> string to speed up operations, it is not meant 22 // to be used generally, but for a specific pattern to delete keys 23 // from a map. 24 func UnsafeBytesToStr(b []byte) string { 25 return *(*string)(unsafe.Pointer(&b)) 26 }