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