llgo/ssa: allocaCStr; runtime: String

This commit is contained in:
xushiwei
2024-04-30 18:22:56 +08:00
parent c6cb2931e1
commit ae0906d322
9 changed files with 164 additions and 4 deletions

View File

@@ -18,6 +18,8 @@ package runtime
import (
"unsafe"
"github.com/goplus/llgo/internal/runtime/c"
)
// -----------------------------------------------------------------------------
@@ -38,4 +40,28 @@ func EmptyString() String {
return String{nil, 0}
}
// StringLen returns the length of a string.
func StringLen(s Slice) int {
return s.len
}
// StringData returns the data pointer of a string.
func StringData(s String) unsafe.Pointer {
return s.data
}
// CStrCopy copies a Go string to a C string buffer and returns it.
func CStrCopy(dest unsafe.Pointer, s String) *int8 {
n := s.len
c.Memcpy(dest, s.data, uintptr(n))
arr := (*[1 << 30]int8)(dest)
arr[n] = 0
return (*int8)(dest)
}
func CStrDup(s String) *int8 {
dest := Alloc(uintptr(s.len + 1))
return CStrCopy(dest, s)
}
// -----------------------------------------------------------------------------