github.com/moreal/bencodex-go@v0.0.0-20231021172012-18277a477d15/internal/unsafe.go (about)

     1  package internal
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  // https://github.com/valyala/fastjson/blob/master/util.go
     9  
    10  func B2S(b []byte) string {
    11  	return *(*string)(unsafe.Pointer(&b))
    12  }
    13  
    14  func S2B(s string) (b []byte) {
    15  	strh := (*reflect.StringHeader)(unsafe.Pointer(&s))
    16  	sh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
    17  	sh.Data = strh.Data
    18  	sh.Len = strh.Len
    19  	sh.Cap = strh.Len
    20  	return b
    21  }