-
Notifications
You must be signed in to change notification settings - Fork 8
/
pcre2.go
562 lines (484 loc) · 11.1 KB
/
pcre2.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
Package pcre2 is a wrapper around PCRE2 C library. This library aims to
provide compatible API as that of regexp package from Go stdlib.
Note that while PCRE2 provides support for 8, 16, and 32 bit inputs,
this library assumes UTF-8 (32bit) input. Therefore if you use anything
other than UTF-8, matches will not succeed.
*/
package pcre2
/*
#define PCRE2_CODE_UNIT_WIDTH 32
#cgo pkg-config: libpcre2-32
#include <stdio.h>
#include <stdlib.h>
#include <pcre2.h>
#define MY_PCRE2_ERROR_MESSAGE_BUF_LEN 256
static
void *
MY_pcre2_get_error_message(int errnum) {
PCRE2_UCHAR *buf = (PCRE2_UCHAR *) malloc(sizeof(PCRE2_UCHAR) * MY_PCRE2_ERROR_MESSAGE_BUF_LEN);
pcre2_get_error_message(errnum, buf, MY_PCRE2_ERROR_MESSAGE_BUF_LEN);
return buf;
}
*/
import "C"
import (
"fmt"
"reflect"
"unicode/utf8"
"unsafe"
)
// Error returns the string representation of the error.
func (e ErrCompile) Error() string {
return fmt.Sprintf("PCRE2 compilation failed at offset %d: %s", e.offset, e.message)
}
func strToRuneArray(s string) ([]rune, []int, error) {
rs := []rune{}
ls := []int{} // length of each rune
for len(s) > 0 {
r, n := utf8.DecodeRuneInString(s)
if r == utf8.RuneError {
return nil, nil, ErrInvalidUTF8String
}
s = s[n:]
rs = append(rs, r)
ls = append(ls, n)
}
return rs, ls, nil
}
func bytesToRuneArray(b []byte) ([]rune, []int, error) {
rs := []rune{} // actual runes
ls := []int{} // length of each rune
for len(b) > 0 {
r, n := utf8.DecodeRune(b)
if r == utf8.RuneError {
return nil, nil, ErrInvalidUTF8String
}
b = b[n:]
rs = append(rs, r)
ls = append(ls, n)
}
return rs, ls, nil
}
// Compile takes the input string and creates a compiled Regexp object.
// Regexp objects created by Compile must be released by calling Free
func Compile(pattern string) (*Regexp, error) {
patc, _, err := strToRuneArray(pattern)
if err != nil {
return nil, err
}
var errnum C.int
var erroff C.PCRE2_SIZE
re := C.pcre2_compile(
(C.PCRE2_SPTR)(unsafe.Pointer(&patc[0])),
C.size_t(len(patc)),
0,
&errnum,
&erroff,
nil,
)
if re == nil {
rawbytes := C.MY_pcre2_get_error_message(errnum)
msg := C.GoBytes(rawbytes, 32/8*256)
defer C.free(unsafe.Pointer(rawbytes))
return nil, ErrCompile{
pattern: pattern,
offset: int(erroff),
message: string(msg),
}
}
return &Regexp{
pattern: pattern,
ptr: uintptr(unsafe.Pointer(re)),
}, nil
}
// MustCompile is like Compile but panics if the expression cannot be
// parsed.
func MustCompile(pattern string) *Regexp {
r, err := Compile(pattern)
if err != nil {
panic(err)
}
return r
}
func (r *Regexp) validRegexpPtr() (*C.pcre2_code, error) {
if r == nil {
return nil, ErrInvalidRegexp
}
if rptr := r.ptr; rptr != 0 {
return (*C.pcre2_code)(unsafe.Pointer(rptr)), nil
}
return nil, ErrInvalidRegexp
}
// Free releases the underlying C resources
func (r *Regexp) Free() error {
rptr, err := r.validRegexpPtr()
if err != nil {
return err
}
C.pcre2_code_free(rptr)
r.ptr = 0
return nil
}
// String returns the source text used to compile the regular expression.
func (r Regexp) String() string {
return r.pattern
}
func (r *Regexp) Match(b []byte) bool {
rs, _, err := bytesToRuneArray(b)
if err != nil {
return false
}
return r.matchRuneArray(rs, 0, 0, nil) >= 0
}
func (r *Regexp) MatchString(s string) bool {
rs, _, err := strToRuneArray(s)
if err != nil {
return false
}
return r.matchRuneArray(rs, 0, 0, nil) >= 0
}
func (r *Regexp) matchRuneArray(rs []rune, offset int, options int, matchData *C.pcre2_match_data) int {
rptr, err := r.validRegexpPtr()
if err != nil {
return -1
}
if matchData == nil {
matchData = C.pcre2_match_data_create_from_pattern(rptr, nil)
defer C.pcre2_match_data_free(matchData)
}
rc := C.pcre2_match(
rptr,
(C.PCRE2_SPTR)(unsafe.Pointer(&rs[0])),
C.size_t(len(rs)),
(C.PCRE2_SIZE)(offset),
(C.uint32_t)(options),
matchData,
nil,
)
return int(rc)
}
func pcre2GetOvectorPointer(matchData *C.pcre2_match_data, howmany int) []C.size_t {
ovector := C.pcre2_get_ovector_pointer(matchData)
// Note that by doing this SliceHeader maigc, we allow Go
// slice syntax but Go doesn't own the underlying pointer.
// We need to free it. In this case, it means the caller
// must remember to free matchData
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(ovector)),
Len: howmany * 2,
Cap: howmany * 2,
}
return *(*[]C.size_t)(unsafe.Pointer(&hdr))
}
func (r *Regexp) HasOption(opt int) bool {
rptr, err := r.validRegexpPtr()
if err != nil {
return false
}
var i C.uint32_t
C.pcre2_pattern_info(rptr, C.PCRE2_INFO_ALLOPTIONS, unsafe.Pointer(&i))
return (uint32(i) & uint32(opt)) != 0
}
func (r *Regexp) isCRLFValid() bool {
rptr, err := r.validRegexpPtr()
if err != nil {
return false
}
var i C.uint32_t
C.pcre2_pattern_info(rptr, C.PCRE2_INFO_NEWLINE, unsafe.Pointer(&i))
switch i {
case C.PCRE2_NEWLINE_ANY, C.PCRE2_NEWLINE_CRLF, C.PCRE2_NEWLINE_ANYCRLF:
return true
}
return false
}
func (r *Regexp) FindIndex(b []byte) []int {
rs, ls, err := bytesToRuneArray(b)
if err != nil {
return nil
}
is := r.findAllIndex(rs, ls, 1)
if len(is) != 1 {
return nil
}
return is[0]
}
func (r *Regexp) Find(b []byte) []byte {
is := r.FindIndex(b)
if is == nil {
return nil
}
return b[is[0]:is[1]]
}
func (r *Regexp) FindStringIndex(s string) []int {
rs, ls, err := strToRuneArray(s)
if err != nil {
return nil
}
is := r.findAllIndex(rs, ls, 1)
if len(is) != 1 {
return nil
}
return is[0]
}
func (r *Regexp) FindSubmatch(b []byte) [][]byte {
matches := r.FindSubmatchIndex(b)
if matches == nil {
return nil
}
ret := make([][]byte, 0, len(matches)/2)
for i := 0; i < len(matches)/2; i++ {
ret = append(ret, b[matches[2*i]:matches[2*i+1]])
}
return ret
}
func (r *Regexp) FindSubmatchIndex(b []byte) []int {
rs, ls, err := bytesToRuneArray(b)
if err != nil {
return nil
}
return r.findSubmatchIndex(rs, ls)
}
func (r *Regexp) FindStringSubmatchIndex(s string) []int {
rs, ls, err := strToRuneArray(s)
if err != nil {
return nil
}
return r.findSubmatchIndex(rs, ls)
}
func (r *Regexp) findSubmatchIndex(rs []rune, ls []int) []int {
rptr, err := r.validRegexpPtr()
if err != nil {
return nil
}
matchData := C.pcre2_match_data_create_from_pattern(rptr, nil)
defer C.pcre2_match_data_free(matchData)
out := []int(nil)
options := 0
count := r.matchRuneArray(rs, 0, options, matchData)
if count <= 0 {
return nil
}
ovector := pcre2GetOvectorPointer(matchData, count)
for i := 0; i < count; i++ {
ovec0 := int(ovector[2*i])
b1 := 0
for x := 0; x < ovec0; x++ {
b1 += ls[x]
}
ovec1 := int(ovector[2*i+1])
b2 := b1
for x := ovec0; x < ovec1; x++ {
b2 += ls[x]
}
out = append(out, []int{b1, b2}...)
}
return out
}
func (r *Regexp) FindStringSubmatch(s string) []string {
matches := r.FindStringSubmatchIndex(s)
if matches == nil {
return nil
}
ret := make([]string, 0, len(matches))
for i := 0; i < len(matches)/2; i++ {
ret = append(ret, s[matches[2*i]:matches[2*i+1]])
}
return ret
}
func (r *Regexp) FindString(s string) string {
is := r.FindStringIndex(s)
if is == nil {
return ""
}
return s[is[0]:is[1]]
}
func (r *Regexp) FindAll(b []byte, n int) [][]byte {
rs, ls, err := bytesToRuneArray(b)
if err != nil {
return nil
}
ret := [][]byte(nil)
for _, is := range r.findAllIndex(rs, ls, n) {
ret = append(ret, b[is[0]:is[1]])
}
return ret
}
func (r *Regexp) FindAllString(s string, n int) []string {
if n == 0 {
return nil
}
rs, ls, err := strToRuneArray(s)
if err != nil {
return nil
}
ret := []string{}
for _, is := range r.findAllIndex(rs, ls, n) {
ret = append(ret, s[is[0]:is[1]])
if n > 0 && len(ret) >= n {
break
}
}
return ret
}
func (r *Regexp) findAllIndex(rs []rune, ls []int, n int) [][]int {
if n == 0 {
return nil
}
rptr, err := r.validRegexpPtr()
if err != nil {
return nil
}
matchData := C.pcre2_match_data_create_from_pattern(rptr, nil)
defer C.pcre2_match_data_free(matchData)
out := [][]int(nil)
offset := 0
options := 0
for len(rs) > 0 {
count := r.matchRuneArray(rs, 0, options, matchData)
if count <= 0 {
break
}
ovector := pcre2GetOvectorPointer(matchData, count)
ovec0 := int(ovector[0])
b1 := 0
for x := 0; x < ovec0; x++ {
b1 += ls[x]
}
b2 := b1
for x := ovec0; x < int(ovector[1]); x++ {
b2 += ls[x]
}
out = append(out, []int{offset + b1, offset + b2})
units := int(ovector[1])
for x := 0; x < units; x++ {
offset += ls[x]
}
rs = rs[units:]
ls = ls[units:]
if n > 0 && len(out) >= n {
break
}
}
return out
}
func (r *Regexp) FindAllIndex(b []byte, n int) [][]int {
rs, ls, err := bytesToRuneArray(b)
if err != nil {
return nil
}
return r.findAllIndex(rs, ls, n)
}
func (r *Regexp) FindAllStringIndex(s string, n int) [][]int {
rs, ls, err := strToRuneArray(s)
if err != nil {
return nil
}
return r.findAllIndex(rs, ls, n)
}
func (r *Regexp) findAllSubmatchIndex(rs []rune, ls []int, n int) [][]int {
if n == 0 {
return nil
}
rptr, err := r.validRegexpPtr()
if err != nil {
return nil
}
matchData := C.pcre2_match_data_create_from_pattern(rptr, nil)
defer C.pcre2_match_data_free(matchData)
out := [][]int(nil)
offset := 0
options := 0
for len(rs) > 0 {
count := r.matchRuneArray(rs, 0, options, matchData)
if count <= 0 {
break
}
ovector := pcre2GetOvectorPointer(matchData, count)
curmatch := make([]int, 0, count)
for i := 0; i < count; i++ {
ovec2i := int(ovector[2*i])
b1 := 0
for x := 0; x < ovec2i; x++ {
b1 += ls[x]
}
b2 := b1
for x := ovec2i; x < int(ovector[2*i+1]); x++ {
b2 += ls[x]
}
curmatch = append(curmatch, offset+b1, offset+b2)
}
out = append(out, curmatch)
units := int(ovector[1])
for x := 0; x < units; x++ {
offset += ls[x]
}
rs = rs[units:]
ls = ls[units:]
if n > 0 && len(out) >= n {
break
}
}
return out
}
func (r *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte {
rs, ls, err := bytesToRuneArray(b)
if err != nil {
return nil
}
all := r.findAllSubmatchIndex(rs, ls, n)
if all == nil {
return nil
}
ret := make([][][]byte, 0, len(all))
for _, is := range all {
l := len(is) / 2
cur := make([][]byte, 0, l)
for i := 0; i < l; i++ {
cur = append(cur, b[is[2*i]:is[2*i+1]])
}
ret = append(ret, cur)
if n > 0 && len(ret) >= n {
break
}
}
return ret
}
func (r *Regexp) FindAllStringSubmatch(s string, n int) [][]string {
rs, ls, err := strToRuneArray(s)
if err != nil {
return nil
}
all := r.findAllSubmatchIndex(rs, ls, n)
if all == nil {
return nil
}
ret := make([][]string, 0, len(all))
for _, is := range all {
l := len(is) / 2
cur := make([]string, 0, l)
for i := 0; i < l; i++ {
cur = append(cur, s[is[2*i]:is[2*i+1]])
}
ret = append(ret, cur)
if n > 0 && len(ret) >= n {
break
}
}
return ret
}
func (r *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int {
rs, ls, err := bytesToRuneArray(b)
if err != nil {
return nil
}
return r.findAllSubmatchIndex(rs, ls, n)
}
func (r *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int {
rs, ls, err := strToRuneArray(s)
if err != nil {
return nil
}
return r.findAllSubmatchIndex(rs, ls, n)
}