...

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

Documentation: github.com/cloudwego/base64x

     1  /*
     2   * Copyright 2024 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package base64x
    18  
    19  import (
    20      `reflect`
    21      `unsafe`
    22  )
    23  
    24  func mem2str(v []byte) (s string) {
    25      (*reflect.StringHeader)(unsafe.Pointer(&s)).Len  = (*reflect.SliceHeader)(unsafe.Pointer(&v)).Len
    26      (*reflect.StringHeader)(unsafe.Pointer(&s)).Data = (*reflect.SliceHeader)(unsafe.Pointer(&v)).Data
    27      return
    28  }
    29  
    30  func str2mem(s string) (v []byte) {
    31      (*reflect.SliceHeader)(unsafe.Pointer(&v)).Cap  = (*reflect.StringHeader)(unsafe.Pointer(&s)).Len
    32      (*reflect.SliceHeader)(unsafe.Pointer(&v)).Len  = (*reflect.StringHeader)(unsafe.Pointer(&s)).Len
    33      (*reflect.SliceHeader)(unsafe.Pointer(&v)).Data = (*reflect.StringHeader)(unsafe.Pointer(&s)).Data
    34      return
    35  }
    36  
    37  func mem2addr(v []byte) unsafe.Pointer {
    38      return *(*unsafe.Pointer)(unsafe.Pointer(&v))
    39  }
    40  
    41  // NoEscape hides a pointer from escape analysis. NoEscape is
    42  // the identity function but escape analysis doesn't think the
    43  // output depends on the input. NoEscape is inlined and currently
    44  // compiles down to zero instructions.
    45  // USE CAREFULLY!
    46  //go:nosplit
    47  //goland:noinspection GoVetUnsafePointer
    48  func noEscape(p unsafe.Pointer) unsafe.Pointer {
    49      x := uintptr(p)
    50      return unsafe.Pointer(x ^ 0)
    51  }
    52  

View as plain text