modernise reflect.SliceHeader to unsafe.Slice

Signed-off-by: Jakob Borg <jakob@kastelo.net>
This commit is contained in:
Jakob Borg
2026-05-19 12:58:35 +02:00
parent 20826e87d8
commit fed9970582
2 changed files with 10 additions and 29 deletions

View File

@@ -28,7 +28,6 @@ import "C"
import ( import (
"math" "math"
"reflect"
"unsafe" "unsafe"
) )
@@ -91,9 +90,11 @@ func (c *SQLiteContext) ResultNull() {
// ResultText sets the result of an SQL function. // ResultText sets the result of an SQL function.
// See: sqlite3_result_text, http://sqlite.org/c3ref/result_blob.html // See: sqlite3_result_text, http://sqlite.org/c3ref/result_blob.html
func (c *SQLiteContext) ResultText(s string) { func (c *SQLiteContext) ResultText(s string) {
h := (*reflect.StringHeader)(unsafe.Pointer(&s)) if len(s) == 0 {
cs, l := (*C.char)(unsafe.Pointer(h.Data)), C.int(h.Len) C.my_result_text((*C.sqlite3_context)(c), (*C.char)(unsafe.Pointer(&placeHolder[0])), 0)
C.my_result_text((*C.sqlite3_context)(c), cs, l) return
}
C.my_result_text((*C.sqlite3_context)(c), (*C.char)(unsafe.Pointer(unsafe.StringData(s))), C.int(len(s)))
} }
// ResultZeroblob sets the result of an SQL function. // ResultZeroblob sets the result of an SQL function.

View File

@@ -270,7 +270,6 @@ import "C"
import ( import (
"fmt" "fmt"
"math" "math"
"reflect"
"unsafe" "unsafe"
) )
@@ -329,11 +328,7 @@ type InfoOrderBy struct {
} }
func constraints(info *C.sqlite3_index_info) []InfoConstraint { func constraints(info *C.sqlite3_index_info) []InfoConstraint {
slice := *(*[]C.struct_sqlite3_index_constraint)(unsafe.Pointer(&reflect.SliceHeader{ slice := unsafe.Slice(info.aConstraint, int(info.nConstraint))
Data: uintptr(unsafe.Pointer(info.aConstraint)),
Len: int(info.nConstraint),
Cap: int(info.nConstraint),
}))
cst := make([]InfoConstraint, 0, len(slice)) cst := make([]InfoConstraint, 0, len(slice))
for _, c := range slice { for _, c := range slice {
@@ -351,11 +346,7 @@ func constraints(info *C.sqlite3_index_info) []InfoConstraint {
} }
func orderBys(info *C.sqlite3_index_info) []InfoOrderBy { func orderBys(info *C.sqlite3_index_info) []InfoOrderBy {
slice := *(*[]C.struct_sqlite3_index_orderby)(unsafe.Pointer(&reflect.SliceHeader{ slice := unsafe.Slice(info.aOrderBy, int(info.nOrderBy))
Data: uintptr(unsafe.Pointer(info.aOrderBy)),
Len: int(info.nOrderBy),
Cap: int(info.nOrderBy),
}))
ob := make([]InfoOrderBy, 0, len(slice)) ob := make([]InfoOrderBy, 0, len(slice))
for _, c := range slice { for _, c := range slice {
@@ -400,10 +391,7 @@ func goMInit(db, pClientData unsafe.Pointer, argc C.int, argv **C.char, pzErr **
return 0 return 0
} }
args := make([]string, argc) args := make([]string, argc)
var A []*C.char for i, s := range unsafe.Slice(argv, int(argc)) {
slice := reflect.SliceHeader{Data: uintptr(unsafe.Pointer(argv)), Len: int(argc), Cap: int(argc)}
a := reflect.NewAt(reflect.TypeOf(A), unsafe.Pointer(&slice)).Elem().Interface()
for i, s := range a.([]*C.char) {
args[i] = C.GoString(s) args[i] = C.GoString(s)
} }
var vTab VTab var vTab VTab
@@ -466,11 +454,7 @@ func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char {
// Get a pointer to constraint_usage struct so we can update in place. // Get a pointer to constraint_usage struct so we can update in place.
slice := *(*[]C.struct_sqlite3_index_constraint_usage)(unsafe.Pointer(&reflect.SliceHeader{ slice := unsafe.Slice(info.aConstraintUsage, int(info.nConstraint))
Data: uintptr(unsafe.Pointer(info.aConstraintUsage)),
Len: int(info.nConstraint),
Cap: int(info.nConstraint),
}))
index := 1 index := 1
for i := range slice { for i := range slice {
if res.Used[i] { if res.Used[i] {
@@ -488,11 +472,7 @@ func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char {
} }
info.needToFreeIdxStr = C.int(1) info.needToFreeIdxStr = C.int(1)
idxStr := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ idxStr := unsafe.Slice((*byte)(unsafe.Pointer(info.idxStr)), len(res.IdxStr)+1)
Data: uintptr(unsafe.Pointer(info.idxStr)),
Len: len(res.IdxStr) + 1,
Cap: len(res.IdxStr) + 1,
}))
copy(idxStr, res.IdxStr) copy(idxStr, res.IdxStr)
idxStr[len(idxStr)-1] = 0 // null-terminated string idxStr[len(idxStr)-1] = 0 // null-terminated string