mirror of https://github.com/gin-gonic/gin.git
Security Optimization string to byte slice
This commit is contained in:
parent
78dad9d77d
commit
8c235252b6
|
@ -4,21 +4,12 @@
|
||||||
|
|
||||||
package bytesconv
|
package bytesconv
|
||||||
|
|
||||||
import (
|
// StringToBytes converts string to byte slice
|
||||||
"unsafe"
|
|
||||||
)
|
|
||||||
|
|
||||||
// StringToBytes converts string to byte slice without a memory allocation.
|
|
||||||
func StringToBytes(s string) []byte {
|
func StringToBytes(s string) []byte {
|
||||||
return *(*[]byte)(unsafe.Pointer(
|
return []byte(s)
|
||||||
&struct {
|
|
||||||
string
|
|
||||||
Cap int
|
|
||||||
}{s, len(s)},
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BytesToString converts byte slice to string without a memory allocation.
|
// BytesToString converts byte slice to string
|
||||||
func BytesToString(b []byte) string {
|
func BytesToString(b []byte) string {
|
||||||
return *(*string)(unsafe.Pointer(&b))
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue