add Unsafe* functions for gconv

This commit is contained in:
john
2019-08-09 09:14:48 +08:00
parent 7172ae0232
commit c7c6e2866e
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,17 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gconv
import "unsafe"
func UnsafeStrToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
}
func UnsafeBytesToStr(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

View File

@ -0,0 +1,25 @@
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gconv_test
import (
"github.com/gogf/gf/test/gtest"
"github.com/gogf/gf/util/gconv"
"testing"
)
func Test_Unsafe(t *testing.T) {
gtest.Case(t, func() {
s := "John爱中国"
gtest.AssertEQ(gconv.UnsafeStrToBytes(s), []byte(s))
})
gtest.Case(t, func() {
b := []byte("John爱中国")
gtest.AssertEQ(gconv.UnsafeBytesToStr(b), string(b))
})
}