// Unsafe string↔[]byte conversion library // Copyright (c) 2017 by Michał Nazarewicz // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // Package unsafeConvert provides functions for unsafely convert between // a string and a slice of bytes. You probably shouldn’t use it unless you need // to squeeze extra performance from your performance-critical code path. package unsafeConvert import ( "reflect" "unsafe" ) // String converts a slice of bytes into a string without performing a copy. // NOTE: This is an unsafe operation and may lead to problems if the bytes // passed as argument are changed while the string is used. No checking whether // bytes are valid UTF-8 data is performed. func String(bytes []byte) string { hdr := *(*reflect.SliceHeader)(unsafe.Pointer(&bytes)) return *(*string)(unsafe.Pointer(&reflect.StringHeader{ Data: hdr.Data, Len: hdr.Len, })) } // Bytes converts a string into a slice of bytes without performing a copy. // NOTE: This is an unsafe operation and may lead to problems if the bytes are // changed. func Bytes(str string) []byte { hdr := *(*reflect.StringHeader)(unsafe.Pointer(&str)) return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ Data: hdr.Data, Len: hdr.Len, Cap: hdr.Len, })) } // Following added for libsimpleio by Philip Munts on 25 October 2020 func Ptr(str string) *byte { return &Bytes(str)[0] }