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)
|
|
|
|
}
|
|
|
|
|
2021-12-16 15:16:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func NoEscapeBytes (_input *[]byte) (*[]byte) {
|
|
|
|
return (*[]byte) (NoEscape (unsafe.Pointer (_input)))
|
2018-11-15 17:11:34 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 15:16:49 +00:00
|
|
|
|
|
|
|
func NoEscapeString (_input *string) (*string) {
|
|
|
|
return (*string) (NoEscape (unsafe.Pointer (_input)))
|
2018-11-15 17:11:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-17 17:41:00 +00:00
|
|
|
|
2021-12-16 15:16:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
func BytesToString (_input []byte) (string) {
|
|
|
|
|
|
|
|
// NOTE: The following is not enough?!
|
|
|
|
return *(*string) (unsafe.Pointer (&_input))
|
|
|
|
|
|
|
|
_output := ""
|
|
|
|
_inputHeader := (*reflect.SliceHeader) (unsafe.Pointer (&_input))
|
|
|
|
_outputHeader := (*reflect.StringHeader) (unsafe.Pointer (&_output))
|
|
|
|
|
|
|
|
_outputHeader.Data = _inputHeader.Data
|
|
|
|
_outputHeader.Len = _inputHeader.Len
|
|
|
|
|
|
|
|
return _output
|
2019-08-11 22:29:04 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 15:16:49 +00:00
|
|
|
|
|
|
|
func StringToBytes (_input string) ([]byte) {
|
2021-11-17 19:42:36 +00:00
|
|
|
|
|
|
|
// NOTE: The following is broken!
|
2021-12-16 15:16:49 +00:00
|
|
|
// return *(*[]byte) (unsafe.Pointer (&_input))
|
2021-11-17 19:42:36 +00:00
|
|
|
|
|
|
|
// NOTE: Based on `https://github.com/valyala/fasthttp/blob/2a6f7db5bbc4d7c11f1ccc0cb827e145b9b7d7ea/bytesconv.go#L342`
|
2021-12-16 15:16:49 +00:00
|
|
|
_output := []byte (nil)
|
|
|
|
_outputHeader := (*reflect.SliceHeader) (unsafe.Pointer (&_output))
|
|
|
|
_inputHeader := (*reflect.StringHeader) (unsafe.Pointer (&_input))
|
|
|
|
|
|
|
|
_outputHeader.Data = _inputHeader.Data
|
|
|
|
_outputHeader.Len = _inputHeader.Len
|
|
|
|
_outputHeader.Cap = _inputHeader.Len
|
|
|
|
|
|
|
|
return _output
|
2018-11-17 17:41:00 +00:00
|
|
|
}
|
|
|
|
|