github.com/goplus/llgo@v0.8.3/internal/runtime/z_string.go (about)

     1  /*
     2   * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
     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 runtime
    18  
    19  import (
    20  	"unsafe"
    21  
    22  	"github.com/goplus/llgo/internal/runtime/c"
    23  )
    24  
    25  // -----------------------------------------------------------------------------
    26  
    27  // String is the runtime representation of a string.
    28  // It cannot be used safely or portably and its representation may
    29  // change in a later release.
    30  //
    31  // Unlike reflect.StringHeader, its Data field is sufficient to guarantee the
    32  // data it references will not be garbage collected.
    33  type String struct {
    34  	data unsafe.Pointer
    35  	len  int
    36  }
    37  
    38  // EmptyString returns an empty string.
    39  func EmptyString() String {
    40  	return String{nil, 0}
    41  }
    42  
    43  // NewString creates a new string.
    44  func NewString(data unsafe.Pointer, len int) String {
    45  	return String{data, len}
    46  }
    47  
    48  // StringCat concatenates two strings.
    49  func StringCat(a, b String) String {
    50  	n := a.len + b.len
    51  	dest := AllocU(uintptr(n))
    52  	c.Memcpy(dest, a.data, uintptr(a.len))
    53  	c.Memcpy(c.Advance(dest, a.len), b.data, uintptr(b.len))
    54  	return String{dest, n}
    55  }
    56  
    57  // -----------------------------------------------------------------------------
    58  
    59  // CStrCopy copies a Go string to a C string buffer and returns it.
    60  func CStrCopy(dest unsafe.Pointer, s String) *int8 {
    61  	n := s.len
    62  	c.Memcpy(dest, s.data, uintptr(n))
    63  	arr := (*[1 << 30]int8)(dest)
    64  	arr[n] = 0
    65  	return (*int8)(dest)
    66  }
    67  
    68  func CStrDup(s String) *int8 {
    69  	dest := AllocU(uintptr(s.len + 1))
    70  	return CStrCopy(dest, s)
    71  }
    72  
    73  func NewStringSlice(base String, i, j int) String {
    74  	if i < 0 || j < i || j > base.len {
    75  		panic("string slice index out of bounds")
    76  	}
    77  	if i < base.len {
    78  		return String{c.Advance(base.data, i), j - i}
    79  	}
    80  	return String{nil, 0}
    81  }
    82  
    83  // -----------------------------------------------------------------------------