1
0
Fork 0
mirror of https://github.com/chrislusf/seaweedfs synced 2024-06-26 04:18:59 +02:00
seaweedfs/weed/query/sqltypes/unsafe.go
Chris Lu e26670c67a add some basic sql types
copied some code from vitness
2019-10-06 22:35:08 -07:00

31 lines
621 B
Go

package sqltypes
import (
"reflect"
"unsafe"
)
// BytesToString casts slice to string without copy
func BytesToString(b []byte) (s string) {
if len(b) == 0 {
return ""
}
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := reflect.StringHeader{Data: bh.Data, Len: bh.Len}
return *(*string)(unsafe.Pointer(&sh))
}
// StringToBytes casts string to slice without copy
func StringToBytes(s string) []byte {
if len(s) == 0 {
return []byte{}
}
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{Data: sh.Data, Len: sh.Len, Cap: sh.Len}
return *(*[]byte)(unsafe.Pointer(&bh))
}