[runtime] Refactor no-escape and conversion functions.

This commit is contained in:
Ciprian Dorin Craciun 2021-12-16 17:16:49 +02:00
parent d24e149590
commit c0d4b43090

View file

@ -13,31 +13,51 @@ func NoEscape (p unsafe.Pointer) (unsafe.Pointer) {
return unsafe.Pointer (x ^ 0) return unsafe.Pointer (x ^ 0)
} }
func NoEscapeBytes (p *[]byte) (*[]byte) {
return (*[]byte) (NoEscape (unsafe.Pointer (p)))
}
func NoEscapeString (p *string) (*string) {
return (*string) (NoEscape (unsafe.Pointer (p)))
func NoEscapeBytes (_input *[]byte) (*[]byte) {
return (*[]byte) (NoEscape (unsafe.Pointer (_input)))
} }
func BytesToString (b []byte) (string) { func NoEscapeString (_input *string) (*string) {
return *(*string) (unsafe.Pointer (&b)) return (*string) (NoEscape (unsafe.Pointer (_input)))
} }
func StringToBytes (_string string) ([]byte) {
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
}
func StringToBytes (_input string) ([]byte) {
// NOTE: The following is broken! // NOTE: The following is broken!
// return *(*[]byte) (unsafe.Pointer (&_string)) // return *(*[]byte) (unsafe.Pointer (&_input))
// NOTE: Based on `https://github.com/valyala/fasthttp/blob/2a6f7db5bbc4d7c11f1ccc0cb827e145b9b7d7ea/bytesconv.go#L342` // NOTE: Based on `https://github.com/valyala/fasthttp/blob/2a6f7db5bbc4d7c11f1ccc0cb827e145b9b7d7ea/bytesconv.go#L342`
_bytes := []byte (nil) _output := []byte (nil)
_bytesHeader := (*reflect.SliceHeader) (unsafe.Pointer (&_bytes)) _outputHeader := (*reflect.SliceHeader) (unsafe.Pointer (&_output))
_stringHeader := (*reflect.StringHeader) (unsafe.Pointer (&_string)) _inputHeader := (*reflect.StringHeader) (unsafe.Pointer (&_input))
_bytesHeader.Data = _stringHeader.Data
_bytesHeader.Len = _stringHeader.Len _outputHeader.Data = _inputHeader.Data
_bytesHeader.Cap = _stringHeader.Len _outputHeader.Len = _inputHeader.Len
return _bytes _outputHeader.Cap = _inputHeader.Len
return _output
} }