OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, the Dart project authors. All rights reserved. | |
2 // Copyright 2013 the V8 project authors. All rights reserved. | |
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. | |
4 // | |
5 // Redistribution and use in source and binary forms, with or without | |
6 // modification, are permitted provided that the following conditions | |
7 // are met: | |
8 // 1. Redistributions of source code must retain the above copyright | |
9 // notice, this list of conditions and the following disclaimer. | |
10 // 2. Redistributions in binary form must reproduce the above copyright | |
11 // notice, this list of conditions and the following disclaimer in the | |
12 // documentation and/or other materials provided with the distribution. | |
13 // | |
14 // THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y | |
15 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
16 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
17 // DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN
Y | |
18 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
19 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
20 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N | |
21 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
23 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 | |
25 import 'v8_regexp_utils.dart'; | |
26 import 'package:expect/expect.dart'; | |
27 | |
28 void main() { | |
29 description("This page tests handling of parentheses subexpressions."); | |
30 | |
31 var regexp1 = new RegExp(r"(a|A)(b|B)"); | |
32 shouldBe(regexp1.firstMatch('abc'), ['ab','a','b']); | |
33 | |
34 var regexp2 = new RegExp(r"(a((b)|c|d))e"); | |
35 shouldBe(regexp2.firstMatch('abacadabe'), ['abe','ab','b','b']); | |
36 | |
37 var regexp3 = new RegExp(r"(a(b|(c)|d))e"); | |
38 shouldBe(regexp3.firstMatch('abacadabe'), ['abe','ab','b',null]); | |
39 | |
40 var regexp4 = new RegExp(r"(a(b|c|(d)))e"); | |
41 shouldBe(regexp4.firstMatch('abacadabe'), ['abe','ab','b',null]); | |
42 | |
43 var regexp5 = new RegExp(r"(a((b)|(c)|(d)))e"); | |
44 shouldBe(regexp5.firstMatch('abacadabe'), ['abe','ab','b','b',null,null]); | |
45 | |
46 var regexp6 = new RegExp(r"(a((b)|(c)|(d)))"); | |
47 shouldBe(regexp6.firstMatch('abcde'), ['ab','ab','b','b',null,null]); | |
48 | |
49 var regexp7 = new RegExp(r"(a(b)??)??c"); | |
50 shouldBe(regexp7.firstMatch('abc'), ['abc','ab','b']); | |
51 | |
52 var regexp8 = new RegExp(r"(a|(e|q))(x|y)"); | |
53 shouldBe(regexp8.firstMatch('bcaddxqy') , ['qy','q','q','y']); | |
54 | |
55 var regexp9 = new RegExp(r"((t|b)?|a)$"); | |
56 shouldBe(regexp9.firstMatch('asdfjejgsdflaksdfjkeljghkjea'), ['a','a',null]); | |
57 | |
58 var regexp10 = new RegExp(r"(?:h|e?(?:t|b)?|a?(?:t|b)?)(?:$)"); | |
59 shouldBe(regexp10.firstMatch('asdfjejgsdflaksdfjkeljghat'), ['at']); | |
60 | |
61 var regexp11 = new RegExp(r"([Jj]ava([Ss]cript)?)\sis\s(fun\w*)"); | |
62 shouldBeNull(regexp11.firstMatch('Developing with JavaScript is dangerous, do
not try it without assistance')); | |
63 | |
64 var regexp12 = new RegExp(r"(?:(.+), )?(.+), (..) to (?:(.+), )?(.+), (..)"); | |
65 shouldBe(regexp12.firstMatch('Seattle, WA to Buckley, WA'), ['Seattle, WA to B
uckley, WA', null, 'Seattle', 'WA', null, 'Buckley', 'WA']); | |
66 | |
67 var regexp13 = new RegExp(r"(A)?(A.*)"); | |
68 shouldBe(regexp13.firstMatch('zxcasd;fl\ ^AaaAAaaaf;lrlrzs'), ['AaaAAaaaf;lrlr
zs',null,'AaaAAaaaf;lrlrzs']); | |
69 | |
70 var regexp14 = new RegExp(r"(a)|(b)"); | |
71 shouldBe(regexp14.firstMatch('b'), ['b',null,'b']); | |
72 | |
73 var regexp15 = new RegExp(r"^(?!(ab)de|x)(abd)(f)"); | |
74 shouldBe(regexp15.firstMatch('abdf'), ['abdf',null,'abd','f']); | |
75 | |
76 var regexp16 = new RegExp(r"(a|A)(b|B)"); | |
77 shouldBe(regexp16.firstMatch('abc'), ['ab','a','b']); | |
78 | |
79 var regexp17 = new RegExp(r"(a|d|q|)x", caseSensitive: false); | |
80 shouldBe(regexp17.firstMatch('bcaDxqy'), ['Dx','D']); | |
81 | |
82 var regexp18 = new RegExp(r"^.*?(:|$)"); | |
83 shouldBe(regexp18.firstMatch('Hello: World'), ['Hello:',':']); | |
84 | |
85 var regexp19 = new RegExp(r"(ab|^.{0,2})bar"); | |
86 shouldBe(regexp19.firstMatch('barrel'), ['bar','']); | |
87 | |
88 var regexp20 = new RegExp(r"(?:(?!foo)...|^.{0,2})bar(.*)"); | |
89 shouldBe(regexp20.firstMatch('barrel'), ['barrel','rel']); | |
90 shouldBe(regexp20.firstMatch('2barrel'), ['2barrel','rel']); | |
91 | |
92 var regexp21 = new RegExp(r"([a-g](b|B)|xyz)"); | |
93 shouldBe(regexp21.firstMatch('abc'), ['ab','ab','b']); | |
94 | |
95 var regexp22 = new RegExp(r"(?:^|;)\s*abc=([^;]*)"); | |
96 shouldBeNull(regexp22.firstMatch('abcdlskfgjdslkfg')); | |
97 | |
98 var regexp23 = new RegExp("\"[^<\"]*\"|'[^<']*'"); | |
99 shouldBe(regexp23.firstMatch('<html xmlns=\"http://www.w3.org/1999/xhtml\"'), | |
100 ['\"http://www.w3.org/1999/xhtml\"']); | |
101 | |
102 var regexp24 = new RegExp(r"^(?:(?=abc)\w{3}:|\d\d)$"); | |
103 shouldBeNull(regexp24.firstMatch('123')); | |
104 | |
105 var regexp25 = new RegExp(r"^\s*(\*|[\w\-]+)(\b|$)?"); | |
106 shouldBe(regexp25.firstMatch('this is a test'), ['this','this',null]); | |
107 shouldBeNull(regexp25.firstMatch('!this is a test')); | |
108 | |
109 var regexp26 = new RegExp(r"a(b)(a*)|aaa"); | |
110 shouldBe(regexp26.firstMatch('aaa'), ['aaa',null,null]); | |
111 | |
112 var regexp27 = new RegExp( | |
113 "^" + | |
114 "(?:" + | |
115 "([^:/?#]+):" + /* scheme */ | |
116 ")?" + | |
117 "(?:" + | |
118 "(//)" + /* authorityRoot */ | |
119 "(" + /* authority */ | |
120 "(?:" + | |
121 "(" + /* userInfo */ | |
122 "([^:@]*)" + /* user */ | |
123 ":?" + | |
124 "([^:@]*)" + /* password */ | |
125 ")?" + | |
126 "@" + | |
127 ")?" + | |
128 "([^:/?#]*)" + /* domain */ | |
129 "(?::(\\d*))?" + /* port */ | |
130 ")" + | |
131 ")?" + | |
132 "([^?#]*)" + /*path*/ | |
133 "(?:\\?([^#]*))?" + /* queryString */ | |
134 "(?:#(.*))?" /*fragment */ | |
135 ); | |
136 shouldBe(regexp27.firstMatch('file:///Users/Someone/Desktop/HelloWorld/index.h
tml'), ['file:///Users/Someone/Desktop/HelloWorld/index.html','file','//','',nul
l,null,null,'',null,'/Users/Someone/Desktop/HelloWorld/index.html',null,null]); | |
137 | |
138 var regexp28 = new RegExp( | |
139 "^" + | |
140 "(?:" + | |
141 "([^:/?#]+):" + /* scheme */ | |
142 ")?" + | |
143 "(?:" + | |
144 "(//)" + /* authorityRoot */ | |
145 "(" + /* authority */ | |
146 "(" + /* userInfo */ | |
147 "([^:@]*)" + /* user */ | |
148 ":?" + | |
149 "([^:@]*)" + /* password */ | |
150 ")?" + | |
151 "@" + | |
152 ")" + | |
153 ")?" | |
154 ); | |
155 shouldBe(regexp28.firstMatch('file:///Users/Someone/Desktop/HelloWorld/index.h
tml'), ['file:','file',null,null,null,null,null]); | |
156 | |
157 var regexp29 = new RegExp(r'^\s*((\[[^\]]+\])|(u?)("[^"]+"))\s*'); | |
158 shouldBeNull(regexp29.firstMatch('Committer:')); | |
159 | |
160 var regexp30 = new RegExp(r'^\s*((\[[^\]]+\])|m(u?)("[^"]+"))\s*'); | |
161 shouldBeNull(regexp30.firstMatch('Committer:')); | |
162 | |
163 var regexp31 = new RegExp(r'^\s*(m(\[[^\]]+\])|m(u?)("[^"]+"))\s*'); | |
164 shouldBeNull(regexp31.firstMatch('Committer:')); | |
165 | |
166 var regexp32 = new RegExp(r'\s*(m(\[[^\]]+\])|m(u?)("[^"]+"))\s*'); | |
167 shouldBeNull(regexp32.firstMatch('Committer:')); | |
168 | |
169 var regexp33 = new RegExp('^(?:(?:(a)(xyz|[^>"\'\s]*)?)|(/?>)|.[^\w\s>]*)'); | |
170 shouldBe(regexp33.firstMatch('> <head>'), ['>',null,null,'>']); | |
171 | |
172 var regexp34 = new RegExp(r"(?:^|\b)btn-\S+"); | |
173 shouldBeNull(regexp34.firstMatch('xyz123')); | |
174 shouldBe(regexp34.firstMatch('btn-abc'),['btn-abc']); | |
175 shouldBeNull(regexp34.firstMatch('btn- abc')); | |
176 shouldBeNull(regexp34.firstMatch('XXbtn-abc')); | |
177 shouldBe(regexp34.firstMatch('XX btn-abc'),['btn-abc']); | |
178 | |
179 var regexp35 = new RegExp(r"^((a|b)(x|xxx)|)$"); | |
180 shouldBe(regexp35.firstMatch('ax'), ['ax','ax','a','x']); | |
181 shouldBeNull(regexp35.firstMatch('axx')); | |
182 shouldBe(regexp35.firstMatch('axxx'), ['axxx','axxx','a','xxx']); | |
183 shouldBe(regexp35.firstMatch('bx'), ['bx','bx','b','x']); | |
184 shouldBeNull(regexp35.firstMatch('bxx')); | |
185 shouldBe(regexp35.firstMatch('bxxx'), ['bxxx','bxxx','b','xxx']); | |
186 | |
187 var regexp36 = new RegExp(r"^((\/|\.|\-)(\d\d|\d\d\d\d)|)$"); | |
188 shouldBe(regexp36.firstMatch('/2011'), ['/2011','/2011','/','2011']); | |
189 shouldBe(regexp36.firstMatch('/11'), ['/11','/11','/','11']); | |
190 shouldBeNull(regexp36.firstMatch('/123')); | |
191 | |
192 var regexp37 = new RegExp(r"^([1][0-2]|[0]\d|\d)(\/|\.|\-)([0-2]\d|[3][0-1]|\d
)((\/|\.|\-)(\d\d|\d\d\d\d)|)$"); | |
193 shouldBe(regexp37.firstMatch('7/4/1776'), ['7/4/1776','7','/','4','/1776','/',
'1776']); | |
194 shouldBe(regexp37.firstMatch('07-04-1776'), ['07-04-1776','07','-','04','-1776
','-','1776']); | |
195 | |
196 var regexp38 = new RegExp(r"^(z|(x|xx)|b|)$"); | |
197 shouldBe(regexp38.firstMatch('xx'), ['xx','xx','xx']); | |
198 shouldBe(regexp38.firstMatch('b'), ['b','b',null]); | |
199 shouldBe(regexp38.firstMatch('z'), ['z','z',null]); | |
200 shouldBe(regexp38.firstMatch(''), ['','',null]); | |
201 | |
202 var regexp39 = new RegExp(r"(8|((?=P)))?"); | |
203 shouldBe(regexp39.firstMatch(''), ['',null,null]); | |
204 shouldBe(regexp39.firstMatch('8'), ['8','8',null]); | |
205 shouldBe(regexp39.firstMatch('zP'), ['',null,null]); | |
206 | |
207 var regexp40 = new RegExp(r"((8)|((?=P){4}))?()"); | |
208 shouldBe(regexp40.firstMatch(''), ['',null,null,null,'']); | |
209 shouldBe(regexp40.firstMatch('8'), ['8','8','8',null,'']); | |
210 shouldBe(regexp40.firstMatch('zPz'), ['',null,null,null,'']); | |
211 shouldBe(regexp40.firstMatch('zPPz'), ['',null,null,null,'']); | |
212 shouldBe(regexp40.firstMatch('zPPPz'), ['',null,null,null,'']); | |
213 shouldBe(regexp40.firstMatch('zPPPPz'), ['',null,null,null,'']); | |
214 | |
215 var regexp41 = new RegExp(r"(([\w\-]+:\/\/?|www[.])[^\s()<>]+(?:([\w\d]+)|([^\
[:punct:\]\s()<>\W]|\/)))"); | |
216 shouldBe(regexp41.firstMatch('Here is a link: http://www.acme.com/our_products
/index.html. That is all we want!'), | |
217 ['http://www.acme.com/our_products/index.html','http://www.acme.com/our_pr
oducts/index.html','http://','l',null]); | |
218 | |
219 var regexp42 = new RegExp(r"((?:(4)?))?"); | |
220 shouldBe(regexp42.firstMatch(''), ['',null,null]); | |
221 shouldBe(regexp42.firstMatch('4'), ['4','4','4']); | |
222 shouldBe(regexp42.firstMatch('4321'), ['4','4','4']); | |
223 | |
224 shouldBeTrue(new RegExp(r"(?!(?=r{0}){2,})|((z)?)?", caseSensitive: false).has
Match('')); | |
225 | |
226 var regexp43 = new RegExp(r"(?!(?:\1+s))"); | |
227 shouldBe(regexp43.firstMatch('SSS'), ['']); | |
228 | |
229 var regexp44 = new RegExp(r"(?!(?:\3+(s+?)))"); | |
230 shouldBe(regexp44.firstMatch('SSS'), ['',null]); | |
231 | |
232 var regexp45 = new RegExp(r"((?!(?:|)v{2,}|))"); | |
233 shouldBeNull(regexp45.firstMatch('vt')); | |
234 | |
235 var regexp46 = new RegExp(r"(w)(?:5{3}|())|pk"); | |
236 shouldBeNull(regexp46.firstMatch('5')); | |
237 shouldBe(regexp46.firstMatch('pk'), ['pk',null,null]); | |
238 shouldBe(regexp46.firstMatch('Xw555'), ['w555','w',null]); | |
239 shouldBe(regexp46.firstMatch('Xw55pk5'), ['w','w','']); | |
240 | |
241 var regexp47 = new RegExp(r"(.*?)(?:(?:\?(.*?)?)?)(?:(?:#)?)$"); | |
242 shouldBe(regexp47.firstMatch('/www.acme.com/this/is/a/path/file.txt'), | |
243 ['/www.acme.com/this/is/a/path/file.txt','/www.acme.com/this/is/a/path/f
ile.txt',null]); | |
244 | |
245 var regexp48 = new RegExp(r"^(?:(\w+):\/*([\w\.\-\d]+)(?::(\d+)|)(?=(?:\/|$))|
)(?:$|\/?(.*?)(?:\?(.*?)?|)(?:#(.*)|)$)"); | |
246 shouldBe(regexp48.firstMatch('http://www.acme.com/this/is/a/path/file.txt'), | |
247 ['http://www.acme.com/this/is/a/path/file.txt','http','www.acme.com',null,
'this/is/a/path/file.txt',null,null]); | |
248 | |
249 var regexp49 = new RegExp(r"(?:([^:]*?)(?:(?:\?(.*?)?)?)(?:(?:#)?)$)|(?:^(?:(\
w+):\/*([\w\.\-\d]+)(?::(\d+)|)(?=(?:\/|$))|)(?:$|\/?(.*?)(?:\?(.*?)?|)(?:#(.*)|
)$))"); | |
250 shouldBe(regexp49.firstMatch('http://www.acme.com/this/is/a/path/file.txt'), | |
251 ['http://www.acme.com/this/is/a/path/file.txt',null,null,'http','www.acme.
com',null,'this/is/a/path/file.txt',null,null]); | |
252 | |
253 var regexp50 = new RegExp(r"((a)b{28,}c|d)x"); | |
254 shouldBeNull(regexp50.firstMatch('((a)b{28,}c|d)x')); | |
255 shouldBe(regexp50.firstMatch('abbbbbbbbbbbbbbbbbbbbbbbbbbbbcx'), ['abbbbbbbbbb
bbbbbbbbbbbbbbbbbbcx', 'abbbbbbbbbbbbbbbbbbbbbbbbbbbbc', 'a']); | |
256 shouldBe(regexp50.firstMatch('dx'), ['dx', 'd', null]); | |
257 | |
258 var s = "((.\s{-}).{28,}\P{Yi}?{,30}\|.)\x9e{-,}\P{Any}"; | |
259 var regexp51 = new RegExp(s); | |
260 shouldBeNull(regexp51.firstMatch('abc')); | |
261 shouldBe(regexp51.firstMatch(s), [')\x9e{-,}P{Any}',')',null]); | |
262 | |
263 var regexp52 = new RegExp(r"(Rob)|(Bob)|(Robert)|(Bobby)"); | |
264 shouldBe(regexp52.firstMatch('Hi Bob'), ['Bob',null,'Bob',null,null]); | |
265 | |
266 // Test cases discovered by fuzzing that crashed the compiler. | |
267 var regexp53 = new RegExp(r"(?=(?:(?:(gB)|(?!cs|<))((?=(?!v6){0,})))|(?=#)+?)"
, multiLine: true); | |
268 shouldBe(regexp53.firstMatch('#'), ['',null,'']); | |
269 var regexp54 = new RegExp(r"((?:(?:()|(?!))((?=(?!))))|())", multiLine: true); | |
270 shouldBe(regexp54.firstMatch('#'), ['','',null,null,'']); | |
271 var regexp55 = new RegExp(r"(?:(?:(?:a?|(?:))((?:)))|a?)", multiLine: true); | |
272 shouldBe(regexp55.firstMatch('#'), ['','']); | |
273 | |
274 // Test evaluation order of empty subpattern alternatives. | |
275 var regexp56 = new RegExp(r"(|a)"); | |
276 shouldBe(regexp56.firstMatch('a'), ['','']); | |
277 var regexp57 = new RegExp(r"(a|)"); | |
278 shouldBe(regexp57.firstMatch('a'), ['a','a']); | |
279 | |
280 // Tests that non-greedy repeat quantified parentheses will backtrack through
multiple frames of subpattern matches. | |
281 var regexp58 = new RegExp(r"a|b(?:[^b])*?c"); | |
282 shouldBe(regexp58.firstMatch('badbc'), ['a']); | |
283 var regexp59 = new RegExp(r"(X(?:.(?!X))*?Y)|(Y(?:.(?!Y))*?Z)"); | |
284 Expect.listEquals(regexp59.allMatches('Y aaa X Match1 Y aaa Y Match2 Z') | |
285 .map((m) => m.group(0)) | |
286 .toList(), | |
287 ['X Match1 Y','Y Match2 Z']); | |
288 } | |
OLD | NEW |