stringToBytes returns a []byte view over s without copying, avoiding the allocation+copy of []byte(s) on the response write path (the zero-copy technique used by fasthttp/fiber). Contract — all of the following must hold at every call site: - The result is read-only: writing through it is undefined
(s string)
| 32 | // concrete writer may be a wrapping ResponseWriter (e.g. gzip); such writers must copy, not alias. |
| 33 | // - s must stay reachable for as long as the slice is used: the slice aliases s's backing array. |
| 34 | func stringToBytes(s string) []byte { |
| 35 | if s == "" { |
| 36 | return nil |
| 37 | } |
| 38 | return unsafe.Slice(unsafe.StringData(s), len(s)) |
| 39 | } |
| 40 | |
| 41 | // jsonpOpen and jsonpClose are the constant byte wrappers for JSONP payloads, kept as package-level |
| 42 | // slices to avoid allocating them on every JSONP response. |