github.com/agiledragon/gomonkey/v2@v2.11.1-0.20240427155748-d56c6823ec17/creflect/ae1.17.go (about)

     1  //go:build go1.17
     2  // +build go1.17
     3  
     4  package creflect
     5  
     6  import (
     7  	"unsafe"
     8  )
     9  
    10  // name is an encoded type name with optional extra data.
    11  type name struct {
    12  	bytes *byte
    13  }
    14  
    15  func (n name) data(off int, whySafe string) *byte {
    16  	return (*byte)(add(unsafe.Pointer(n.bytes), uintptr(off), whySafe))
    17  }
    18  
    19  func (n name) readVarint(off int) (int, int) {
    20  	v := 0
    21  	for i := 0; ; i++ {
    22  		x := *n.data(off+i, "read varint")
    23  		v += int(x&0x7f) << (7 * i)
    24  		if x&0x80 == 0 {
    25  			return i + 1, v
    26  		}
    27  	}
    28  }
    29  
    30  func (n name) name() (s string) {
    31  	if n.bytes == nil {
    32  		return
    33  	}
    34  	i, l := n.readVarint(1)
    35  	hdr := (*String)(unsafe.Pointer(&s))
    36  	hdr.Data = unsafe.Pointer(n.data(1+i, "non-empty string"))
    37  	hdr.Len = l
    38  	return
    39  }