forked from mathpl/golang-pkg-pcre
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcre.go
517 lines (468 loc) · 15.6 KB
/
pcre.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
// Copyright (c) 2011 Florian Weimer. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// This package provides access to the Perl Compatible Regular
// Expresion library, PCRE.
//
// It implements two main types, Regexp and Matcher. Regexp objects
// store a compiled regular expression. They are immutable.
// Compilation of regular expressions using Compile or MustCompile is
// slightly expensive, so these objects should be kept and reused,
// instead of compiling them from scratch for each matching attempt.
//
// Matcher objects keeps the results of a match against a []byte or
// string subject. The Group and GroupString functions provide access
// to capture groups; both versions work no matter if the subject was a
// []byte or string, but the version with the matching type is slightly
// more efficient.
//
// Matcher objects contain some temporary space and refer the original
// subject. They are mutable and can be reused (using Match,
// MatchString, Reset or ResetString).
//
// For details on the regular expression language implemented by this
// package and the flags defined below, see the PCRE documentation.
package pcre
/*
#cgo LDFLAGS: -lpcre
#cgo CFLAGS: -I/opt/local/include
#include <pcre.h>
#include <string.h>
*/
import "C"
import (
"fmt"
"strconv"
"unsafe"
)
// Flags for Compile and Match functions.
const (
ANCHORED = C.PCRE_ANCHORED
BSR_ANYCRLF = C.PCRE_BSR_ANYCRLF
BSR_UNICODE = C.PCRE_BSR_UNICODE
NEWLINE_ANY = C.PCRE_NEWLINE_ANY
NEWLINE_ANYCRLF = C.PCRE_NEWLINE_ANYCRLF
NEWLINE_CR = C.PCRE_NEWLINE_CR
NEWLINE_CRLF = C.PCRE_NEWLINE_CRLF
NEWLINE_LF = C.PCRE_NEWLINE_LF
NO_UTF8_CHECK = C.PCRE_NO_UTF8_CHECK
)
// Flags for Compile functions
const (
CASELESS = C.PCRE_CASELESS
DOLLAR_ENDONLY = C.PCRE_DOLLAR_ENDONLY
DOTALL = C.PCRE_DOTALL
DUPNAMES = C.PCRE_DUPNAMES
EXTENDED = C.PCRE_EXTENDED
EXTRA = C.PCRE_EXTRA
FIRSTLINE = C.PCRE_FIRSTLINE
JAVASCRIPT_COMPAT = C.PCRE_JAVASCRIPT_COMPAT
MULTILINE = C.PCRE_MULTILINE
NO_AUTO_CAPTURE = C.PCRE_NO_AUTO_CAPTURE
UNGREEDY = C.PCRE_UNGREEDY
UTF8 = C.PCRE_UTF8
)
// Flags for Match functions
const (
NOTBOL = C.PCRE_NOTBOL
NOTEOL = C.PCRE_NOTEOL
NOTEMPTY = C.PCRE_NOTEMPTY
NOTEMPTY_ATSTART = C.PCRE_NOTEMPTY_ATSTART
NO_START_OPTIMIZE = C.PCRE_NO_START_OPTIMIZE
PARTIAL_HARD = C.PCRE_PARTIAL_HARD
PARTIAL_SOFT = C.PCRE_PARTIAL_SOFT
)
// Exec-time and get/set-time error codes
const (
ERROR_NO_MATCH = C.PCRE_ERROR_NOMATCH
ERROR_NULL = C.PCRE_ERROR_NULL
ERROR_BADOPTION = C.PCRE_ERROR_BADOPTION
ERROR_BADMAGIC = C.PCRE_ERROR_BADMAGIC
ERROR_UNKNOWN_OPCODE = C.PCRE_ERROR_UNKNOWN_OPCODE
ERROR_UNKNOWN_NODE = C.PCRE_ERROR_UNKNOWN_NODE
ERROR_NOMEMORY = C.PCRE_ERROR_NOMEMORY
ERROR_NOSUBSTRING = C.PCRE_ERROR_NOSUBSTRING
ERROR_MATCHLIMIT = C.PCRE_ERROR_MATCHLIMIT
ERROR_CALLOUT = C.PCRE_ERROR_CALLOUT
ERROR_BADUTF8 = C.PCRE_ERROR_BADUTF8
ERROR_BADUTF8_OFFSET = C.PCRE_ERROR_BADUTF8_OFFSET
ERROR_PARTIAL = C.PCRE_ERROR_PARTIAL
ERROR_BADPARTIAL = C.PCRE_ERROR_BADPARTIAL
)
// A reference to a compiled regular expression.
// Use Compile or MustCompile to create such objects.
type Regexp struct {
ptr []byte
}
// Number of bytes in the compiled pattern
func pcresize(ptr *C.pcre) (size C.size_t) {
C.pcre_fullinfo(ptr, nil, C.PCRE_INFO_SIZE, unsafe.Pointer(&size))
return
}
// Number of capture groups
func pcregroups(ptr *C.pcre) (count C.int) {
C.pcre_fullinfo(ptr, nil,
C.PCRE_INFO_CAPTURECOUNT, unsafe.Pointer(&count))
return
}
// Move pattern to the Go heap so that we do not have to use a
// finalizer. PCRE patterns are fully relocatable. (We do not use
// custom character tables.)
func toheap(ptr *C.pcre) (re Regexp) {
defer C.free(unsafe.Pointer(ptr))
size := pcresize(ptr)
re.ptr = make([]byte, size)
C.memcpy(unsafe.Pointer(&re.ptr[0]), unsafe.Pointer(ptr), size)
return
}
// Try to compile the pattern. If an error occurs, the second return
// value is non-nil.
func Compile(pattern string, flags int) (Regexp, *CompileError) {
pattern1 := C.CString(pattern)
defer C.free(unsafe.Pointer(pattern1))
if clen := int(C.strlen(pattern1)); clen != len(pattern) {
return Regexp{}, &CompileError{
Pattern: pattern,
Message: "NUL byte in pattern",
Offset: clen,
}
}
var errptr *C.char
var erroffset C.int
ptr := C.pcre_compile(pattern1, C.int(flags), &errptr, &erroffset, nil)
if ptr == nil {
return Regexp{}, &CompileError{
Pattern: pattern,
Message: C.GoString(errptr),
Offset: int(erroffset),
}
}
return toheap(ptr), nil
}
// Compile the pattern. If compilation fails, panic.
func MustCompile(pattern string, flags int) (re Regexp) {
re, err := Compile(pattern, flags)
if err != nil {
panic(err)
}
return
}
// Returns the number of capture groups in the compiled pattern.
func (re Regexp) Groups() int {
if re.ptr == nil {
panic("Regexp.Groups: uninitialized")
}
return int(pcregroups((*C.pcre)(unsafe.Pointer(&re.ptr[0]))))
}
// Matcher objects provide a place for storing match results.
// They can be created by the Matcher and MatcherString functions,
// or they can be initialized with Reset or ResetString.
type Matcher struct {
re Regexp
groups int
ovector []C.int // scratch space for capture offsets
matches bool // last match was successful
partial bool // was the last match a partial match?
subjects string // one of these fields is set to record the subject,
subjectb []byte // so that Group/GroupString can return slices
}
// Returns a new matcher object, with the byte array slice as a
// subject.
func (re Regexp) Matcher(subject []byte, flags int) (m *Matcher) {
m = new(Matcher)
m.Reset(re, subject, flags)
return
}
// Returns a new matcher object, with the specified subject string.
func (re Regexp) MatcherString(subject string, flags int) (m *Matcher) {
m = new(Matcher)
m.ResetString(re, subject, flags)
return
}
// Switches the matcher object to the specified pattern and subject.
func (m *Matcher) Reset(re Regexp, subject []byte, flags int) {
if re.ptr == nil {
panic("Regexp.Matcher: uninitialized")
}
m.init(re)
m.Match(subject, flags)
}
// Switches the matcher object to the specified pattern and subject
// string.
func (m *Matcher) ResetString(re Regexp, subject string, flags int) {
if re.ptr == nil {
panic("Regexp.Matcher: uninitialized")
}
m.init(re)
m.MatchString(subject, flags)
}
func (m *Matcher) init(re Regexp) {
m.matches = false
if m.re.ptr != nil && &m.re.ptr[0] == &re.ptr[0] {
// Skip group count extraction if the matcher has
// already been initialized with the same regular
// expression.
return
}
m.re = re
m.groups = re.Groups()
if ovectorlen := 3 * (1 + m.groups); len(m.ovector) < ovectorlen {
m.ovector = make([]C.int, ovectorlen)
}
}
var nullbyte = []byte{0}
// Tries to match the speficied byte array slice to the current
// pattern. Returns true if the match succeeds.
func (m *Matcher) Match(subject []byte, flags int) bool {
rc := m.Exec(subject, flags)
m.matches = matched(rc)
m.partial = (rc == C.PCRE_ERROR_PARTIAL)
return m.matches
}
// Tries to match the speficied subject string to the current pattern.
// Returns true if the match succeeds.
func (m *Matcher) MatchString(subject string, flags int) bool {
rc := m.ExecString(subject, flags)
m.matches = matched(rc)
m.partial = (rc == ERROR_PARTIAL)
return m.matches
}
// Tries to match the speficied byte array slice to the current
// pattern. Returns exec result.
func (m *Matcher) Exec(subject []byte, flags int) int {
if m.re.ptr == nil {
panic("Matcher.Match: uninitialized")
}
length := len(subject)
m.subjects = ""
m.subjectb = subject
if length == 0 {
subject = nullbyte // make first character adressable
}
subjectptr := (*C.char)(unsafe.Pointer(&subject[0]))
return m.exec(subjectptr, length, flags)
}
// Tries to match the speficied subject string to the current pattern.
// Returns exec result.
func (m *Matcher) ExecString(subject string, flags int) int {
if m.re.ptr == nil {
panic("Matcher.Match: uninitialized")
}
length := len(subject)
m.subjects = subject
m.subjectb = nil
if length == 0 {
subject = "\000" // make first character addressable
}
// The following is a non-portable kludge to avoid a copy
subjectptr := *(**C.char)(unsafe.Pointer(&subject))
return m.exec(subjectptr, length, flags)
}
func (m *Matcher) exec(subjectptr *C.char, length, flags int) int {
rc := C.pcre_exec((*C.pcre)(unsafe.Pointer(&m.re.ptr[0])), nil,
subjectptr, C.int(length),
0, C.int(flags), &m.ovector[0], C.int(len(m.ovector)))
return int(rc)
}
func matched(rc int) bool {
switch {
case rc >= 0 || rc == C.PCRE_ERROR_PARTIAL:
return true
case rc == C.PCRE_ERROR_NOMATCH:
return false
case rc == C.PCRE_ERROR_BADOPTION:
panic("PCRE.Match: invalid option flag")
}
panic("unexepected return code from pcre_exec: " +
strconv.Itoa(int(rc)))
}
// Returns true if a previous call to Matcher, MatcherString, Reset,
// ResetString, Match or MatchString succeeded.
func (m *Matcher) Matches() bool {
return m.matches
}
// Returns true if a previous call to Matcher, MatcherString, Reset,
// ResetString, Match or MatchString found a partial match. Not really
// an ideal interface but good enough for our current needs.
func (m *Matcher) Partial() bool {
return m.partial
}
// Returns the number of groups in the current pattern.
func (m *Matcher) Groups() int {
return m.groups
}
// Returns true if the numbered capture group is present in the last
// match (performed by Matcher, MatcherString, Reset, ResetString,
// Match, or MatchString). Group numbers start at 1. A capture group
// can be present and match the empty string.
func (m *Matcher) Present(group int) bool {
return m.ovector[2*group] >= 0
}
// Returns the numbered capture group of the last match (performed by
// Matcher, MatcherString, Reset, ResetString, Match, or MatchString).
// Group 0 is the part of the subject which matches the whole pattern;
// the first actual capture group is numbered 1. Capture groups which
// are not present return a nil slice.
func (m *Matcher) Group(group int) []byte {
start := m.ovector[2*group]
end := m.ovector[2*group+1]
if start >= 0 {
if m.subjectb != nil {
return m.subjectb[start:end]
}
return []byte(m.subjects[start:end])
}
return nil
}
// Returns the numbered capture group positions of the last match
// (performed by Matcher, MatcherString, Reset, ResetString, Match,
// or MatchString). Group 0 is the part of the subject which matches
// the whole pattern; the first actual capture group is numbered 1.
// Capture groups which are not present return a nil slice.
func (m *Matcher) GroupIndices(group int) []int {
start := m.ovector[2*group]
end := m.ovector[2*group+1]
if start >= 0 {
return []int{int(start), int(end)}
}
return nil
}
func (m *Matcher) ExtractString() []string {
if m.matches {
captured_texts := make([]string, m.groups+1)
captured_texts[0] = m.subjects
//fmt.Printf("capture(%d): %v\n", len(m.subjectb), m.subjects)
for i := 1; i < m.groups+1; i++ {
start := m.ovector[2*i]
end := m.ovector[2*i+1]
// fmt.Printf("start: %v, end: %v\n", start, end)
captured_text := m.subjects[start:end]
captured_texts[i] = captured_text
}
return captured_texts
} else {
return nil
}
}
// Returns the numbered capture group as a string. Group 0 is the
// part of the subject which matches the whole pattern; the first
// actual capture group is numbered 1. Capture groups which are not
// present return an empty string.
func (m *Matcher) GroupString(group int) string {
start := m.ovector[2*group]
end := m.ovector[2*group+1]
if start >= 0 {
if m.subjectb != nil {
return string(m.subjectb[start:end])
}
return m.subjects[start:end]
}
return ""
}
// Index returns the start and end of the first match, if a previous
// call to Matcher, MatcherString, Reset, ResetString, Match or
// MatchString succeeded. loc[0] is the start and loc[1] is the end.
func (m *Matcher) Index() []int {
if !m.Matches() {
return nil
}
return []int{int(m.ovector[0]), int(m.ovector[1])}
}
func (m *Matcher) name2index(name string) (group int, err error) {
if m.re.ptr == nil {
err = fmt.Errorf("Matcher.Named: uninitialized")
return
}
name1 := C.CString(name)
defer C.free(unsafe.Pointer(name1))
group = int(C.pcre_get_stringnumber(
(*C.pcre)(unsafe.Pointer(&m.re.ptr[0])), name1))
if group < 0 {
err = fmt.Errorf("Matcher.Named: unknown name: " + name)
return
}
return
}
// Returns the value of the named capture group. This is a nil slice
// if the capture group is not present. Panics if the name does not
// refer to a group.
func (m *Matcher) Named(group string) (g []byte, err error) {
group_num, err := m.name2index(group)
if err != nil {
return
}
return m.Group(group_num), nil
}
// Returns the value of the named capture group, or an empty string if
// the capture group is not present. Panics if the name does not
// refer to a group.
func (m *Matcher) NamedString(group string) (g string, err error) {
group_num, err := m.name2index(group)
if err != nil {
return
}
return m.GroupString(group_num), nil
}
// Returns true if the named capture group is present. Panics if the
// name does not refer to a group.
func (m *Matcher) NamedPresent(group string) (pres bool) {
group_num, err := m.name2index(group)
if err != nil {
return false
}
return m.Present(group_num)
}
// Return the start and end of the first match, or nil if no match.
// loc[0] is the start and loc[1] is the end.
func (re *Regexp) FindIndex(bytes []byte, flags int) []int {
m := re.Matcher(bytes, flags)
if m.Matches() {
return []int{int(m.ovector[0]), int(m.ovector[1])}
}
return nil
}
// Return a copy of a byte slice with pattern matches replaced by repl.
func (re Regexp) ReplaceAll(bytes, repl []byte, flags int) []byte {
m := re.Matcher(bytes, 0)
r := []byte{}
for m.Match(bytes, flags) {
r = append(append(r, bytes[:m.ovector[0]]...), repl...)
bytes = bytes[m.ovector[1]:]
}
return append(r, bytes...)
}
// A compilation error, as returned by the Compile function. The
// offset is the byte position in the pattern string at which the
// error was detected.
type CompileError struct {
Pattern string
Message string
Offset int
}
func (e *CompileError) String() string {
return e.Pattern + " (" + strconv.Itoa(e.Offset) + "): " + e.Message
}
func (e *CompileError) Error() string {
return e.String()
}