mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
31 lines
621 B
Go
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))
|
||
|
}
|