From a8340c6b74299c6521176f26583c020aec5f2b93 Mon Sep 17 00:00:00 2001 From: FJSDS <821431199@qq.com> Date: Thu, 11 Jan 2018 16:07:24 +0800 Subject: [PATCH] fix unsafe.go --- internal/unsafe.go | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/internal/unsafe.go b/internal/unsafe.go index c18b25c..a54a34a 100644 --- a/internal/unsafe.go +++ b/internal/unsafe.go @@ -3,25 +3,20 @@ package internal import ( - "reflect" "unsafe" ) +// BytesToString converts byte slice to string. func BytesToString(b []byte) string { - bytesHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - strHeader := reflect.StringHeader{ - Data: bytesHeader.Data, - Len: bytesHeader.Len, - } - return *(*string)(unsafe.Pointer(&strHeader)) + return *(*string)(unsafe.Pointer(&b)) } +// StringToBytes converts string to byte slice. func StringToBytes(s string) []byte { - sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) - bh := reflect.SliceHeader{ - Data: sh.Data, - Len: sh.Len, - Cap: sh.Len, - } - return *(*[]byte)(unsafe.Pointer(&bh)) + return *(*[]byte)(unsafe.Pointer( + &struct { + string + Cap int + }{s,len(s)}, + )) }