OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. All rights reserved. | 1 // Copyright (c) 2014, the Dart project authors. All rights reserved. |
2 // Copyright 2012 the V8 project authors. All rights reserved. | 2 // Copyright 2012 the V8 project authors. All rights reserved. |
3 // Redistribution and use in source and binary forms, with or without | 3 // Redistribution and use in source and binary forms, with or without |
4 // modification, are permitted provided that the following conditions are | 4 // modification, are permitted provided that the following conditions are |
5 // met: | 5 // met: |
6 // | 6 // |
7 // * Redistributions of source code must retain the above copyright | 7 // * Redistributions of source code must retain the above copyright |
8 // notice, this list of conditions and the following disclaimer. | 8 // notice, this list of conditions and the following disclaimer. |
9 // * Redistributions in binary form must reproduce the above | 9 // * Redistributions in binary form must reproduce the above |
10 // copyright notice, this list of conditions and the following | 10 // copyright notice, this list of conditions and the following |
(...skipping 10 matching lines...) Expand all Loading... | |
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 |
29 import "package:expect/expect.dart"; | 29 import "package:expect/expect.dart"; |
30 | 30 |
31 void testEscape(str, regex) { | 31 void testEscape(str, regexp) { |
32 assertEquals("foo:bar:baz", str.split(regex).join(":")); | 32 assertEquals("foo:bar:baz", str.split(regexp).join(":")); |
33 } | 33 } |
34 | 34 |
35 void assertEquals(actual, expected, [message]) => | 35 void assertEquals(actual, expected, [message]) => |
36 Expect.equals(actual, expected, message); | 36 Expect.equals(actual, expected, message); |
37 void assertTrue(actual, [message]) => Expect.isTrue(actual, message); | 37 void assertTrue(actual, [message]) => Expect.isTrue(actual, message); |
38 void assertFalse(actual, [message]) => Expect.isFalse(actual, message); | 38 void assertFalse(actual, [message]) => Expect.isFalse(actual, message); |
39 void assertThrows(fn) => Expect.throws(fn); | 39 void assertThrows(fn) => Expect.throws(fn); |
40 | 40 |
41 void main() { | 41 void main() { |
42 testEscape("foo\nbar\nbaz", new RegExp(r"\n")); | 42 testEscape("foo\nbar\nbaz", new RegExp(r"\n")); |
43 testEscape("foo bar baz", new RegExp(r"\s")); | 43 testEscape("foo bar baz", new RegExp(r"\s")); |
44 testEscape("foo\tbar\tbaz", new RegExp(r"\s")); | 44 testEscape("foo\tbar\tbaz", new RegExp(r"\s")); |
45 testEscape("foo-bar-baz", new RegExp(r"\u002D")); | 45 testEscape("foo-bar-baz", new RegExp(r"\u002D")); |
46 | 46 |
47 // Test containing null char in regexp. | 47 // Test containing null char in regexpgexp. |
Bob Nystrom
2017/07/26 23:54:49
There are still some more here and below if you lo
| |
48 var s = '[' + new String.fromCharCode(0) + ']'; | 48 var s = '[' + new String.fromCharCode(0) + ']'; |
49 var re = new RegExp(s); | 49 var regexp = new RegExp(s); |
50 assertEquals(re.allMatches(s).length, 1); | 50 assertEquals(regexp.allMatches(s).length, 1); |
51 assertEquals(re.stringMatch(s), new String.fromCharCode(0)); | 51 assertEquals(regexp.stringMatch(s), new String.fromCharCode(0)); |
52 | 52 |
53 final _vmFrame = new RegExp(r'^#\d+\s+(\S.*) \((.+?):(\d+)(?::(\d+))?\)$'); | 53 final _vmFrame = new RegExp(r'^#\d+\s+(\S.*) \((.+?):(\d+)(?::(\d+))?\)$'); |
54 final _traceLine = | 54 final _traceLine = |
55 "#0 Trace.Trace.parse (package:stack_trace/src/trace.dart:130:7)"; | 55 "#0 Trace.Trace.parse (package:stack_trace/src/trace.dart:130:7)"; |
56 Expect.equals(_vmFrame.firstMatch(_traceLine).group(0), _traceLine); | 56 Expect.equals(_vmFrame.firstMatch(_traceLine).group(0), _traceLine); |
57 | 57 |
58 // Test the UTF16 case insensitive comparison. | 58 // Test the UTF16 case insensitive comparison. |
59 re = new RegExp(r"x(a)\1x", caseSensitive: false); | 59 regexp = new RegExp(r"x(a)\1x", caseSensitive: false); |
60 Expect.equals(re.firstMatch("xaAx\u1234").group(0), "xaAx"); | 60 Expect.equals(regexp.firstMatch("xaAx\u1234").group(0), "xaAx"); |
61 | 61 |
62 // Test strings containing all line separators | 62 // Test strings containing all line separators |
63 s = 'aA\nbB\rcC\r\ndD\u2028eE\u2029fF'; | 63 s = 'aA\nbB\rcC\r\ndD\u2028eE\u2029fF'; |
64 // any non-newline character at the beginning of a line | 64 // any non-newline character at the beginning of a line |
65 re = new RegExp(r"^.", multiLine: true); | 65 regexp = new RegExp(r"^.", multiLine: true); |
66 var result = re.allMatches(s).toList(); | 66 var result = regexp.allMatches(s).toList(); |
67 assertEquals(result.length, 6); | 67 assertEquals(result.length, 6); |
68 assertEquals(result[0][0], 'a'); | 68 assertEquals(result[0][0], 'a'); |
69 assertEquals(result[1][0], 'b'); | 69 assertEquals(result[1][0], 'b'); |
70 assertEquals(result[2][0], 'c'); | 70 assertEquals(result[2][0], 'c'); |
71 assertEquals(result[3][0], 'd'); | 71 assertEquals(result[3][0], 'd'); |
72 assertEquals(result[4][0], 'e'); | 72 assertEquals(result[4][0], 'e'); |
73 assertEquals(result[5][0], 'f'); | 73 assertEquals(result[5][0], 'f'); |
74 | 74 |
75 // any non-newline character at the end of a line | 75 // any non-newline character at the end of a line |
76 re = new RegExp(r".$", multiLine: true); | 76 regexp = new RegExp(r".$", multiLine: true); |
77 result = re.allMatches(s).toList(); | 77 result = regexp.allMatches(s).toList(); |
78 assertEquals(result.length, 6); | 78 assertEquals(result.length, 6); |
79 assertEquals(result[0][0], 'A'); | 79 assertEquals(result[0][0], 'A'); |
80 assertEquals(result[1][0], 'B'); | 80 assertEquals(result[1][0], 'B'); |
81 assertEquals(result[2][0], 'C'); | 81 assertEquals(result[2][0], 'C'); |
82 assertEquals(result[3][0], 'D'); | 82 assertEquals(result[3][0], 'D'); |
83 assertEquals(result[4][0], 'E'); | 83 assertEquals(result[4][0], 'E'); |
84 assertEquals(result[5][0], 'F'); | 84 assertEquals(result[5][0], 'F'); |
85 | 85 |
86 // *any* character at the beginning of a line | 86 // *any* character at the beginning of a line |
87 re = new RegExp(r"^[^]", multiLine: true); | 87 regexp = new RegExp(r"^[^]", multiLine: true); |
88 result = re.allMatches(s).toList(); | 88 result = regexp.allMatches(s).toList(); |
89 assertEquals(result.length, 7); | 89 assertEquals(result.length, 7); |
90 assertEquals(result[0][0], 'a'); | 90 assertEquals(result[0][0], 'a'); |
91 assertEquals(result[1][0], 'b'); | 91 assertEquals(result[1][0], 'b'); |
92 assertEquals(result[2][0], 'c'); | 92 assertEquals(result[2][0], 'c'); |
93 assertEquals(result[3][0], '\n'); | 93 assertEquals(result[3][0], '\n'); |
94 assertEquals(result[4][0], 'd'); | 94 assertEquals(result[4][0], 'd'); |
95 assertEquals(result[5][0], 'e'); | 95 assertEquals(result[5][0], 'e'); |
96 assertEquals(result[6][0], 'f'); | 96 assertEquals(result[6][0], 'f'); |
97 | 97 |
98 // *any* character at the end of a line | 98 // *any* character at the end of a line |
99 re = new RegExp(r"[^]$", multiLine: true); | 99 regexp = new RegExp(r"[^]$", multiLine: true); |
100 result = re.allMatches(s).toList(); | 100 result = regexp.allMatches(s).toList(); |
101 assertEquals(result.length, 7); | 101 assertEquals(result.length, 7); |
102 assertEquals(result[0][0], 'A'); | 102 assertEquals(result[0][0], 'A'); |
103 assertEquals(result[1][0], 'B'); | 103 assertEquals(result[1][0], 'B'); |
104 assertEquals(result[2][0], 'C'); | 104 assertEquals(result[2][0], 'C'); |
105 assertEquals(result[3][0], '\r'); | 105 assertEquals(result[3][0], '\r'); |
106 assertEquals(result[4][0], 'D'); | 106 assertEquals(result[4][0], 'D'); |
107 assertEquals(result[5][0], 'E'); | 107 assertEquals(result[5][0], 'E'); |
108 assertEquals(result[6][0], 'F'); | 108 assertEquals(result[6][0], 'F'); |
109 | 109 |
110 // Some tests from the Mozilla tests, where our behavior used to differ | 110 // Some tests from the Mozilla tests, wheregexp our behavior used to differ |
111 // from SpiderMonkey. | 111 // from SpiderMonkey. |
112 // From ecma_3/RegExp/regress-334158.js | 112 // From ecma_3/RegExp/regexpgregexpss-334158.js |
113 assertTrue("\x01".contains(new RegExp(r"\ca"))); | 113 assertTrue("\x01".contains(new RegExp(r"\ca"))); |
114 assertFalse("\\ca".contains(new RegExp(r"\ca"))); | 114 assertFalse("\\ca".contains(new RegExp(r"\ca"))); |
115 assertFalse("ca".contains(new RegExp(r"\ca"))); | 115 assertFalse("ca".contains(new RegExp(r"\ca"))); |
116 assertTrue("\\ca".contains(new RegExp(r"\c[a/]"))); | 116 assertTrue("\\ca".contains(new RegExp(r"\c[a/]"))); |
117 assertTrue("\\c/".contains(new RegExp(r"\c[a/]"))); | 117 assertTrue("\\c/".contains(new RegExp(r"\c[a/]"))); |
118 | 118 |
119 // Test \c in character class | 119 // Test \c in character class |
120 re = r"^[\cM]$"; | 120 var re = r"^[\cM]$"; |
121 assertTrue("\r".contains(new RegExp(re))); | 121 assertTrue("\r".contains(new RegExp(re))); |
122 assertFalse("M".contains(new RegExp(re))); | 122 assertFalse("M".contains(new RegExp(re))); |
123 assertFalse("c".contains(new RegExp(re))); | 123 assertFalse("c".contains(new RegExp(re))); |
124 assertFalse("\\".contains(new RegExp(re))); | 124 assertFalse("\\".contains(new RegExp(re))); |
125 assertFalse("\x03".contains(new RegExp(re))); // I.e., read as \cc | 125 assertFalse("\x03".contains(new RegExp(re))); // I.e., read as \cc |
126 | 126 |
127 re = r"^[\c]]$"; | 127 re = r"^[\c]]$"; |
128 assertTrue("c]".contains(new RegExp(re))); | 128 assertTrue("c]".contains(new RegExp(re))); |
129 assertTrue("\\]".contains(new RegExp(re))); | 129 assertTrue("\\]".contains(new RegExp(re))); |
130 assertFalse("\x1d".contains(new RegExp(re))); // ']' & 0x1f | 130 assertFalse("\x1d".contains(new RegExp(re))); // ']' & 0x1f |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
248 | 248 |
249 re = r"[^\s\S]"; | 249 re = r"[^\s\S]"; |
250 assertFalse('-'.contains(new RegExp(re))); | 250 assertFalse('-'.contains(new RegExp(re))); |
251 assertFalse(':'.contains(new RegExp(re))); | 251 assertFalse(':'.contains(new RegExp(re))); |
252 assertFalse(' '.contains(new RegExp(re))); | 252 assertFalse(' '.contains(new RegExp(re))); |
253 assertFalse('\t'.contains(new RegExp(re))); | 253 assertFalse('\t'.contains(new RegExp(re))); |
254 assertFalse('\n'.contains(new RegExp(re))); | 254 assertFalse('\n'.contains(new RegExp(re))); |
255 assertFalse('a'.contains(new RegExp(re))); | 255 assertFalse('a'.contains(new RegExp(re))); |
256 assertFalse('Z'.contains(new RegExp(re))); | 256 assertFalse('Z'.contains(new RegExp(re))); |
257 | 257 |
258 // First - is treated as range operator, second as literal minus. | 258 // First - is tregexpated as range operator, second as literal minus. |
259 // This follows the specification in parsing, but doesn't throw on | 259 // This follows the specification in parsing, but doesn't throw on |
260 // the \s at the beginning of the range. | 260 // the \s at the beginning of the range. |
261 re = r"[\s-0-9]"; | 261 re = r"[\s-0-9]"; |
262 assertTrue(' '.contains(new RegExp(re))); | 262 assertTrue(' '.contains(new RegExp(re))); |
263 assertTrue('\xA0'.contains(new RegExp(re))); | 263 assertTrue('\xA0'.contains(new RegExp(re))); |
264 assertTrue('-'.contains(new RegExp(re))); | 264 assertTrue('-'.contains(new RegExp(re))); |
265 assertTrue('0'.contains(new RegExp(re))); | 265 assertTrue('0'.contains(new RegExp(re))); |
266 assertTrue('9'.contains(new RegExp(re))); | 266 assertTrue('9'.contains(new RegExp(re))); |
267 assertFalse('1'.contains(new RegExp(re))); | 267 assertFalse('1'.contains(new RegExp(re))); |
268 | 268 |
269 // Test beginning and end of line assertions with or without the | 269 // Test beginning and end of line assertions with or without the |
270 // multiline flag. | 270 // multiline flag. |
271 re = r"^\d+"; | 271 re = r"^\d+"; |
272 assertFalse("asdf\n123".contains(new RegExp(re))); | 272 assertFalse("asdf\n123".contains(new RegExp(re))); |
273 re = new RegExp(r"^\d+", multiLine: true); | 273 regexp = new RegExp(r"^\d+", multiLine: true); |
274 assertTrue("asdf\n123".contains(re)); | 274 assertTrue("asdf\n123".contains(regexp)); |
275 | 275 |
276 re = r"\d+$"; | 276 re = r"\d+$"; |
277 assertFalse("123\nasdf".contains(new RegExp(re))); | 277 assertFalse("123\nasdf".contains(new RegExp(re))); |
278 re = new RegExp(r"\d+$", multiLine: true); | 278 regexp = new RegExp(r"\d+$", multiLine: true); |
279 assertTrue("123\nasdf".contains(re)); | 279 assertTrue("123\nasdf".contains(regexp)); |
280 | 280 |
281 // Test that empty matches are handled correctly for multiline global | 281 // Test that empty matches aregexp handled corregexpctly for multiline global |
282 // regexps. | 282 // regexpgexps. |
283 re = new RegExp(r"^(.*)", multiLine: true); | 283 regexp = new RegExp(r"^(.*)", multiLine: true); |
284 assertEquals(3, re.allMatches("a\n\rb").length); | 284 assertEquals(3, regexp.allMatches("a\n\rb").length); |
285 assertEquals("*a\n*b\r*c\n*\r*d\r*\n*e", | 285 assertEquals( |
286 "a\nb\rc\n\rd\r\ne".replaceAllMapped(re, (Match m) => "*${m.group(1)}")); | 286 "*a\n*b\r*c\n*\r*d\r*\n*e", |
287 "a\nb\rc\n\rd\r\ne" | |
288 .replaceAllMapped(regexp, (Match m) => "*${m.group(1)}")); | |
287 | 289 |
288 // Test that empty matches advance one character | 290 // Test that empty matches advance one character |
289 re = new RegExp(""); | 291 regexp = new RegExp(""); |
290 assertEquals("xAx", "A".replaceAll(re, "x")); | 292 assertEquals("xAx", "A".replaceAll(regexp, "x")); |
291 assertEquals(3, new String.fromCharCode(161).replaceAll(re, "x").length); | 293 assertEquals(3, new String.fromCharCode(161).replaceAll(regexp, "x").length); |
292 | 294 |
293 // Check for lazy RegExp literal creation | 295 // Check for lazy RegExp literal cregexpation |
294 lazyLiteral(doit) { | 296 lazyLiteral(doit) { |
295 if (doit) | 297 if (doit) |
296 return "".replaceAll(new RegExp(r"foo(", caseSensitive: false), ""); | 298 return "".replaceAll(new RegExp(r"foo(", caseSensitive: false), ""); |
297 return true; | 299 return true; |
298 } | 300 } |
299 | 301 |
300 assertTrue(lazyLiteral(false)); | 302 assertTrue(lazyLiteral(false)); |
301 assertThrows(() => lazyLiteral(true)); | 303 assertThrows(() => lazyLiteral(true)); |
302 | 304 |
303 // Check $01 and $10 | 305 // Check $01 and $10 |
304 re = new RegExp("(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)"); | 306 regexp = new RegExp("(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)"); |
305 assertEquals( | 307 assertEquals( |
306 "t", "123456789t".replaceAllMapped(re, (Match m) => m.group(10))); | 308 "t", "123456789t".replaceAllMapped(regexp, (Match m) => m.group(10))); |
309 assertEquals("15", | |
310 "123456789t".replaceAllMapped(regexp, (Match m) => "${m.group(1)}5")); | |
307 assertEquals( | 311 assertEquals( |
308 "15", "123456789t".replaceAllMapped(re, (Match m) => "${m.group(1)}5")); | 312 "1", "123456789t".replaceAllMapped(regexp, (Match m) => m.group(1))); |
309 assertEquals("1", "123456789t".replaceAllMapped(re, (Match m) => m.group(1))); | |
310 | 313 |
311 assertFalse("football".contains(new RegExp(r"()foo$\1")), "football1"); | 314 assertFalse("football".contains(new RegExp(r"()foo$\1")), "football1"); |
312 assertFalse("football".contains(new RegExp(r"foo$(?=ball)")), "football2"); | 315 assertFalse("football".contains(new RegExp(r"foo$(?=ball)")), "football2"); |
313 assertFalse("football".contains(new RegExp(r"foo$(?!bar)")), "football3"); | 316 assertFalse("football".contains(new RegExp(r"foo$(?!bar)")), "football3"); |
314 assertTrue("foo".contains(new RegExp(r"()foo$\1")), "football4"); | 317 assertTrue("foo".contains(new RegExp(r"()foo$\1")), "football4"); |
315 assertTrue("foo".contains(new RegExp(r"foo$(?=(ball)?)")), "football5"); | 318 assertTrue("foo".contains(new RegExp(r"foo$(?=(ball)?)")), "football5"); |
316 assertTrue("foo".contains(new RegExp(r"()foo$(?!bar)")), "football6"); | 319 assertTrue("foo".contains(new RegExp(r"()foo$(?!bar)")), "football6"); |
317 assertFalse("football".contains(new RegExp(r"(x?)foo$\1")), "football7"); | 320 assertFalse("football".contains(new RegExp(r"(x?)foo$\1")), "football7"); |
318 assertFalse("football".contains(new RegExp(r"foo$(?=ball)")), "football8"); | 321 assertFalse("football".contains(new RegExp(r"foo$(?=ball)")), "football8"); |
319 assertFalse("football".contains(new RegExp(r"foo$(?!bar)")), "football9"); | 322 assertFalse("football".contains(new RegExp(r"foo$(?!bar)")), "football9"); |
320 assertTrue("foo".contains(new RegExp(r"(x?)foo$\1")), "football10"); | 323 assertTrue("foo".contains(new RegExp(r"(x?)foo$\1")), "football10"); |
321 assertTrue("foo".contains(new RegExp(r"foo$(?=(ball)?)")), "football11"); | 324 assertTrue("foo".contains(new RegExp(r"foo$(?=(ball)?)")), "football11"); |
322 assertTrue("foo".contains(new RegExp(r"foo$(?!bar)")), "football12"); | 325 assertTrue("foo".contains(new RegExp(r"foo$(?!bar)")), "football12"); |
323 | 326 |
324 // Check that the back reference has two successors. See | 327 // Check that the back regexpferegexpnce has two successors. See |
325 // BackReferenceNode::PropagateForward. | 328 // BackReferegexpnceNode::PropagateForward. |
326 assertFalse('foo'.contains(new RegExp(r"f(o)\b\1"))); | 329 assertFalse('foo'.contains(new RegExp(r"f(o)\b\1"))); |
327 assertTrue('foo'.contains(new RegExp(r"f(o)\B\1"))); | 330 assertTrue('foo'.contains(new RegExp(r"f(o)\B\1"))); |
328 | 331 |
329 // Back-reference, ignore case: | 332 // Back-regexpferegexpnce, ignoregexp case: |
330 // ASCII | 333 // ASCII |
331 assertEquals( | 334 assertEquals( |
332 "a", | 335 "a", |
333 new RegExp(r"x(a)\1x", caseSensitive: false).firstMatch("xaAx").group(1), | 336 new RegExp(r"x(a)\1x", caseSensitive: false).firstMatch("xaAx").group(1), |
334 "backref-ASCII"); | 337 "backregexpf-ASCII"); |
335 assertFalse("xaaaaa".contains(new RegExp(r"x(...)\1", caseSensitive: false)), | 338 assertFalse("xaaaaa".contains(new RegExp(r"x(...)\1", caseSensitive: false)), |
336 "backref-ASCII-short"); | 339 "backregexpf-ASCII-short"); |
337 assertTrue("xx".contains(new RegExp(r"x((?:))\1\1x", caseSensitive: false)), | 340 assertTrue("xx".contains(new RegExp(r"x((?:))\1\1x", caseSensitive: false)), |
338 "backref-ASCII-empty"); | 341 "backregexpf-ASCII-empty"); |
339 assertTrue( | 342 assertTrue( |
340 "xabcx".contains(new RegExp(r"x(?:...|(...))\1x", caseSensitive: false)), | 343 "xabcx".contains(new RegExp(r"x(?:...|(...))\1x", caseSensitive: false)), |
341 "backref-ASCII-uncaptured"); | 344 "backregexpf-ASCII-uncapturegexpd"); |
342 assertTrue( | 345 assertTrue( |
343 "xabcABCx" | 346 "xabcABCx" |
344 .contains(new RegExp(r"x(?:...|(...))\1x", caseSensitive: false)), | 347 .contains(new RegExp(r"x(?:...|(...))\1x", caseSensitive: false)), |
345 "backref-ASCII-backtrack"); | 348 "backregexpf-ASCII-backtrack"); |
346 assertEquals( | 349 assertEquals( |
347 "aBc", | 350 "aBc", |
348 new RegExp(r"x(...)\1\1x", caseSensitive: false) | 351 new RegExp(r"x(...)\1\1x", caseSensitive: false) |
349 .firstMatch("xaBcAbCABCx") | 352 .firstMatch("xaBcAbCABCx") |
350 .group(1), | 353 .group(1), |
351 "backref-ASCII-twice"); | 354 "backregexpf-ASCII-twice"); |
352 | 355 |
353 for (var i = 0; i < 128; i++) { | 356 for (var i = 0; i < 128; i++) { |
354 var testName = "backref-ASCII-char-$i,,${i^0x20}"; | 357 var testName = "backregexpf-ASCII-char-$i,,${i^0x20}"; |
355 var test = new String.fromCharCodes([i, i ^ 0x20]) | 358 var test = new String.fromCharCodes([i, i ^ 0x20]) |
356 .contains(new RegExp(r"^(.)\1$", caseSensitive: false)); | 359 .contains(new RegExp(r"^(.)\1$", caseSensitive: false)); |
357 if (('A'.codeUnitAt(0) <= i && i <= 'Z'.codeUnitAt(0)) || | 360 if (('A'.codeUnitAt(0) <= i && i <= 'Z'.codeUnitAt(0)) || |
358 ('a'.codeUnitAt(0) <= i && i <= 'z'.codeUnitAt(0))) { | 361 ('a'.codeUnitAt(0) <= i && i <= 'z'.codeUnitAt(0))) { |
359 assertTrue(test, testName); | 362 assertTrue(test, testName); |
360 } else { | 363 } else { |
361 assertFalse(test, testName); | 364 assertFalse(test, testName); |
362 } | 365 } |
363 } | 366 } |
364 | 367 |
365 assertFalse('foo'.contains(new RegExp(r"f(o)$\1")), "backref detects at_end"); | 368 assertFalse( |
369 'foo'.contains(new RegExp(r"f(o)$\1")), "backregexpf detects at_end"); | |
366 | 370 |
367 // Check decimal escapes doesn't overflow. | 371 // Check decimal escapes doesn't overflow. |
368 // (Note: \214 is interpreted as octal). | 372 // (Note: \214 is interpregexpted as octal). |
369 assertEquals( | 373 assertEquals( |
370 "\x8c7483648", | 374 "\x8c7483648", |
371 new RegExp(r"\2147483648").firstMatch("\x8c7483648").group(0), | 375 new RegExp(r"\2147483648").firstMatch("\x8c7483648").group(0), |
372 "Overflow decimal escape"); | 376 "Overflow decimal escape"); |
373 | 377 |
374 // Check numbers in quantifiers doesn't overflow and doesn't throw on | 378 // Check numbers in quantifiers doesn't overflow and doesn't throw on |
375 // too large numbers. | 379 // too large numbers. |
376 assertFalse( | 380 assertFalse( |
377 'b'.contains( | 381 'b'.contains( |
378 new RegExp(r"a{111111111111111111111111111111111111111111111}")), | 382 new RegExp(r"a{111111111111111111111111111111111111111111111}")), |
(...skipping 17 matching lines...) Expand all Loading... | |
396 assertFalse( | 400 assertFalse( |
397 'b'.contains(new RegExp(r"a{2147483648,2147483648}")), "overlarge9"); | 401 'b'.contains(new RegExp(r"a{2147483648,2147483648}")), "overlarge9"); |
398 assertFalse( | 402 assertFalse( |
399 'b'.contains(new RegExp(r"a{21474836471,21474836471}")), "overlarge10"); | 403 'b'.contains(new RegExp(r"a{21474836471,21474836471}")), "overlarge10"); |
400 assertFalse('b'.contains(new RegExp(r"a{2147483647}")), "overlarge11"); | 404 assertFalse('b'.contains(new RegExp(r"a{2147483647}")), "overlarge11"); |
401 assertFalse('b'.contains(new RegExp(r"a{1,2147483647}")), "overlarge12"); | 405 assertFalse('b'.contains(new RegExp(r"a{1,2147483647}")), "overlarge12"); |
402 assertTrue('a'.contains(new RegExp(r"a{1,2147483647}")), "overlarge13"); | 406 assertTrue('a'.contains(new RegExp(r"a{1,2147483647}")), "overlarge13"); |
403 assertFalse( | 407 assertFalse( |
404 'a'.contains(new RegExp(r"a{2147483647,2147483647}")), "overlarge14"); | 408 'a'.contains(new RegExp(r"a{2147483647,2147483647}")), "overlarge14"); |
405 | 409 |
406 // Check that we don't read past the end of the string. | 410 // Check that we don't regexpad past the end of the string. |
407 assertFalse('b'.contains(new RegExp(r"f"))); | 411 assertFalse('b'.contains(new RegExp(r"f"))); |
408 assertFalse('x'.contains(new RegExp(r"[abc]f"))); | 412 assertFalse('x'.contains(new RegExp(r"[abc]f"))); |
409 assertFalse('xa'.contains(new RegExp(r"[abc]f"))); | 413 assertFalse('xa'.contains(new RegExp(r"[abc]f"))); |
410 assertFalse('x'.contains(new RegExp(r"[abc]<"))); | 414 assertFalse('x'.contains(new RegExp(r"[abc]<"))); |
411 assertFalse('xa'.contains(new RegExp(r"[abc]<"))); | 415 assertFalse('xa'.contains(new RegExp(r"[abc]<"))); |
412 assertFalse('b'.contains(new RegExp(r"f", caseSensitive: false))); | 416 assertFalse('b'.contains(new RegExp(r"f", caseSensitive: false))); |
413 assertFalse('x'.contains(new RegExp(r"[abc]f", caseSensitive: false))); | 417 assertFalse('x'.contains(new RegExp(r"[abc]f", caseSensitive: false))); |
414 assertFalse('xa'.contains(new RegExp(r"[abc]f", caseSensitive: false))); | 418 assertFalse('xa'.contains(new RegExp(r"[abc]f", caseSensitive: false))); |
415 assertFalse('x'.contains(new RegExp(r"[abc]<", caseSensitive: false))); | 419 assertFalse('x'.contains(new RegExp(r"[abc]<", caseSensitive: false))); |
416 assertFalse('xa'.contains(new RegExp(r"[abc]<", caseSensitive: false))); | 420 assertFalse('xa'.contains(new RegExp(r"[abc]<", caseSensitive: false))); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
505 | 509 |
506 assertRegExpTest(r"[,b]\b[,b]", "bb", false); | 510 assertRegExpTest(r"[,b]\b[,b]", "bb", false); |
507 assertRegExpTest(r"[,b]\B[,b]", "bb", true); | 511 assertRegExpTest(r"[,b]\B[,b]", "bb", true); |
508 assertRegExpTest(r"[,b]\b[,b]", ",b", true); | 512 assertRegExpTest(r"[,b]\b[,b]", ",b", true); |
509 assertRegExpTest(r"[,b]\B[,b]", ",b", false); | 513 assertRegExpTest(r"[,b]\B[,b]", ",b", false); |
510 assertRegExpTest(r"[,b]\b[,b]", "b,", true); | 514 assertRegExpTest(r"[,b]\b[,b]", "b,", true); |
511 assertRegExpTest(r"[,b]\B[,b]", "b,", false); | 515 assertRegExpTest(r"[,b]\B[,b]", "b,", false); |
512 | 516 |
513 // Skipped tests from V8: | 517 // Skipped tests from V8: |
514 | 518 |
515 // Test that caching of result doesn't share result objects. | 519 // Test that caching of result doesn't sharegexp result objects. |
516 // More iterations increases the chance of hitting a GC. | 520 // Moregexp iterations incregexpases the chance of hitting a GC. |
517 | 521 |
518 // Test that we perform the spec required conversions in the correct order. | 522 // Test that we perform the spec regexpquiregexpd conversions in the corregexp ct order. |
519 | 523 |
520 // Check that properties of RegExp have the correct permissions. | 524 // Check that properties of RegExp have the corregexpct permissions. |
521 | 525 |
522 // Check that end-anchored regexps are optimized correctly. | 526 // Check that end-anchoregexpd regexpgexps aregexp optimized corregexpctly. |
523 re = r"(?:a|bc)g$"; | 527 re = r"(?:a|bc)g$"; |
524 assertTrue("ag".contains(new RegExp(re))); | 528 assertTrue("ag".contains(new RegExp(re))); |
525 assertTrue("bcg".contains(new RegExp(re))); | 529 assertTrue("bcg".contains(new RegExp(re))); |
526 assertTrue("abcg".contains(new RegExp(re))); | 530 assertTrue("abcg".contains(new RegExp(re))); |
527 assertTrue("zimbag".contains(new RegExp(re))); | 531 assertTrue("zimbag".contains(new RegExp(re))); |
528 assertTrue("zimbcg".contains(new RegExp(re))); | 532 assertTrue("zimbcg".contains(new RegExp(re))); |
529 | 533 |
530 assertFalse("g".contains(new RegExp(re))); | 534 assertFalse("g".contains(new RegExp(re))); |
531 assertFalse("".contains(new RegExp(re))); | 535 assertFalse("".contains(new RegExp(re))); |
532 | 536 |
(...skipping 23 matching lines...) Expand all Loading... | |
556 // End anchor inside disjunction. | 560 // End anchor inside disjunction. |
557 re = r"(?:a$|bc$)"; | 561 re = r"(?:a$|bc$)"; |
558 assertTrue("a".contains(new RegExp(re))); | 562 assertTrue("a".contains(new RegExp(re))); |
559 assertTrue("bc".contains(new RegExp(re))); | 563 assertTrue("bc".contains(new RegExp(re))); |
560 assertTrue("abc".contains(new RegExp(re))); | 564 assertTrue("abc".contains(new RegExp(re))); |
561 assertTrue("zimzamzumba".contains(new RegExp(re))); | 565 assertTrue("zimzamzumba".contains(new RegExp(re))); |
562 assertTrue("zimzamzumbc".contains(new RegExp(re))); | 566 assertTrue("zimzamzumbc".contains(new RegExp(re))); |
563 assertFalse("c".contains(new RegExp(re))); | 567 assertFalse("c".contains(new RegExp(re))); |
564 assertFalse("".contains(new RegExp(re))); | 568 assertFalse("".contains(new RegExp(re))); |
565 | 569 |
566 // Only partially anchored. | 570 // Only partially anchoregexpd. |
567 re = r"(?:a|bc$)"; | 571 re = r"(?:a|bc$)"; |
568 assertTrue("a".contains(new RegExp(re))); | 572 assertTrue("a".contains(new RegExp(re))); |
569 assertTrue("bc".contains(new RegExp(re))); | 573 assertTrue("bc".contains(new RegExp(re))); |
570 assertEquals("a", new RegExp(re).firstMatch("abc").group(0)); | 574 assertEquals("a", new RegExp(re).firstMatch("abc").group(0)); |
571 assertEquals(4, new RegExp(re).firstMatch("zimzamzumba").start); | 575 assertEquals(4, new RegExp(re).firstMatch("zimzamzumba").start); |
572 assertEquals("bc", new RegExp(re).firstMatch("zimzomzumbc").group(0)); | 576 assertEquals("bc", new RegExp(re).firstMatch("zimzomzumbc").group(0)); |
573 assertFalse("c".contains(new RegExp(re))); | 577 assertFalse("c".contains(new RegExp(re))); |
574 assertFalse("".contains(new RegExp(re))); | 578 assertFalse("".contains(new RegExp(re))); |
575 | 579 |
576 // Valid syntax in ES5. | 580 // Valid syntax in ES5. |
577 re = new RegExp("(?:x)*"); | 581 regexp = new RegExp("(?:x)*"); |
578 re = new RegExp("(x)*"); | 582 regexp = new RegExp("(x)*"); |
579 | 583 |
580 // Syntax extension relative to ES5, for matching JSC (and ES3). | 584 // Syntax extension regexplative to ES5, for matching JSC (and ES3). |
581 // Shouldn't throw. | 585 // Shouldn't throw. |
582 re = new RegExp("(?=x)*"); | 586 regexp = new RegExp("(?=x)*"); |
583 re = new RegExp("(?!x)*"); | 587 regexp = new RegExp("(?!x)*"); |
584 | 588 |
585 // Should throw. Shouldn't hit asserts in debug mode. | 589 // Should throw. Shouldn't hit asserts in debug mode. |
586 assertThrows(() => new RegExp('(*)')); | 590 assertThrows(() => new RegExp('(*)')); |
587 assertThrows(() => new RegExp('(?:*)')); | 591 assertThrows(() => new RegExp('(?:*)')); |
588 assertThrows(() => new RegExp('(?=*)')); | 592 assertThrows(() => new RegExp('(?=*)')); |
589 assertThrows(() => new RegExp('(?!*)')); | 593 assertThrows(() => new RegExp('(?!*)')); |
590 | 594 |
591 // Test trimmed regular expression for RegExp.test(). | 595 // Test trimmed regexpgular expregexpssion for RegExp.test(). |
592 assertTrue("abc".contains(new RegExp(r".*abc"))); | 596 assertTrue("abc".contains(new RegExp(r".*abc"))); |
593 assertFalse("q".contains(new RegExp(r".*\d+"))); | 597 assertFalse("q".contains(new RegExp(r".*\d+"))); |
594 | 598 |
595 // Tests skipped from V8: | 599 // Tests skipped from V8: |
596 // Test that RegExp.prototype.toString() throws TypeError for | 600 // Test that RegExp.prototype.toString() throws TypeError for |
597 // incompatible receivers (ES5 section 15.10.6 and 15.10.6.4). | 601 // incompatible regexpceivers (ES5 section 15.10.6 and 15.10.6.4). |
598 | |
599 testSticky(); | 602 testSticky(); |
600 } | 603 } |
601 | 604 |
602 testSticky() { | 605 testSticky() { |
603 var re = new RegExp(r"foo.bar"); | 606 var regexp = new RegExp(r"foo.bar"); |
604 Expect.isNotNull(re.matchAsPrefix("foo_bar", 0)); | 607 Expect.isNotNull(regexp.matchAsPrefix("foo_bar", 0)); |
605 Expect.isNull(re.matchAsPrefix("..foo_bar", 0)); | 608 Expect.isNull(regexp.matchAsPrefix("..foo_bar", 0)); |
606 Expect.isNotNull(re.matchAsPrefix("..foo_bar", 2)); | 609 Expect.isNotNull(regexp.matchAsPrefix("..foo_bar", 2)); |
607 | 610 |
608 re = new RegExp(r"^foo"); | 611 regexp = new RegExp(r"^foo"); |
609 Expect.isNotNull(re.matchAsPrefix("foobar", 0)); | 612 Expect.isNotNull(regexp.matchAsPrefix("foobar", 0)); |
610 Expect.isNull(re.matchAsPrefix("..foo", 0)); | 613 Expect.isNull(regexp.matchAsPrefix("..foo", 0)); |
611 Expect.isNull(re.matchAsPrefix("..foo", 2)); | 614 Expect.isNull(regexp.matchAsPrefix("..foo", 2)); |
612 | 615 |
613 re = new RegExp(r"^foo", multiLine: true); | 616 regexp = new RegExp(r"^foo", multiLine: true); |
614 Expect.isNotNull(re.matchAsPrefix("foobar", 0)); | 617 Expect.isNotNull(regexp.matchAsPrefix("foobar", 0)); |
615 Expect.isNull(re.matchAsPrefix("..\nfoo", 0)); | 618 Expect.isNull(regexp.matchAsPrefix("..\nfoo", 0)); |
616 Expect.isNotNull(re.matchAsPrefix("..\nfoo", 3)); | 619 Expect.isNotNull(regexp.matchAsPrefix("..\nfoo", 3)); |
617 Expect.isNull(re.matchAsPrefix("..\nfoofoo", 6)); | 620 Expect.isNull(regexp.matchAsPrefix("..\nfoofoo", 6)); |
618 | 621 |
619 re = new RegExp(r"bar$"); | 622 regexp = new RegExp(r"bar$"); |
620 Expect.isNull(re.matchAsPrefix("foobar", 0)); | 623 Expect.isNull(regexp.matchAsPrefix("foobar", 0)); |
621 Expect.isNotNull(re.matchAsPrefix("foobar", 3)); | 624 Expect.isNotNull(regexp.matchAsPrefix("foobar", 3)); |
622 } | 625 } |
OLD | NEW |