2019-12-30 20:53:28 -05: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 22:43:18 -04:00
|
|
|
func StringToReadOnlyBytes(s string) (bs []byte) {
|
2019-12-30 20:53:28 -05:00
|
|
|
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
2020-06-24 22:43:18 -04:00
|
|
|
bh := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
|
|
|
|
bh.Data = sh.Data
|
|
|
|
bh.Cap = sh.Len
|
|
|
|
bh.Len = sh.Len
|
|
|
|
return
|
2019-12-30 20:53:28 -05:00
|
|
|
}
|