2019-12-31 07:23:28 +05:30
|
|
|
// +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-25 08:13:18 +05:30
|
|
|
func StringToReadOnlyBytes(s string) (bs []byte) {
|
2019-12-31 07:23:28 +05:30
|
|
|
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
2020-06-25 08:13:18 +05:30
|
|
|
bh := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
|
|
|
|
bh.Data = sh.Data
|
|
|
|
bh.Cap = sh.Len
|
|
|
|
bh.Len = sh.Len
|
|
|
|
return
|
2019-12-31 07:23:28 +05:30
|
|
|
}
|