2020-02-28 23:11:07 +00:00
|
|
|
package uutils
|
|
|
|
|
|
|
|
import (
|
2022-02-21 03:16:15 +00:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"unsafe"
|
2020-02-28 23:11:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: Add a safe build mode for things like Google Appengine
|
|
|
|
|
|
|
|
func StringToBytes(s string) (bytes []byte) {
|
2022-02-21 03:16:15 +00:00
|
|
|
str := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
|
|
|
slice := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
|
|
|
|
slice.Data = str.Data
|
|
|
|
slice.Len = str.Len
|
|
|
|
slice.Cap = str.Len
|
|
|
|
runtime.KeepAlive(&s)
|
|
|
|
return bytes
|
2020-02-28 23:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BytesToString(bytes []byte) (s string) {
|
2022-02-21 03:16:15 +00:00
|
|
|
slice := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
|
|
|
|
str := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
|
|
|
str.Data = slice.Data
|
|
|
|
str.Len = slice.Len
|
|
|
|
runtime.KeepAlive(&bytes)
|
|
|
|
return s
|
2020-02-28 23:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
//go:linkname nanotime runtime.nanotime
|
|
|
|
func nanotime() int64
|
|
|
|
|
|
|
|
func Nanotime() int64 {
|
2022-02-21 03:16:15 +00:00
|
|
|
return nanotime()
|
2020-02-28 23:11:07 +00:00
|
|
|
}
|