github.com/bytedance/mockey@v1.2.10/internal/unsafereflect/name_above_1_17.go (about)

     1  //go:build go1.17
     2  // +build go1.17
     3  
     4  /*
     5   * Copyright 2023 ByteDance Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package unsafereflect
    21  
    22  import "unsafe"
    23  
    24  // name is an encoded type name with optional extra data.
    25  type name struct {
    26  	bytes *byte
    27  }
    28  
    29  func (n name) data(off int, whySafe string) *byte {
    30  	return (*byte)(add(unsafe.Pointer(n.bytes), uintptr(off), whySafe))
    31  }
    32  
    33  func (n name) readVarint(off int) (int, int) {
    34  	v := 0
    35  	for i := 0; ; i++ {
    36  		x := *n.data(off+i, "read varint")
    37  		v += int(x&0x7f) << (7 * i)
    38  		if x&0x80 == 0 {
    39  			return i + 1, v
    40  		}
    41  	}
    42  }
    43  
    44  func (n name) name() (s string) {
    45  	if n.bytes == nil {
    46  		return
    47  	}
    48  	i, l := n.readVarint(1)
    49  	hdr := (*_String)(unsafe.Pointer(&s))
    50  	hdr.Data = unsafe.Pointer(n.data(1+i, "non-empty string"))
    51  	hdr.Len = l
    52  	return
    53  }