1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-22 00:33:09 +00:00
fiber/utils/convert.go
Fenny a63f4f067b 🛠 export utils
Co-Authored-By: RW <7063188+ReneWerner87@users.noreply.github.com>
2020-09-14 12:16:45 +02:00

33 lines
786 B
Go

// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import (
"reflect"
"unsafe"
)
// #nosec G103
// GetString returns a string pointer without allocation
func GetString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// #nosec G103
// GetBytes returns a byte pointer without allocation
func GetBytes(s string) (bs []byte) {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
return
}
// ImmutableString copies a string to make it immutable
func ImmutableString(s string) string {
return string(GetBytes(s))
}