-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcno.go
More file actions
23 lines (20 loc) · 686 Bytes
/
cno.go
File metadata and controls
23 lines (20 loc) · 686 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package cno
import (
"syscall"
"unsafe"
)
// Function to convert a Go string to a UTF8 C string and return its pointer
func CStr(str string) (uintptr) {
ptr, err := syscall.BytePtrFromString(str)
if err != nil {panic(err)}
return uintptr(unsafe.Pointer(ptr))
}
// Function to identify a UTF8 C string and return a copy of its contents as a Go string
// As a copy is returned, the C string may be freed without affecting the returned Go string
func GStr(ptr uintptr) (string) {
len := 0
for ;; len++ {if *(*byte)(unsafe.Pointer(ptr + uintptr(len))) == 0 {break}}
bytes := make([]byte, len)
copy(bytes, unsafe.Slice((*byte)(unsafe.Pointer(ptr)), len))
return string(bytes)
}