Repository navigation
Expand file tree
/
Copy pathregexp_corpus_helpers_test.go
More file actions
68 lines (59 loc) · 1.76 KB
/
Copy pathregexp_corpus_helpers_test.go
File metadata and controls
68 lines (59 loc) · 1.76 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
package regexp2
import "testing"
func corpusFindStringSubmatchIndex(t *testing.T, re *Regexp, input string) (bool, []int) {
t.Helper()
m, err := re.FindStringMatch(input)
if err != nil {
t.Fatalf("FindStringMatch failed: %v", err)
}
if m == nil {
return false, nil
}
return true, corpusMatchSubmatchIndex(m)
}
func corpusMatchSubmatchIndex(m *Match) []int {
groups := m.Groups()
out := make([]int, 0, len(groups)*2)
for i := range groups {
if len(groups[i].Captures) == 0 {
out = append(out, -1, -1)
continue
}
start, length := groups[i].ByteRange()
out = append(out, start, start+length)
}
return out
}
func assertCorpusOptimizedEquivalent(t *testing.T, pattern string, options []CompileOption, input string) {
t.Helper()
optimized := MustCompile(pattern, options...)
unoptimized := MustCompile(pattern, options...)
unoptimized.stringPrefixFilter = nil
gotMatch, got := corpusFindStringSubmatchIndex(t, optimized, input)
wantMatch, want := corpusFindStringSubmatchIndex(t, unoptimized, input)
if gotMatch != wantMatch || !corpusIntSlicesEqual(got, want) {
t.Fatalf("optimized FindStringMatch = (%v, %v), unoptimized = (%v, %v)", gotMatch, got, wantMatch, want)
}
gotIndexes, err := optimized.FindAllStringIndex(input, -1)
if err != nil {
t.Fatalf("optimized FindAllStringIndex failed: %v", err)
}
wantIndexes, err := unoptimized.FindAllStringIndex(input, -1)
if err != nil {
t.Fatalf("unoptimized FindAllStringIndex failed: %v", err)
}
if !sameStringIndexes(gotIndexes, wantIndexes) {
t.Fatalf("optimized FindAllStringIndex = %v, unoptimized = %v", gotIndexes, wantIndexes)
}
}
func corpusIntSlicesEqual(a, b []int) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}