forked from karalabe/usb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wchar.go
227 lines (209 loc) · 4.67 KB
/
wchar.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// This file is https://github.com/orofarne/gowchar/blob/master/gowchar.go
//
// It was vendored inline to work around CGO limitations that don't allow C types
// to directly cross package API boundaries.
//
// The vendored file is licensed under the 3-clause BSD license, according to:
// https://github.com/orofarne/gowchar/blob/master/LICENSE
// +build !ios
// +build freebsd linux darwin windows
package usb
/*
#include <wchar.h>
const size_t SIZEOF_WCHAR_T = sizeof(wchar_t);
void gowchar_set (wchar_t *arr, int pos, wchar_t val)
{
arr[pos] = val;
}
wchar_t gowchar_get (wchar_t *arr, int pos)
{
return arr[pos];
}
*/
import "C"
import (
"fmt"
"unicode/utf16"
"unicode/utf8"
)
var sizeofWcharT C.size_t = C.size_t(C.SIZEOF_WCHAR_T)
func stringToWcharT(s string) (*C.wchar_t, C.size_t) {
switch sizeofWcharT {
case 2:
return stringToWchar2(s) // Windows
case 4:
return stringToWchar4(s) // Unix
default:
panic(fmt.Sprintf("Invalid sizeof(wchar_t) = %v", sizeofWcharT))
}
}
func wcharTToString(s *C.wchar_t) (string, error) {
switch sizeofWcharT {
case 2:
return wchar2ToString(s) // Windows
case 4:
return wchar4ToString(s) // Unix
default:
panic(fmt.Sprintf("Invalid sizeof(wchar_t) = %v", sizeofWcharT))
}
}
func wcharTNToString(s *C.wchar_t, size C.size_t) (string, error) {
switch sizeofWcharT {
case 2:
return wchar2NToString(s, size) // Windows
case 4:
return wchar4NToString(s, size) // Unix
default:
panic(fmt.Sprintf("Invalid sizeof(wchar_t) = %v", sizeofWcharT))
}
}
// Windows
func stringToWchar2(s string) (*C.wchar_t, C.size_t) {
var slen int
s1 := s
for len(s1) > 0 {
r, size := utf8.DecodeRuneInString(s1)
if er, _ := utf16.EncodeRune(r); er == '\uFFFD' {
slen += 1
} else {
slen += 2
}
s1 = s1[size:]
}
slen++ // \0
res := C.malloc(C.size_t(slen) * sizeofWcharT)
var i int
for len(s) > 0 {
r, size := utf8.DecodeRuneInString(s)
if r1, r2 := utf16.EncodeRune(r); r1 != '\uFFFD' {
C.gowchar_set((*C.wchar_t)(res), C.int(i), C.wchar_t(r1))
i++
C.gowchar_set((*C.wchar_t)(res), C.int(i), C.wchar_t(r2))
i++
} else {
C.gowchar_set((*C.wchar_t)(res), C.int(i), C.wchar_t(r))
i++
}
s = s[size:]
}
C.gowchar_set((*C.wchar_t)(res), C.int(slen-1), C.wchar_t(0)) // \0
return (*C.wchar_t)(res), C.size_t(slen)
}
// Unix
func stringToWchar4(s string) (*C.wchar_t, C.size_t) {
slen := utf8.RuneCountInString(s)
slen++ // \0
res := C.malloc(C.size_t(slen) * sizeofWcharT)
var i int
for len(s) > 0 {
r, size := utf8.DecodeRuneInString(s)
C.gowchar_set((*C.wchar_t)(res), C.int(i), C.wchar_t(r))
s = s[size:]
i++
}
C.gowchar_set((*C.wchar_t)(res), C.int(slen-1), C.wchar_t(0)) // \0
return (*C.wchar_t)(res), C.size_t(slen)
}
// Windows
func wchar2ToString(s *C.wchar_t) (string, error) {
var i int
var res string
for {
ch := C.gowchar_get(s, C.int(i))
if ch == 0 {
break
}
r := rune(ch)
i++
if !utf16.IsSurrogate(r) {
if !utf8.ValidRune(r) {
err := fmt.Errorf("Invalid rune at position %v", i)
return "", err
}
res += string(r)
} else {
ch2 := C.gowchar_get(s, C.int(i))
r2 := rune(ch2)
r12 := utf16.DecodeRune(r, r2)
if r12 == '\uFFFD' {
err := fmt.Errorf("Invalid surrogate pair at position %v", i-1)
return "", err
}
res += string(r12)
i++
}
}
return res, nil
}
// Unix
func wchar4ToString(s *C.wchar_t) (string, error) {
var i int
var res string
for {
ch := C.gowchar_get(s, C.int(i))
if ch == 0 {
break
}
r := rune(ch)
if !utf8.ValidRune(r) {
err := fmt.Errorf("Invalid rune at position %v", i)
return "", err
}
res += string(r)
i++
}
return res, nil
}
// Windows
func wchar2NToString(s *C.wchar_t, size C.size_t) (string, error) {
var i int
var res string
N := int(size)
for i < N {
ch := C.gowchar_get(s, C.int(i))
if ch == 0 {
break
}
r := rune(ch)
i++
if !utf16.IsSurrogate(r) {
if !utf8.ValidRune(r) {
err := fmt.Errorf("Invalid rune at position %v", i)
return "", err
}
res += string(r)
} else {
if i >= N {
err := fmt.Errorf("Invalid surrogate pair at position %v", i-1)
return "", err
}
ch2 := C.gowchar_get(s, C.int(i))
r2 := rune(ch2)
r12 := utf16.DecodeRune(r, r2)
if r12 == '\uFFFD' {
err := fmt.Errorf("Invalid surrogate pair at position %v", i-1)
return "", err
}
res += string(r12)
i++
}
}
return res, nil
}
// Unix
func wchar4NToString(s *C.wchar_t, size C.size_t) (string, error) {
var i int
var res string
N := int(size)
for i < N {
ch := C.gowchar_get(s, C.int(i))
r := rune(ch)
if !utf8.ValidRune(r) {
err := fmt.Errorf("Invalid rune at position %v", i)
return "", err
}
res += string(r)
i++
}
return res, nil
}