2019-12-30 18:53:28 -07:00
|
|
|
// +build !appengine,!js
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BytesToReadOnlyString returns a string converted from given bytes.
|
|
|
|
func BytesToReadOnlyString(b []byte) string {
|
|
|
|
return *(*string)(unsafe.Pointer(&b))
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringToReadOnlyBytes returns bytes converted from given string.
|
2020-06-24 20:43:18 -06:00
|
|
|
func StringToReadOnlyBytes(s string) (bs []byte) {
|
2019-12-30 18:53:28 -07:00
|
|
|
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
2020-06-24 20:43:18 -06:00
|
|
|
bh := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
|
|
|
|
bh.Data = sh.Data
|
|
|
|
bh.Cap = sh.Len
|
|
|
|
bh.Len = sh.Len
|
|
|
|
return
|
2019-12-30 18:53:28 -07:00
|
|
|
}
|