...

Source file src/github.com/chenzhuoyu/base64x/faststr.go

Documentation: github.com/chenzhuoyu/base64x

     1  package base64x
     2  
     3  import (
     4      `reflect`
     5      `unsafe`
     6  )
     7  
     8  func mem2str(v []byte) (s string) {
     9      (*reflect.StringHeader)(unsafe.Pointer(&s)).Len  = (*reflect.SliceHeader)(unsafe.Pointer(&v)).Len
    10      (*reflect.StringHeader)(unsafe.Pointer(&s)).Data = (*reflect.SliceHeader)(unsafe.Pointer(&v)).Data
    11      return
    12  }
    13  
    14  func str2mem(s string) (v []byte) {
    15      (*reflect.SliceHeader)(unsafe.Pointer(&v)).Cap  = (*reflect.StringHeader)(unsafe.Pointer(&s)).Len
    16      (*reflect.SliceHeader)(unsafe.Pointer(&v)).Len  = (*reflect.StringHeader)(unsafe.Pointer(&s)).Len
    17      (*reflect.SliceHeader)(unsafe.Pointer(&v)).Data = (*reflect.StringHeader)(unsafe.Pointer(&s)).Data
    18      return
    19  }
    20  
    21  func mem2addr(v []byte) unsafe.Pointer {
    22      return *(*unsafe.Pointer)(unsafe.Pointer(&v))
    23  }
    24  
    25  // NoEscape hides a pointer from escape analysis. NoEscape is
    26  // the identity function but escape analysis doesn't think the
    27  // output depends on the input. NoEscape is inlined and currently
    28  // compiles down to zero instructions.
    29  // USE CAREFULLY!
    30  //go:nosplit
    31  //goland:noinspection GoVetUnsafePointer
    32  func noEscape(p unsafe.Pointer) unsafe.Pointer {
    33      x := uintptr(p)
    34      return unsafe.Pointer(x ^ 0)
    35  }
    36  

View as plain text