2014-04-11 10:21:50 +04:00
|
|
|
package hack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// no copy to change slice to string
|
|
|
|
// use your own risk
|
|
|
|
func String(b []byte) (s string) {
|
|
|
|
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
|
|
|
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
|
|
|
pstring.Data = pbytes.Data
|
|
|
|
pstring.Len = pbytes.Len
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// no copy to change string to slice
|
|
|
|
// use your own risk
|
|
|
|
func Slice(s string) (b []byte) {
|
|
|
|
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
|
|
|
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
|
|
|
pbytes.Data = pstring.Data
|
|
|
|
pbytes.Len = pstring.Len
|
|
|
|
pbytes.Cap = pstring.Len
|
|
|
|
return
|
|
|
|
}
|
2014-05-09 05:39:23 +04:00
|
|
|
|
|
|
|
func Int64Slice(v int64) (b []byte) {
|
|
|
|
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
|
|
|
pbytes.Data = uintptr(unsafe.Pointer(&v))
|
|
|
|
pbytes.Len = 8
|
|
|
|
pbytes.Cap = 8
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func Int32Slice(v int32) (b []byte) {
|
|
|
|
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
|
|
|
pbytes.Data = uintptr(unsafe.Pointer(&v))
|
|
|
|
pbytes.Len = 4
|
|
|
|
pbytes.Cap = 4
|
|
|
|
return
|
|
|
|
}
|