-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbindings.go
315 lines (278 loc) · 7.18 KB
/
bindings.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package onig
// #cgo pkg-config: oniguruma
// #include <bindings.h>
import "C"
import (
"reflect"
"runtime"
"unsafe"
)
// NOTE WELL: This is written against Oniguruma 5.9.6. At the time of writing,
// latest master contains some API changes that may impact the behavior of
// these bindings.
//
// This is the only file in this package that is allowed to import "C". All
// other files must access C objects via unexported Go symbols defined in this
// file. No references to "C" may be visible in the package godoc.
type errorInfo [C.sizeof_OnigErrorInfo]byte
const (
regexSizeof = C.sizeof_regex_t
regionSizeof = C.sizeof_OnigRegion
)
// For error_test.go only
const errCodeEmptyCharClass = C.ONIGERR_EMPTY_CHAR_CLASS
const (
optIgnoreCase CompileOptions = C.ONIG_OPTION_IGNORECASE
optExtend CompileOptions = C.ONIG_OPTION_EXTEND
optMultiline CompileOptions = C.ONIG_OPTION_MULTILINE
optSingleline CompileOptions = C.ONIG_OPTION_SINGLELINE
optFindLongest CompileOptions = C.ONIG_OPTION_FIND_LONGEST
optFindNotEmpty CompileOptions = C.ONIG_OPTION_FIND_NOT_EMPTY
optNegateSingleline CompileOptions = C.ONIG_OPTION_NEGATE_SINGLELINE
optDontCaptureGroup CompileOptions = C.ONIG_OPTION_DONT_CAPTURE_GROUP
optCaptureGroup CompileOptions = C.ONIG_OPTION_CAPTURE_GROUP
optNotBOL MatchOptions = C.ONIG_OPTION_NOTBOL
optNotEOL MatchOptions = C.ONIG_OPTION_NOTEOL
)
type nameTableEntry struct {
Name string
Num int
}
func init() {
C.onig_init()
SyntaxAsIs = Syntax(unsafe.Pointer(&C.OnigSyntaxASIS))
SyntaxPosixBasic = Syntax(unsafe.Pointer(&C.OnigSyntaxPosixBasic))
SyntaxPosixExtended = Syntax(unsafe.Pointer(&C.OnigSyntaxPosixExtended))
SyntaxEmacs = Syntax(unsafe.Pointer(&C.OnigSyntaxEmacs))
SyntaxGrep = Syntax(unsafe.Pointer(&C.OnigSyntaxGrep))
SyntaxGNU = Syntax(unsafe.Pointer(&C.OnigSyntaxGnuRegex))
SyntaxJava = Syntax(unsafe.Pointer(&C.OnigSyntaxJava))
SyntaxPerl = Syntax(unsafe.Pointer(&C.OnigSyntaxPerl))
SyntaxPerlNG = Syntax(unsafe.Pointer(&C.OnigSyntaxPerl_NG))
SyntaxRuby = Syntax(unsafe.Pointer(&C.OnigSyntaxRuby))
}
func errStr(code int, info *errorInfo) string {
buf := make([]byte, C.ONIG_MAX_ERROR_MESSAGE_LEN)
l := C.goonig_error_code_to_str((*C.OnigUChar)(unsafe.Pointer(&buf[0])), C.int(code), info.cPtr())
buf = buf[:l]
return string(buf)
}
func regexInit(r *Regex, pattern string, options CompileOptions, syntax Syntax) error {
patternC := C.CString(pattern)
var errInfo errorInfo
errInfoPtr := &errInfo
errCode := C.goonig_init_regex(
r.cPtr(),
patternC,
C.int(len(pattern)),
options.cVal(),
syntax.cPtr(),
errInfoPtr.cPtr(),
)
if errCode != 0 {
return onigError{
code: int(errCode),
info: errInfoPtr,
}
}
runtime.SetFinalizer(r, func(r *Regex) {
// Free any ancillary objects associated with the regex.
C.goonig_free_regex(r.cPtr())
})
return nil
}
func regexMatch(r *Regex, s string, options MatchOptions, m *Match) bool {
sC := C.CString(s)
result := C.goonig_regex_match(
r.cPtr(),
sC,
C.int(len(s)),
m.cPtr(),
options.cVal(),
)
return result >= 0
}
func regexMatchBytes(r *Regex, b []byte, options MatchOptions, m *Match) bool {
sC := (*C.char)(unsafe.Pointer(&b[0]))
result := C.goonig_regex_match(
r.cPtr(),
sC,
C.int(len(b)),
m.cPtr(),
options.cVal(),
)
return result >= 0
}
func regexSearch(r *Regex, s string, options MatchOptions, rev bool, m *Match) bool {
sC := C.CString(s)
revC := C.int(0)
if rev {
revC = C.int(1)
}
result := C.goonig_regex_search(
r.cPtr(),
sC,
C.int(len(s)),
revC,
m.cPtr(),
options.cVal(),
)
return result >= 0
}
func regexSearchBytes(r *Regex, b []byte, options MatchOptions, rev bool, m *Match) bool {
sC := (*C.char)(unsafe.Pointer(&b[0]))
revC := C.int(0)
if rev {
revC = C.int(1)
}
result := C.goonig_regex_search(
r.cPtr(),
sC,
C.int(len(b)),
revC,
m.cPtr(),
options.cVal(),
)
return result >= 0
}
func regexCaptureCount(r *Regex) int {
result := C.goonig_regex_capture_count(r.cPtr())
return int(result)
}
func regexNameTable(r *Regex) []nameTableEntry {
l := regexCaptureCount(r)
if l == 0 {
return nil
}
table := make([]C.goonig_name_table_entry, l)
realLen := C.goonig_regex_name_table(r.cPtr(), &table[0])
if realLen == 0 {
return nil
}
ret := make([]nameTableEntry, realLen)
for i, raw := range table {
nameLen := int(raw.len)
nameBytes := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
uintptr(unsafe.Pointer(raw.start)), nameLen, nameLen,
}))
ret[i] = nameTableEntry{
Name: string(nameBytes),
Num: int(raw.idx),
}
}
return ret
}
func matchInit(m *Match) {
C.goonig_init_region(m.cPtr())
runtime.SetFinalizer(m, func(m *Match) {
// Free any buffers associated with the match.
C.goonig_free_region(m.cPtr())
})
}
func matchInitFake(m *Match, spans []Span) error {
matchInit(m)
c := m.cPtr()
l := len(spans)
errCode := C.goonig_region_resize(c, C.int(l))
if errCode != 0 {
return onigError{
code: int(errCode),
}
}
begs := *(*[]C.int)(unsafe.Pointer(&reflect.SliceHeader{
uintptr(unsafe.Pointer(c.beg)), l, l,
}))
ends := *(*[]C.int)(unsafe.Pointer(&reflect.SliceHeader{
uintptr(unsafe.Pointer(c.end)), l, l,
}))
for i, span := range spans {
begs[i] = C.int(span.Start)
ends[i] = C.int(span.End)
}
return nil
}
func matchCaptureCount(m *Match) int {
c := m.cPtr()
return int(c.num_regs) - 1
}
func matchCapture(m *Match, idx int) Span {
max := matchCaptureCount(m)
if idx > max || idx < 0 {
panic("capture index out of range")
}
c := m.cPtr()
begPU := uintptr(unsafe.Pointer(c.beg))
endPU := uintptr(unsafe.Pointer(c.end))
begPU += uintptr(idx)
endPU += uintptr(idx)
begP := (*C.int)(unsafe.Pointer(begPU))
endP := (*C.int)(unsafe.Pointer(endPU))
return Span{
Start: int(*begP),
End: int(*endP),
}
}
func matchEqual(a *Match, b *Match) bool {
if (a == nil) != (b == nil) {
return false
}
if a == nil {
return true
}
aC := a.cPtr()
bC := b.cPtr()
if aC.num_regs != bC.num_regs {
return false
}
num := int(aC.num_regs)
aBegs := *(*[]int)(unsafe.Pointer(&reflect.SliceHeader{
uintptr(unsafe.Pointer(aC.beg)), num, num,
}))
bBegs := *(*[]int)(unsafe.Pointer(&reflect.SliceHeader{
uintptr(unsafe.Pointer(bC.beg)), num, num,
}))
aEnds := *(*[]int)(unsafe.Pointer(&reflect.SliceHeader{
uintptr(unsafe.Pointer(aC.end)), num, num,
}))
bEnds := *(*[]int)(unsafe.Pointer(&reflect.SliceHeader{
uintptr(unsafe.Pointer(bC.end)), num, num,
}))
for i := range aBegs {
if aBegs[i] != bBegs[i] {
return false
}
}
for i := range aEnds {
if aEnds[i] != bEnds[i] {
return false
}
}
return true
}
func (r *Regex) cPtr() *C.regex_t {
if r == nil {
return nil
}
return (*C.regex_t)(unsafe.Pointer(&r.c[0]))
}
func (m *Match) cPtr() *C.OnigRegion {
if m == nil {
return nil
}
return (*C.OnigRegion)(unsafe.Pointer(&m.c[0]))
}
func (r *errorInfo) cPtr() *C.OnigErrorInfo {
if r == nil {
return nil
}
return (*C.OnigErrorInfo)(unsafe.Pointer(&r[0]))
}
func (s Syntax) cPtr() *C.OnigSyntaxType {
return (*C.OnigSyntaxType)(unsafe.Pointer(uintptr(s)))
}
func (o CompileOptions) cVal() C.OnigOptionType {
return C.OnigOptionType(o)
}
func (o MatchOptions) cVal() C.OnigOptionType {
return C.OnigOptionType(o)
}