mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
* utils: add Go 1.20+ way of converting string to byte slice Ref.d2f97fc426/s2b_old.go
. Ref.d2f97fc426/s2b_new.go
. * utils: fix golangci-lint apparently running with Go < 1.20 See https://github.com/gofiber/fiber/actions/runs/4968641325/jobs/8891360463?pr=2462.
26 lines
544 B
Go
26 lines
544 B
Go
//go:build !go1.20
|
|
// +build !go1.20
|
|
|
|
package utils
|
|
|
|
import (
|
|
"reflect"
|
|
"unsafe"
|
|
)
|
|
|
|
const MaxStringLen = 0x7fff0000 // Maximum string length for UnsafeBytes. (decimal: 2147418112)
|
|
|
|
// UnsafeBytes returns a byte pointer without allocation.
|
|
// String length shouldn't be more than 2147418112.
|
|
//
|
|
//nolint:gosec // unsafe is used for better performance here
|
|
func UnsafeBytes(s string) []byte {
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
|
|
return (*[MaxStringLen]byte)(unsafe.Pointer(
|
|
(*reflect.StringHeader)(unsafe.Pointer(&s)).Data),
|
|
)[:len(s):len(s)]
|
|
}
|