diff options
Diffstat (limited to 's2b_old.go')
-rw-r--r-- | s2b_old.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/s2b_old.go b/s2b_old.go new file mode 100644 index 0000000..4cc141c --- /dev/null +++ b/s2b_old.go @@ -0,0 +1,24 @@ +//go:build !go1.20 +// +build !go1.20 + +package fasthttp + +import ( + "reflect" + "unsafe" +) + +// s2b converts string to a byte slice without memory allocation. +// +// Note it may break if string and/or slice header will change +// in the future go versions. +func s2b(s string) (b []byte) { + /* #nosec G103 */ + bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) + /* #nosec G103 */ + sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) + bh.Data = sh.Data + bh.Cap = sh.Len + bh.Len = sh.Len + return b +} |