forked from mattn/go-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunsafe_go121.go
32 lines (27 loc) · 870 Bytes
/
unsafe_go121.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//go:build go1.21
// +build go1.21
// The unsafe.StringData function was made available in Go 1.20 but it
// was not until Go 1.21 that Go was changed to interpret the Go version
// in go.mod (1.19 as of writing this) as the minimum version required
// instead of the exact version.
//
// See: https://github.com/golang/go/issues/59033
package sqlite3
import "unsafe"
// stringData is a safe version of unsafe.StringData that handles empty strings.
func stringData(s string) *byte {
if len(s) != 0 {
return unsafe.StringData(s)
}
// The return value of unsafe.StringData
// is unspecified if the string is empty.
return &placeHolder[0]
}
// unsafeString returns a string value whose underlying bytes start at ptr and
// whose length is n.
func unsafeString(ptr *byte, n int) string {
if ptr == nil || n <= 0 {
return ""
}
return unsafe.String(ptr, n)
}