Files
mochi/pkg/str2bytes/str2bytes.go
Lawrence, Rendall 665899017e (untested) move unsafe string conversion to pkg
* fix invalid UDP f/e start when no workers provided
* add bench for entire server
2023-03-22 17:51:13 +03:00

22 lines
601 B
Go

// Package str2bytes provides fast, but unsafe functions to convert string to []byte
// or vice versa.
package str2bytes
import "unsafe"
// StringToBytes converts string to slice of bytes
// without allocation. Note, that returned slice
// must NOT be modified, since strings in Go are
// immutable.
// See unsafe.Slice.
func StringToBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
// BytesToString converts slice of bytes to string
// without allocation.
// See unsafe.String
func BytesToString(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}