-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregexp.go
335 lines (285 loc) · 7.93 KB
/
regexp.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package onigmo
/*
#cgo CFLAGS: -I/usr/local/include
#cgo LDFLAGS: -L/usr/local/lib -lonigmo
#include <stdlib.h>
#include <string.h>
#include <onigmo.h>
// cgo does not support vargs
extern int onigmo_helper_error_code_with_info_to_str(UChar* err_buf, int err_code, OnigErrorInfo *errInfo);
extern int onigmo_helper_error_code_to_str(UChar* err_buf, OnigPosition err_code);
extern char* onigmo_helper_get(char* str, OnigPosition* beg, OnigPosition* end, int n);
int onigmo_helper_error_code_with_info_to_str(UChar* err_buf, int err_code, OnigErrorInfo *errInfo) {
return onig_error_code_to_str(err_buf, err_code, errInfo);
}
int onigmo_helper_error_code_to_str(UChar* err_buf, OnigPosition err_code) {
return onig_error_code_to_str(err_buf, err_code);
}
char* onigmo_helper_get(char* str, OnigPosition* beg, OnigPosition* end, int n) {
char *res = strndup((char*)(str + beg[n]), end[n] - beg[n]);
return res;
}
*/
import "C"
import (
"errors"
"fmt"
"strconv"
"sync"
"unsafe"
)
var (
// OnigEncodingUTF8 for onigmo encoding
OnigEncodingUTF8 = &C.OnigEncodingUTF_8
// OnigEncodingASCII for onigmo encoding
OnigEncodingASCII = &C.OnigEncodingASCII
// OnigSyntaxPerl for onigmo syntax
OnigSyntaxPerl = &C.OnigSyntaxPerl
// OnigSyntaxPOSIXBasic for onigmo syntax
OnigSyntaxPOSIXBasic = &C.OnigSyntaxPosixBasic
// OnigSyntaxPOSIXExtended for onigmo syntax
OnigSyntaxPOSIXExtended = &C.OnigSyntaxPosixExtended
)
// Regexp struct for onigmo
type Regexp struct {
encoding C.OnigEncoding
regex C.OnigRegex
mu sync.Mutex
expr string
matchResult *MatchResult
}
// NamedGroupNums for named group regexp
type NamedGroupNums map[string][]C.int
// MatchResult struct for regexp result
type MatchResult struct {
regex C.OnigRegex
matched bool
input string
region *C.OnigRegion
namedGroupNums NamedGroupNums
errorMessage string
}
// Version returns version
func Version() string {
return C.GoString(C.onig_version())
}
// NewRegexp returns Regexp
func NewRegexp(expr string) (*Regexp, error) {
ret := C.onig_init()
if ret != 0 {
return nil, errors.New("failed to initialize encoding for the Onigumo regular expression library")
}
re := &Regexp{
encoding: OnigEncodingUTF8,
expr: expr,
}
re.mu.Lock()
defer re.mu.Unlock()
beginning, end := stringPointers(expr)
defer free(beginning, end)
var errorInfo C.OnigErrorInfo
r := C.onig_new(&re.regex, beginning, end, C.ONIG_OPTION_DEFAULT, re.encoding, C.ONIG_SYNTAX_DEFAULT, &errorInfo)
if r != C.ONIG_NORMAL {
return nil, errors.New(errMsgWithInfo(r, &errorInfo))
}
return re, nil
}
// Compile returns Regexp
func Compile(expr string) (*Regexp, error) {
return NewRegexp(expr)
}
// MustCompile returns Regexp
func MustCompile(expr string) *Regexp {
regexp, error := Compile(expr)
if error != nil {
panic(`regexp: Compile(` + quote(expr) + `): ` + error.Error())
}
return regexp
}
// Match checks by pattern
func Match(pattern string, b []byte) bool {
re, err := Compile(pattern)
if err != nil {
return false
}
return re.match(b)
}
// MatchString checks by pattern
func MatchString(pattern string, s string) bool {
re, err := Compile(pattern)
if err != nil {
return false
}
return re.MatchString(s)
}
// HasCaptureGroup checks have
func (m *MatchResult) HasCaptureGroup(name string) bool {
_, err := m.getNamedGroupNums(name)
return err == nil
}
func (re *Regexp) match(b []byte) bool {
region := C.onig_region_new()
beginning, end := bytePointers(b)
defer free(beginning, end)
input := string(b)
r := C.onig_match(re.regex, beginning, end, beginning, region, C.ONIG_OPTION_NONE)
if r == C.ONIG_MISMATCH {
C.onig_region_free(region, 1)
re.matchResult = &MatchResult{
matched: false,
input: input,
namedGroupNums: make(map[string][]C.int),
}
return false
} else if r < 0 {
C.onig_region_free(region, 1)
re.matchResult = &MatchResult{
matched: false,
input: input,
namedGroupNums: make(map[string][]C.int),
errorMessage: errMsg(r),
}
return false
} else {
re.matchResult = &MatchResult{
matched: true,
region: region,
input: input,
regex: re.regex,
namedGroupNums: make(map[string][]C.int),
}
return true
}
}
// MatchString checks words
func (re *Regexp) MatchString(s string) bool {
b := []byte(s)
return re.match(b)
}
func (re *Regexp) search(b []byte) bool {
region := C.onig_region_new()
beginning, end := bytePointers(b)
searchBeginning := beginning
searchEnd := end
defer free(beginning, end)
input := string(b)
r := C.onig_search(re.regex, beginning, end, searchBeginning, searchEnd, region, C.ONIG_OPTION_NONE)
if r == C.ONIG_MISMATCH {
C.onig_region_free(region, 1)
re.matchResult = &MatchResult{
matched: false,
input: input,
namedGroupNums: make(map[string][]C.int),
}
return false
} else if r < 0 {
C.onig_region_free(region, 1)
re.matchResult = &MatchResult{
matched: false,
input: input,
namedGroupNums: make(map[string][]C.int),
errorMessage: errMsg(r),
}
return false
} else {
re.matchResult = &MatchResult{
matched: true,
region: region,
input: input,
regex: re.regex,
namedGroupNums: make(map[string][]C.int),
}
return true
}
}
// SearchString checks words
func (re *Regexp) SearchString(s string) bool {
b := []byte(s)
return re.search(b)
}
func (m *MatchResult) getNamedGroupNums(s string) ([]C.int, error) {
cached, ok := m.namedGroupNums[s]
if ok {
return cached, nil
}
beginning, end := stringPointers(s)
defer free(beginning, end)
var groupNums *C.int
n := C.onig_name_to_group_numbers(m.regex, beginning, end, &groupNums)
if n <= 0 {
return nil, fmt.Errorf("%v: no such capture group in pattern", s)
}
result := make([]C.int, 0, int(n))
for i := 0; i < int(n); i++ {
result = append(result, getPos(groupNums, C.int(i)))
}
m.namedGroupNums[s] = result
return result, nil
}
// Get returns matched words
func (m *MatchResult) Get(s string) (string, error) {
if !m.matched {
return "", nil
}
groupNums, err := m.getNamedGroupNums(s)
if err != nil {
return "", err
}
for _, groupNum := range groupNums {
w := C.onigmo_helper_get(C.CString(m.input), m.region.beg, m.region.end, groupNum)
word := C.GoString(w)
if word != "" {
return word, nil
}
}
return "", nil
}
// Free for Regexp
func (re *Regexp) Free() {
C.onig_free(re.regex)
}
// Free for MatchResult
func (m *MatchResult) Free() {
if m.matched {
C.onig_region_free(m.region, 1)
}
}
func quote(s string) string {
if strconv.CanBackquote(s) {
return "`" + s + "`"
}
return strconv.Quote(s)
}
func stringPointers(s string) (beginning, end *C.OnigUChar) {
beginning = (*C.OnigUChar)(unsafe.Pointer(C.CString(s)))
end = (*C.OnigUChar)(unsafe.Pointer(uintptr(unsafe.Pointer(beginning)) + uintptr(len(s))))
return
}
func bytePointers(b []byte) (beginning, end *C.OnigUChar) {
beginning = (*C.OnigUChar)(C.CBytes(b))
end = (*C.OnigUChar)(unsafe.Pointer(uintptr(unsafe.Pointer(beginning)) + uintptr(len(b))))
return
}
func getPos(p *C.int, i C.int) C.int {
return *(*C.int)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + uintptr(i)*unsafe.Sizeof(C.int(0))))
}
func free(beginning *C.OnigUChar, end *C.OnigUChar) {
C.memset(unsafe.Pointer(beginning), C.int(0), C.size_t(uintptr(unsafe.Pointer(end))-uintptr(unsafe.Pointer(beginning))))
C.free(unsafe.Pointer(beginning))
}
func errMsgWithInfo(returnCode C.int, errorInfo *C.OnigErrorInfo) string {
msg := make([]byte, C.ONIG_MAX_ERROR_MESSAGE_LEN)
l := C.onigmo_helper_error_code_with_info_to_str((*C.UChar)(&msg[0]), returnCode, errorInfo)
if l <= 0 {
return "unknown error"
}
return string(msg[:l])
}
func errMsg(returnCode C.OnigPosition) string {
msg := make([]byte, C.ONIG_MAX_ERROR_MESSAGE_LEN)
l := C.onigmo_helper_error_code_to_str((*C.UChar)(&msg[0]), returnCode)
if l <= 0 {
return "unknown error"
}
return string(msg[:l])
}