-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorpus_pcre_test.go
More file actions
360 lines (330 loc) · 8.49 KB
/
Copy pathcorpus_pcre_test.go
File metadata and controls
360 lines (330 loc) · 8.49 KB
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
package pcre2lite_test
// This test drives a large, real-world corpus of patterns through both
// github.com/dlclark/regexp2 and go-pcre2-lite/regexp2 and asserts they agree.
//
// Corpus: testdata/pcre2_testoutput1.txt is "testoutput1" from PCRE2 v10.21
// (public domain), as shipped with github.com/dlclark/regexp2. We do NOT assert
// against the file's recorded output (it encodes non-UTF 8-bit semantics);
// instead we use it purely as a source of thousands of real patterns and
// inputs, and run a DIFFERENTIAL comparison between the two Go libraries. The
// parsing mirrors dlclark's own regexp_pcre_test.go so option handling matches.
import (
"bufio"
"fmt"
"os"
"strings"
"testing"
"time"
p2 "github.com/VillanCh/go-pcre2-lite/regexp2"
dl "github.com/dlclark/regexp2"
)
const corpusPath = "testdata/pcre2_testoutput1.txt"
// minAgreement is the required whole-match agreement ratio between dlclark and
// pcre2-lite over the corpus inputs where both engines compile the pattern and
// neither errors (timeout / match-limit). The remaining divergences stem from
// the documented UTF-vs-8bit and .NET-vs-PCRE semantic differences.
const minAgreement = 0.95
func TestPCRECorpusDifferential(t *testing.T) {
f, err := os.Open(corpusPath)
if err != nil {
t.Skipf("corpus not available: %v", err)
}
defer f.Close()
var (
patterns int
compileBoth int
compileOnlyP2 int // p2 compiles, dl rejects (p2/PCRE2 is more capable)
compileOnlyDl int // dl compiles, p2 rejects
inputsCompared int
matchConsistent int
matchDiverge int
engineErrors int
)
var diffSamples []string
addSample := func(format string, args ...interface{}) {
if len(diffSamples) < 50 {
diffSamples = append(diffSamples, fmt.Sprintf(format, args...))
}
}
sc := bufio.NewScanner(f)
sc.Buffer(make([]byte, 1<<20), 1<<20)
for sc.Scan() {
line := sc.Text()
trim := strings.TrimSpace(line)
if trim == "" || strings.HasPrefix(trim, "#") {
continue
}
delim := line[0]
if delim != '/' && delim != '"' {
continue
}
// Accumulate a (possibly multi-line) pattern up to its closing delim.
pattern := line
allowFirst := false
for !corpusContainsEnder(line, delim, allowFirst) {
if !sc.Scan() {
break
}
line = sc.Text()
pattern += "\n" + line
allowFirst = true
}
patterns++
dlRe, p2Re := corpusCompile(pattern)
// Collect the test inputs for this pattern (4-space indented lines),
// stopping at the blank separator before the next pattern.
var inputs []string
for sc.Scan() {
l := sc.Text()
if strings.TrimSpace(l) == "" {
break
}
if strings.HasPrefix(l, "\\= Expect") {
continue
}
if strings.HasPrefix(l, " ") {
inputs = append(inputs, strings.TrimRight(l[4:], " "))
}
// Result lines (" 0: ...", "No match", " 1: ...") are ignored.
}
switch {
case dlRe != nil && p2Re != nil:
compileBoth++
case dlRe == nil && p2Re != nil:
compileOnlyP2++
continue
case dlRe != nil && p2Re == nil:
compileOnlyDl++
addSample("COMPILE-DIVERGE (p2 rejects, dl accepts) pattern=%q", pattern)
continue
default:
continue // both rejected: consistent
}
dlRe.MatchTimeout = 250 * time.Millisecond
for _, raw := range inputs {
if raw == "\\" {
continue
}
in := corpusUnescape(raw)
dlM, dlErr := dlRe.FindStringMatch(in)
p2M, p2Err := p2Re.FindStringMatch(in)
if dlErr != nil || p2Err != nil {
engineErrors++
continue
}
inputsCompared++
if corpusMatchEqual(dlM, p2M) {
matchConsistent++
} else {
matchDiverge++
addSample("MATCH-DIVERGE pattern=%q input=%q dl=%s p2=%s",
pattern, in, dlMatchStr(dlM), p2MatchStr(p2M))
}
}
}
if err := sc.Err(); err != nil {
t.Fatalf("scanning corpus: %v", err)
}
agreement := 1.0
if total := matchConsistent + matchDiverge; total > 0 {
agreement = float64(matchConsistent) / float64(total)
}
t.Logf("patterns parsed: %d", patterns)
t.Logf("compiled by both: %d", compileBoth)
t.Logf("compiled only by p2 (PCRE2 superset): %d", compileOnlyP2)
t.Logf("compiled only by dl: %d", compileOnlyDl)
t.Logf("inputs compared: %d", inputsCompared)
t.Logf("match consistent: %d", matchConsistent)
t.Logf("match divergent: %d", matchDiverge)
t.Logf("engine errors (skipped): %d", engineErrors)
t.Logf("whole-match agreement: %.4f", agreement)
if len(diffSamples) > 0 {
t.Logf("divergence samples (capped at %d):", len(diffSamples))
for _, s := range diffSamples {
t.Logf(" %s", s)
}
}
if agreement < minAgreement {
t.Errorf("whole-match agreement %.4f below required %.4f", agreement, minAgreement)
}
}
// corpusCompile parses the trailing modifiers off a /pattern/opts spec and
// compiles it with both engines. A compile failure (or panic) yields a nil
// *Regexp for that engine rather than aborting the whole corpus run.
func corpusCompile(pattern string) (*dl.Regexp, *p2.Regexp) {
index := strings.LastIndexAny(pattern, "/\"")
var dlOpts dl.RegexOptions
var p2Opts p2.RegexOptions
if index >= 0 && index+1 < len(pattern) {
textOptions := pattern[index+1:]
pattern = pattern[:index+1]
for _, opt := range strings.Split(textOptions, ",") {
if opt == "dupnames" {
continue
}
if strings.Contains(opt, "i") {
dlOpts |= dl.IgnoreCase
p2Opts |= p2.IgnoreCase
}
if strings.Contains(opt, "s") {
dlOpts |= dl.Singleline
p2Opts |= p2.Singleline
}
if strings.Contains(opt, "m") {
dlOpts |= dl.Multiline
p2Opts |= p2.Multiline
}
if strings.Contains(opt, "x") {
dlOpts |= dl.IgnorePatternWhitespace
p2Opts |= p2.IgnorePatternWhitespace
}
}
}
if len(pattern) < 2 {
return nil, nil
}
pattern = pattern[1 : len(pattern)-1]
dlRe := func() (re *dl.Regexp) {
defer func() { _ = recover() }()
r, err := dl.Compile(pattern, dlOpts)
if err != nil {
return nil
}
return r
}()
p2Re := func() (re *p2.Regexp) {
defer func() { _ = recover() }()
r, err := p2.Compile(pattern, p2Opts)
if err != nil {
return nil
}
return r
}()
return dlRe, p2Re
}
func corpusMatchEqual(a *dl.Match, b *p2.Match) bool {
if (a == nil) != (b == nil) {
return false
}
if a == nil {
return true
}
return a.Index == b.Index && a.Length == b.Length && a.String() == b.String()
}
func dlMatchStr(m *dl.Match) string {
if m == nil {
return "<no-match>"
}
return fmt.Sprintf("%q", m.String())
}
func p2MatchStr(m *p2.Match) string {
if m == nil {
return "<no-match>"
}
return fmt.Sprintf("%q", m.String())
}
func corpusContainsEnder(line string, ender byte, allowFirst bool) bool {
index := strings.LastIndexByte(line, ender)
if index > 0 {
return true
}
return index == 0 && allowFirst
}
// corpusUnescape decodes the backslash escapes used in the corpus test inputs
// (\xNN, \nnn octal, \n \r \t etc.), mirroring dlclark's unEscapeToMatch.
func corpusUnescape(line string) string {
idx := strings.IndexRune(line, '\\')
if idx == -1 {
return line
}
var buf strings.Builder
buf.WriteString(line[:idx])
inEscape := false
for i := idx; i < len(line); i++ {
ch := line[i]
if ch == '\\' {
if inEscape {
buf.WriteByte(ch)
}
inEscape = !inEscape
continue
}
if inEscape {
switch ch {
case 'x':
buf.WriteByte(corpusScanHex(line, &i))
case 'a':
buf.WriteByte(0x07)
case 'b':
buf.WriteByte('\b')
case 'e':
buf.WriteByte(0x1b)
case 'f':
buf.WriteByte('\f')
case 'n':
buf.WriteByte('\n')
case 'r':
buf.WriteByte('\r')
case 't':
buf.WriteByte('\t')
case 'v':
buf.WriteByte(0x0b)
default:
if ch >= '0' && ch <= '7' {
buf.WriteByte(corpusScanOctal(line, &i))
} else {
buf.WriteByte(ch)
}
}
inEscape = false
} else {
buf.WriteByte(ch)
}
}
return buf.String()
}
func corpusScanHex(line string, idx *int) byte {
if *idx >= len(line)-2 {
return 0
}
(*idx)++
d1 := corpusHexDigit(line[*idx])
(*idx)++
d2 := corpusHexDigit(line[*idx])
if d1 < 0 || d2 < 0 {
return 0
}
return byte(d1*0x10 + d2)
}
func corpusHexDigit(ch byte) int {
if d := uint(ch - '0'); d <= 9 {
return int(d)
}
if d := uint(ch - 'a'); d <= 5 {
return int(d + 0xa)
}
if d := uint(ch - 'A'); d <= 5 {
return int(d + 0xa)
}
return -1
}
func corpusScanOctal(line string, idx *int) byte {
c := 3
if diff := len(line) - *idx; c > diff {
c = diff
}
i := 0
d := int(line[*idx] - '0')
for c > 0 && d <= 7 {
i *= 8
i += d
c--
(*idx)++
if *idx < len(line) {
d = int(line[*idx] - '0')
}
}
(*idx)--
i &= 0xFF
return byte(i)
}