-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcre2_internal_test.v
208 lines (170 loc) · 5.99 KB
/
pcre2_internal_test.v
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
module pcre2
import strings
fn test_find_match() {
mut r := compile(r'foo')!
mut m := r.find_match('baz foo bar', 0)?
assert m.ovector.len == 1 * 2
assert m.ovector == [4, 7]
if _ := r.find_match('', 1) {
assert false, 'should have returned none'
}
if _ := r.find_match('', 0) {
assert false, 'should have returned none'
}
if _ := r.find_match('baz foo bar', 5) {
assert false, 'should have returned none'
}
m = r.find_match('baz foo bar', 4)?
assert m.ovector.len == 1 * 2
assert m.ovector == [4, 7]
r = compile(r'x|(y)|(z)')!
m = r.find_match('az', 0)?
assert m.ovector.len == 3 * 2
assert m.ovector == [1, 2, -1, -1, 1, 2]
r = compile(r'x|(y)|(?<foo>z)')! // Named groups are included in the ovector.
m = r.find_match('az', 0)?
assert m.ovector.len == 3 * 2
assert m.ovector == [1, 2, -1, -1, 1, 2]
r = compile('\x00')!
m = r.find_match('x\x00z', 0)?
assert m.ovector.len == 1 * 2
assert m.ovector == [1, 2]
/*
This pathalogical pattern generates a `match limit exceeded` error (-47) when searching a string starting with `.`.
The exact same pattern works in Go and TypeScript/JavaScript; Dart has performance issues.
*/
r = compile(r'^\\?\.((?:\s*[a-zA-Z][\w\-]*)+)*(?:\s*)?(#[a-zA-Z][\w\-]*\s*)?(?:\s*)?(?:"(.+?)")?(?:\s*)?(\[.+])?(?:\s*)?([+-][ \w+-]+)?$')!
// This matches
if _ := r.find_match('.xxxxxxxxxx', 0) {
} else {
assert false, 'should not have returned none'
}
// Does not match (returns none)
if _ := r.find_match('.xxxxxxxxxx = ', 0) {
assert false, 'should have returned none'
}
// Panic: PCRE2 error -47 "match limit exceeded"
// Takes a whooping 180ms!
// if _ := r.find_match('.xxxxxxxxxxx =', 0) {
// assert false, 'should have returned none'
// }
}
fn test_get_and_get_all() {
mut r := compile(r'x|(y)|(z)')!
mut m := r.find_match('az', 0)?
assert m.get(0)? == 'z'
assert m.get(1)? == ''
assert m.get(2)? == 'z'
assert m.get(-1) or { 'ERR' } == 'ERR'
assert m.get(3) or { 'ERR' } == 'ERR'
assert m.get_all() == ['z', '', 'z']
}
fn test_replace_matches() {
assert replace_matches('$$ $0 $99', []) == '$ $0 $99'
assert replace_matches('$$ $$0 $0 $1 $2 $100', ['x', 'y']) == '$ $0 x y $2 $100'
}
/*
Tests for Regex methods that have been retired as private (they may be exposed in the future)
*/
fn test_substitute() {
mut r := compile(r'baz')!
mut s := r.substitute('', 0, 'foo', 0)!
assert s == ''
s = r.substitute('baz bar', 0, 'foo', 0)!
assert s == 'foo bar'
s = r.substitute('foobar', 0, 'foo', 0)!
assert s == 'foobar'
s = r.substitute('baz baz', 0, 'foo', 0)!
assert s == 'foo baz'
s = r.substitute('baz baz', 0, 'foo', C.PCRE2_SUBSTITUTE_GLOBAL)!
assert s == 'foo foo'
s = r.substitute(strings.repeat_string('foo', 1024) + 'baz', 0, 'foo', 0)!
assert s == strings.repeat_string('foo', 1025)
if _ := r.substitute('baz bar', 0, '$', 0) {
assert false, 'should have returned an error'
} else {
assert err.msg() == 'pcre2_substitute(): pattern: "baz": error -35 "invalid replacement string" at offset 1'
}
}
fn test_find_n_index() {
mut r := must_compile(r'x([yz])')
assert r.find_n_index('', 1).len == 0
assert r.find_n_index('an xyz', 1) == [[3, 5, 4, 5]]
assert r.find_n_index('an xyz', 2) == [[3, 5, 4, 5]]
assert r.find_n_index('an xy and xz', 2) == [[3, 5, 4, 5],
[10, 12, 11, 12]]
}
fn test_find_n_submatch() {
mut r := must_compile(r'x([yz])')
assert r.find_n_submatch('', 1).len == 0
assert r.find_n_submatch('an xyz', 1) == [['xy', 'y']]
assert r.find_n_submatch('an xyz', 2) == [['xy', 'y']]
assert r.find_n_submatch('an xy and xz', 2) == [['xy', 'y'],
['xz', 'z']]
}
fn test_find_n() {
mut r := must_compile(r'\d')
assert r.find_n('abcdeg', -1) == []
assert r.find_n('abcde5g', -1) == ['5']
assert r.find_n('1 abc 9 de 5 g', -1) == ['1', '9', '5']
assert r.find_n('1 abc 9 de 5 g', -1)[0] == '1'
assert r.find_n('1 abc 9 de 5 g', -1)[1] == '9'
assert r.find_n('1 abc 9 de 5 g', -1)[2] == '5'
assert r.find_n('1 abc 9 de 5 g', 0) == []
assert r.find_n('1 abc 9 de 5 g', 1) == ['1']
assert r.find_n('1 abc 9 de 5 g', 2) == ['1', '9']
assert r.find_n('1 abc 9 de 5 g', 3) == ['1', '9', '5']
assert r.find_n('1 abc 9 de 5 g', 4) == ['1', '9', '5']
assert must_compile(r'\d').find_n('1 abc 9 de 5 g', -1) == ['1', '9', '5']
}
fn test_replace_n_submatch_fn() {
mut r := must_compile(r'x')
assert r.replace_n_submatch_fn('', fn (m []string) string {
return m[0] + 'yz'
}, -1) == ''
assert r.replace_n_submatch_fn('x', fn (_ []string) string {
return 'foo✅'
}, -1) == 'foo✅'
assert r.replace_n_submatch_fn('y', fn (_ []string) string {
return 'foo'
}, -1) == 'y'
assert r.replace_n_submatch_fn('xz', fn (m []string) string {
return m[0] + 'y'
}, -1) == 'xyz'
r = must_compile(r'(([a-z]+)(\d+))')
assert r.replace_n_submatch_fn('456 xyz123', fn (m []string) string {
return '${m[2]} ${m[3]} ${m[1]}'
}, -1) == '456 xyz 123 xyz123'
assert r.replace_n_submatch_fn('xyz123 ab98', fn (m []string) string {
return '${m[1]} ${m[3]} ${m[2]}'
}, -1) == 'xyz123 123 xyz ab98 98 ab'
r = must_compile(r'x|(y)|(z)')
assert r.replace_n_submatch_fn('x', fn (m []string) string {
return '${m[1]}'
}, -1) == ''
assert r.replace_n_submatch_fn('y', fn (m []string) string {
return '${m[1]}'
}, -1) == 'y'
assert r.replace_n_submatch_fn('z', fn (m []string) string {
return '${m[2]}'
}, -1) == 'z'
}
fn test_replace_n_fn() {
mut r := must_compile(r'(x|y|z)')
assert r.replace_n_fn('z yx', fn (m string) string {
return '<${m}>'
}, -1) == '<z> <y><x>'
}
fn test_replace_n() {
mut r := must_compile(r'(x|y|z)')
assert r.replace_n('z y x', '"$1"', -1) == '"z" "y" "x"'
assert r.replace_n('z y x', '"$1"', 0) == 'z y x'
assert r.replace_n('z y x', '"$1"', 1) == '"z" y x'
assert r.replace_n('z y x', '"$1"', 2) == '"z" "y" x'
assert r.replace_n('z y x', '"$1"', 3) == '"z" "y" "x"'
assert r.replace_n('z y x', '"$1"', 4) == '"z" "y" "x"'
r = must_compile(r'x|(y)|(z)')
assert r.replace_n('z yx', '$$$1 $2$$', -1) == '$ z$ \$y $$ $'
r = must_compile(r'✅')
assert r.replace_n('✅ ✅✅', '🚀', 2) == '🚀 🚀✅'
}