2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-08-09 09:14:48 +08:00
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2019-08-09 20:05:36 +08:00
|
|
|
// UnsafeStrToBytes converts string to []byte without memory copy.
|
2021-03-23 17:53:20 +08:00
|
|
|
// Note that, if you completely sure you will never use `s` variable in the feature,
|
2019-09-29 15:59:09 +08:00
|
|
|
// you can use this unsafe function to implement type conversion in high performance.
|
2019-08-09 09:14:48 +08:00
|
|
|
func UnsafeStrToBytes(s string) []byte {
|
|
|
|
return *(*[]byte)(unsafe.Pointer(&s))
|
|
|
|
}
|
|
|
|
|
2019-08-09 20:05:36 +08:00
|
|
|
// UnsafeBytesToStr converts []byte to string without memory copy.
|
2021-03-23 17:53:20 +08:00
|
|
|
// Note that, if you completely sure you will never use `b` variable in the feature,
|
2019-09-29 15:59:09 +08:00
|
|
|
// you can use this unsafe function to implement type conversion in high performance.
|
2019-08-09 09:14:48 +08:00
|
|
|
func UnsafeBytesToStr(b []byte) string {
|
|
|
|
return *(*string)(unsafe.Pointer(&b))
|
|
|
|
}
|