| 1 | //go:build !appengine && !js && !go1.21 |
| 2 | // +build !appengine,!js,!go1.21 |
| 3 | |
| 4 | package util |
| 5 | |
| 6 | import ( |
| 7 | "reflect" |
| 8 | "unsafe" |
| 9 | ) |
| 10 | |
| 11 | // BytesToReadOnlyString returns a string converted from given bytes. |
| 12 | func BytesToReadOnlyString(b []byte) string { |
| 13 | return *(*string)(unsafe.Pointer(&b)) |
| 14 | } |
| 15 | |
| 16 | // StringToReadOnlyBytes returns bytes converted from given string. |
| 17 | func StringToReadOnlyBytes(s string) (bs []byte) { |
| 18 | sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) |
| 19 | bh := (*reflect.SliceHeader)(unsafe.Pointer(&bs)) |
| 20 | bh.Data = sh.Data |
| 21 | bh.Cap = sh.Len |
| 22 | bh.Len = sh.Len |
| 23 | return |
| 24 | } |