-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
43 lines (39 loc) · 762 Bytes
/
utils.go
File metadata and controls
43 lines (39 loc) · 762 Bytes
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
33
34
35
36
37
38
39
40
41
42
43
package main
/*
#include <wchar.h>
*/
import "C"
import (
"unicode/utf16"
"unsafe"
)
func WCharToString(wc *C.wchar_t) string {
if wc == nil {
return ""
}
const max = 0xffff
p := (*[max]uint16)(unsafe.Pointer(wc))
s := p[:max]
for i, v := range s {
if v == 0 {
s = s[0:i]
break
}
}
return string(utf16.Decode(s))
}
func WCharFromString(s string, trailingZero bool) []C.wchar_t {
runes := utf16.Encode([]rune(s))
if trailingZero {
runes = append(runes, 0)
}
cRunes := make([]C.wchar_t, len(runes))
for i, r := range runes {
cRunes[i] = C.wchar_t(r)
}
return cRunes
}
func WCharPtrFromString(s string, trailingZero bool) *C.wchar_t {
runes := WCharFromString(s, trailingZero)
return (*C.wchar_t)(unsafe.Pointer(&runes[0]))
}