s2b_old.go 487 B

123456789101112131415161718192021222324
  1. //go:build !go1.20
  2. // +build !go1.20
  3. package fasthttp
  4. import (
  5. "reflect"
  6. "unsafe"
  7. )
  8. // s2b converts string to a byte slice without memory allocation.
  9. //
  10. // Note it may break if string and/or slice header will change
  11. // in the future go versions.
  12. func s2b(s string) (b []byte) {
  13. /* #nosec G103 */
  14. bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
  15. /* #nosec G103 */
  16. sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
  17. bh.Data = sh.Data
  18. bh.Cap = sh.Len
  19. bh.Len = sh.Len
  20. return b
  21. }