1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-25 08:21:55 +03:00

Merge pull request #693 from FJSDS/change_bytes_string

fix unsafe.go
This commit is contained in:
Vladimir Mihailenco
2018-01-15 16:12:18 +02:00
committed by GitHub

View File

@ -3,25 +3,20 @@
package internal package internal
import ( import (
"reflect"
"unsafe" "unsafe"
) )
// BytesToString converts byte slice to string.
func BytesToString(b []byte) string { func BytesToString(b []byte) string {
bytesHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b)) return *(*string)(unsafe.Pointer(&b))
strHeader := reflect.StringHeader{
Data: bytesHeader.Data,
Len: bytesHeader.Len,
}
return *(*string)(unsafe.Pointer(&strHeader))
} }
// StringToBytes converts string to byte slice.
func StringToBytes(s string) []byte { func StringToBytes(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) return *(*[]byte)(unsafe.Pointer(
bh := reflect.SliceHeader{ &struct {
Data: sh.Data, string
Len: sh.Len, Cap int
Cap: sh.Len, }{s,len(s)},
} ))
return *(*[]byte)(unsafe.Pointer(&bh))
} }