github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/convert/string-unsafe.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package convert
     4  
     5  import (
     6  	"reflect"
     7  	"unsafe"
     8  )
     9  
    10  // UnsafeStringToByteSlice returns ...
    11  func UnsafeStringToByteSlice(req string) (res []byte) {
    12  	var reqStruct = (*reflect.StringHeader)(unsafe.Pointer(&req))
    13  	var resStruct = (*reflect.SliceHeader)(unsafe.Pointer(&res))
    14  	resStruct.Data = reqStruct.Data
    15  	resStruct.Len = reqStruct.Len
    16  	resStruct.Cap = reqStruct.Len
    17  	return
    18  }
    19  
    20  // UnsafeByteSliceToString returns ...
    21  func UnsafeByteSliceToString(req []byte) (res string) {
    22  	var reqStruct = (*reflect.SliceHeader)(unsafe.Pointer(&req))
    23  	var resStruct = (*reflect.StringHeader)(unsafe.Pointer(&res))
    24  	resStruct.Data = reqStruct.Data
    25  	resStruct.Len = reqStruct.Len
    26  	return
    27  }