s2b_old.go 431 B

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