Merge pull request '[agent/codex:gpt-5.3-codex-spark] Read docs/RFC.md fully. Find ONE feature described in the sp...' (#2) from main into dev
This commit is contained in:
commit
279c2f4e13
2 changed files with 76 additions and 1 deletions
|
|
@ -1,6 +1,9 @@
|
|||
package cgo
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func TestBufferLifecycleAndCopy(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
|
@ -68,6 +71,39 @@ func TestBufferUseAfterFreePanics(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestCStringRoundTrip(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
source := "hello"
|
||||
cString := CString(source)
|
||||
defer Free(unsafe.Pointer(cString))
|
||||
|
||||
converted := GoString(cString)
|
||||
if converted != source {
|
||||
t.Fatalf("expected %q, got %q", source, converted)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoStringNilIsEmpty(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := GoString(nil)
|
||||
if got != "" {
|
||||
t.Fatalf("expected empty string, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFreeNilDoesNotPanic(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("Free(nil) panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
Free(nil)
|
||||
}
|
||||
|
||||
func assertPanics(t *testing.T, want string, fn func()) {
|
||||
t.Helper()
|
||||
|
||||
|
|
|
|||
39
string_conversion.go
Normal file
39
string_conversion.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package cgo
|
||||
|
||||
/*
|
||||
#include <stdlib.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import "unsafe"
|
||||
|
||||
// GoString converts a null-terminated C string to a Go string.
|
||||
//
|
||||
// cStr := C.CString("example")
|
||||
// result := cgo.GoString(cStr)
|
||||
// cgo.Free(unsafe.Pointer(cStr))
|
||||
func GoString(cs *C.char) string {
|
||||
if cs == nil {
|
||||
return ""
|
||||
}
|
||||
return C.GoString(cs)
|
||||
}
|
||||
|
||||
// CString converts a Go string to a C string.
|
||||
//
|
||||
// cStr := cgo.CString("hello")
|
||||
// defer cgo.Free(unsafe.Pointer(cStr))
|
||||
func CString(value string) *C.char {
|
||||
return C.CString(value)
|
||||
}
|
||||
|
||||
// Free releases memory previously returned by CString.
|
||||
//
|
||||
// cStr := cgo.CString("hello")
|
||||
// cgo.Free(unsafe.Pointer(cStr))
|
||||
func Free(ptr unsafe.Pointer) {
|
||||
if ptr == nil {
|
||||
return
|
||||
}
|
||||
C.free(ptr)
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue