s2b_old.go 449 B

12345678910111213141516171819202122
  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. bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
  14. sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
  15. bh.Data = sh.Data
  16. bh.Cap = sh.Len
  17. bh.Len = sh.Len
  18. return b
  19. }