github.com/primecitizens/pcz/std@v0.2.1/builtin/string/types.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 4 package stdstring 5 6 import ( 7 "unsafe" 8 ) 9 10 type Header struct { 11 Str unsafe.Pointer 12 Len int 13 } 14 15 // FromBytes converts a byte slice to string without allocation 16 func FromBytes[T ~byte](b []T) string { 17 return unsafe.String((*byte)(unsafe.Pointer(unsafe.SliceData(b))), len(b)) 18 } 19 20 // FromByteArray converts a sequance of NULL terminated bytes into string 21 // assuming UTF-8 encoding. 22 // 23 //go:nosplit 24 func FromByteArray(arr *byte) string { 25 return unsafe.String(arr, FindNull(arr)) 26 } 27 28 //go:nosplit 29 func ToBytes[T ~byte](s string) []T { 30 return unsafe.Slice((*T)(unsafe.Pointer(unsafe.StringData(s))), len(s)) 31 }