mirror of
https://github.com/sot-tech/mochi.git
synced 2026-04-26 23:50:00 -07:00
(untested) move unsafe string conversion to pkg
* fix invalid UDP f/e start when no workers provided * add bench for entire server
This commit is contained in:
21
pkg/str2bytes/str2bytes.go
Normal file
21
pkg/str2bytes/str2bytes.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// 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))
|
||||
}
|
||||
Reference in New Issue
Block a user