2018-11-15 17:11:34 +00:00
|
|
|
|
|
|
|
package common
|
|
|
|
|
2021-11-17 19:42:36 +00:00
|
|
|
import "reflect"
|
2018-11-15 17:11:34 +00:00
|
|
|
import "unsafe"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
func NoEscape (p unsafe.Pointer) (unsafe.Pointer) {
|
|
|
|
x := uintptr (p)
|
|
|
|
return unsafe.Pointer (x ^ 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NoEscapeBytes (p *[]byte) (*[]byte) {
|
2021-11-17 19:42:36 +00:00
|
|
|
return (*[]byte) (NoEscape (unsafe.Pointer (p)))
|
2018-11-15 17:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NoEscapeString (p *string) (*string) {
|
2021-11-17 19:42:36 +00:00
|
|
|
return (*string) (NoEscape (unsafe.Pointer (p)))
|
2018-11-15 17:11:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-17 17:41:00 +00:00
|
|
|
|
|
|
|
func BytesToString (b []byte) (string) {
|
2019-08-11 22:29:04 +00:00
|
|
|
return *(*string) (unsafe.Pointer (&b))
|
|
|
|
}
|
|
|
|
|
2021-11-17 19:42:36 +00:00
|
|
|
func StringToBytes (_string string) ([]byte) {
|
|
|
|
|
|
|
|
// NOTE: The following is broken!
|
|
|
|
// return *(*[]byte) (unsafe.Pointer (&_string))
|
|
|
|
|
|
|
|
// NOTE: Based on `https://github.com/valyala/fasthttp/blob/2a6f7db5bbc4d7c11f1ccc0cb827e145b9b7d7ea/bytesconv.go#L342`
|
|
|
|
_bytes := []byte (nil)
|
|
|
|
_bytesHeader := (*reflect.SliceHeader) (unsafe.Pointer (&_bytes))
|
|
|
|
_stringHeader := (*reflect.StringHeader) (unsafe.Pointer (&_string))
|
|
|
|
_bytesHeader.Data = _stringHeader.Data
|
|
|
|
_bytesHeader.Len = _stringHeader.Len
|
|
|
|
_bytesHeader.Cap = _stringHeader.Len
|
|
|
|
return _bytes
|
2018-11-17 17:41:00 +00:00
|
|
|
}
|
|
|
|
|