OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart 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 |
| 4 // modification, are permitted provided that the following conditions are |
| 5 // met: |
| 6 // |
| 7 // * Redistributions of source code must retain the above copyright |
| 8 // notice, this list of conditions and the following disclaimer. |
| 9 // * Redistributions in binary form must reproduce the above |
| 10 // copyright notice, this list of conditions and the following |
| 11 // disclaimer in the documentation and/or other materials provided |
| 12 // with the distribution. |
| 13 // * Neither the name of Google Inc. nor the names of its |
| 14 // contributors may be used to endorse or promote products derived |
| 15 // from this software without specific prior written permission. |
| 16 // |
| 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 28 |
| 29 import "package:expect/expect.dart"; |
| 30 |
| 31 void testEscape(str, regex) { |
| 32 assertEquals("foo:bar:baz", str.split(regex).join(":")); |
| 33 } |
| 34 |
| 35 void assertEquals(actual, expected, [message]) => |
| 36 Expect.equals(actual, expected, message); |
| 37 void assertTrue(actual, [message]) => Expect.isTrue(actual, message); |
| 38 void assertFalse(actual, [message]) => Expect.isFalse(actual, message); |
| 39 void assertThrows(fn) => Expect.throws(fn); |
| 40 |
| 41 void main() { |
| 42 testEscape("foo\nbar\nbaz", new RegExp(r"\n")); |
| 43 testEscape("foo bar baz", new RegExp(r"\s")); |
| 44 testEscape("foo\tbar\tbaz", new RegExp(r"\s")); |
| 45 testEscape("foo-bar-baz", new RegExp(r"\u002D")); |
| 46 |
| 47 // Test containing null char in regexp. |
| 48 var s = '[' + new String.fromCharCode(0) + ']'; |
| 49 var re = new RegExp(s); |
| 50 assertEquals(re.allMatches(s).length, 1); |
| 51 assertEquals(re.stringMatch(s), new String.fromCharCode(0)); |
| 52 |
| 53 final _vmFrame = new RegExp( |
| 54 r'^#\d+\s+(\S.*) \((.+?):(\d+)(?::(\d+))?\)$'); |
| 55 final _traceLine = |
| 56 "#0 Trace.Trace.parse (package:stack_trace/src/trace.dart:130:7)"; |
| 57 Expect.equals(_vmFrame.firstMatch(_traceLine).group(0), _traceLine); |
| 58 |
| 59 // Test the UTF16 case insensitive comparison. |
| 60 re = new RegExp(r"x(a)\1x", caseSensitive: false); |
| 61 Expect.equals(re.firstMatch("xaAx\u1234").group(0), "xaAx"); |
| 62 |
| 63 // Test strings containing all line separators |
| 64 s = 'aA\nbB\rcC\r\ndD\u2028eE\u2029fF'; |
| 65 // any non-newline character at the beginning of a line |
| 66 re = new RegExp(r"^.", multiLine: true); |
| 67 var result = re.allMatches(s).toList(); |
| 68 assertEquals(result.length, 6); |
| 69 assertEquals(result[0][0], 'a'); |
| 70 assertEquals(result[1][0], 'b'); |
| 71 assertEquals(result[2][0], 'c'); |
| 72 assertEquals(result[3][0], 'd'); |
| 73 assertEquals(result[4][0], 'e'); |
| 74 assertEquals(result[5][0], 'f'); |
| 75 |
| 76 // any non-newline character at the end of a line |
| 77 re = new RegExp(r".$", multiLine: true); |
| 78 result = re.allMatches(s).toList(); |
| 79 assertEquals(result.length, 6); |
| 80 assertEquals(result[0][0], 'A'); |
| 81 assertEquals(result[1][0], 'B'); |
| 82 assertEquals(result[2][0], 'C'); |
| 83 assertEquals(result[3][0], 'D'); |
| 84 assertEquals(result[4][0], 'E'); |
| 85 assertEquals(result[5][0], 'F'); |
| 86 |
| 87 // *any* character at the beginning of a line |
| 88 re = new RegExp(r"^[^]", multiLine: true); |
| 89 result = re.allMatches(s).toList(); |
| 90 assertEquals(result.length, 7); |
| 91 assertEquals(result[0][0], 'a'); |
| 92 assertEquals(result[1][0], 'b'); |
| 93 assertEquals(result[2][0], 'c'); |
| 94 assertEquals(result[3][0], '\n'); |
| 95 assertEquals(result[4][0], 'd'); |
| 96 assertEquals(result[5][0], 'e'); |
| 97 assertEquals(result[6][0], 'f'); |
| 98 |
| 99 // *any* character at the end of a line |
| 100 re = new RegExp(r"[^]$", multiLine: true); |
| 101 result = re.allMatches(s).toList(); |
| 102 assertEquals(result.length, 7); |
| 103 assertEquals(result[0][0], 'A'); |
| 104 assertEquals(result[1][0], 'B'); |
| 105 assertEquals(result[2][0], 'C'); |
| 106 assertEquals(result[3][0], '\r'); |
| 107 assertEquals(result[4][0], 'D'); |
| 108 assertEquals(result[5][0], 'E'); |
| 109 assertEquals(result[6][0], 'F'); |
| 110 |
| 111 // Some tests from the Mozilla tests, where our behavior used to differ |
| 112 // from SpiderMonkey. |
| 113 // From ecma_3/RegExp/regress-334158.js |
| 114 assertTrue( "\x01" .contains(new RegExp(r"\ca"))); |
| 115 assertFalse( "\\ca" .contains(new RegExp(r"\ca"))); |
| 116 assertFalse( "ca" .contains(new RegExp(r"\ca"))); |
| 117 assertTrue( "\\ca" .contains(new RegExp(r"\c[a/]"))); |
| 118 assertTrue( "\\c/" .contains(new RegExp(r"\c[a/]"))); |
| 119 |
| 120 // Test \c in character class |
| 121 re = r"^[\cM]$"; |
| 122 assertTrue("\r".contains(new RegExp(re))); |
| 123 assertFalse("M".contains(new RegExp(re))); |
| 124 assertFalse("c".contains(new RegExp(re))); |
| 125 assertFalse("\\".contains(new RegExp(re))); |
| 126 assertFalse("\x03".contains(new RegExp(re))); // I.e., read as \cc |
| 127 |
| 128 re = r"^[\c]]$"; |
| 129 assertTrue("c]".contains(new RegExp(re))); |
| 130 assertTrue("\\]".contains(new RegExp(re))); |
| 131 assertFalse("\x1d".contains(new RegExp(re))); // ']' & 0x1f |
| 132 assertFalse("\x03]".contains(new RegExp(re))); // I.e., read as \cc |
| 133 |
| 134 // Digit control characters are masked in character classes. |
| 135 re = r"^[\c1]$"; |
| 136 assertTrue("\x11".contains(new RegExp(re))); |
| 137 assertFalse("\\".contains(new RegExp(re))); |
| 138 assertFalse("c".contains(new RegExp(re))); |
| 139 assertFalse("1".contains(new RegExp(re))); |
| 140 |
| 141 // Underscore control character is masked in character classes. |
| 142 re = r"^[\c_]$"; |
| 143 assertTrue("\x1f".contains(new RegExp(re))); |
| 144 assertFalse("\\".contains(new RegExp(re))); |
| 145 assertFalse("c".contains(new RegExp(re))); |
| 146 assertFalse("_".contains(new RegExp(re))); |
| 147 |
| 148 re = r"^[\c$]$"; // Other characters are interpreted literally. |
| 149 assertFalse("\x04".contains(new RegExp(re))); |
| 150 assertTrue("\\".contains(new RegExp(re))); |
| 151 assertTrue("c".contains(new RegExp(re))); |
| 152 assertTrue(r"$".contains(new RegExp(re))); |
| 153 |
| 154 assertTrue("Z[\\cde".contains(new RegExp(r"^[Z-\c-e]*$"))); |
| 155 |
| 156 // Test that we handle \s and \S correctly on special Unicode characters. |
| 157 re = r"\s"; |
| 158 assertTrue("\u2028".contains(new RegExp(re))); |
| 159 assertTrue("\u2029".contains(new RegExp(re))); |
| 160 assertTrue("\uFEFF".contains(new RegExp(re))); |
| 161 |
| 162 re = r"\S"; |
| 163 assertFalse("\u2028".contains(new RegExp(re))); |
| 164 assertFalse("\u2029".contains(new RegExp(re))); |
| 165 assertFalse("\uFEFF".contains(new RegExp(re))); |
| 166 |
| 167 // Test that we handle \s and \S correctly inside some bizarre |
| 168 // character classes. |
| 169 re = r"[\s-:]"; |
| 170 assertTrue('-'.contains(new RegExp(re))); |
| 171 assertTrue(':'.contains(new RegExp(re))); |
| 172 assertTrue(' '.contains(new RegExp(re))); |
| 173 assertTrue('\t'.contains(new RegExp(re))); |
| 174 assertTrue('\n'.contains(new RegExp(re))); |
| 175 assertFalse('a'.contains(new RegExp(re))); |
| 176 assertFalse('Z'.contains(new RegExp(re))); |
| 177 |
| 178 re = r"[\S-:]"; |
| 179 assertTrue('-'.contains(new RegExp(re))); |
| 180 assertTrue(':'.contains(new RegExp(re))); |
| 181 assertFalse(' '.contains(new RegExp(re))); |
| 182 assertFalse('\t'.contains(new RegExp(re))); |
| 183 assertFalse('\n'.contains(new RegExp(re))); |
| 184 assertTrue('a'.contains(new RegExp(re))); |
| 185 assertTrue('Z'.contains(new RegExp(re))); |
| 186 |
| 187 re = r"[^\s-:]"; |
| 188 assertFalse('-'.contains(new RegExp(re))); |
| 189 assertFalse(':'.contains(new RegExp(re))); |
| 190 assertFalse(' '.contains(new RegExp(re))); |
| 191 assertFalse('\t'.contains(new RegExp(re))); |
| 192 assertFalse('\n'.contains(new RegExp(re))); |
| 193 assertTrue('a'.contains(new RegExp(re))); |
| 194 assertTrue('Z'.contains(new RegExp(re))); |
| 195 |
| 196 re = r"[^\S-:]"; |
| 197 assertFalse('-'.contains(new RegExp(re))); |
| 198 assertFalse(':'.contains(new RegExp(re))); |
| 199 assertTrue(' '.contains(new RegExp(re))); |
| 200 assertTrue('\t'.contains(new RegExp(re))); |
| 201 assertTrue('\n'.contains(new RegExp(re))); |
| 202 assertFalse('a'.contains(new RegExp(re))); |
| 203 assertFalse('Z'.contains(new RegExp(re))); |
| 204 |
| 205 re = r"[\s]"; |
| 206 assertFalse('-'.contains(new RegExp(re))); |
| 207 assertFalse(':'.contains(new RegExp(re))); |
| 208 assertTrue(' '.contains(new RegExp(re))); |
| 209 assertTrue('\t'.contains(new RegExp(re))); |
| 210 assertTrue('\n'.contains(new RegExp(re))); |
| 211 assertFalse('a'.contains(new RegExp(re))); |
| 212 assertFalse('Z'.contains(new RegExp(re))); |
| 213 |
| 214 re = r"[^\s]"; |
| 215 assertTrue('-'.contains(new RegExp(re))); |
| 216 assertTrue(':'.contains(new RegExp(re))); |
| 217 assertFalse(' '.contains(new RegExp(re))); |
| 218 assertFalse('\t'.contains(new RegExp(re))); |
| 219 assertFalse('\n'.contains(new RegExp(re))); |
| 220 assertTrue('a'.contains(new RegExp(re))); |
| 221 assertTrue('Z'.contains(new RegExp(re))); |
| 222 |
| 223 re = r"[\S]"; |
| 224 assertTrue('-'.contains(new RegExp(re))); |
| 225 assertTrue(':'.contains(new RegExp(re))); |
| 226 assertFalse(' '.contains(new RegExp(re))); |
| 227 assertFalse('\t'.contains(new RegExp(re))); |
| 228 assertFalse('\n'.contains(new RegExp(re))); |
| 229 assertTrue('a'.contains(new RegExp(re))); |
| 230 assertTrue('Z'.contains(new RegExp(re))); |
| 231 |
| 232 re = r"[^\S]"; |
| 233 assertFalse('-'.contains(new RegExp(re))); |
| 234 assertFalse(':'.contains(new RegExp(re))); |
| 235 assertTrue(' '.contains(new RegExp(re))); |
| 236 assertTrue('\t'.contains(new RegExp(re))); |
| 237 assertTrue('\n'.contains(new RegExp(re))); |
| 238 assertFalse('a'.contains(new RegExp(re))); |
| 239 assertFalse('Z'.contains(new RegExp(re))); |
| 240 |
| 241 re = r"[\s\S]"; |
| 242 assertTrue('-'.contains(new RegExp(re))); |
| 243 assertTrue(':'.contains(new RegExp(re))); |
| 244 assertTrue(' '.contains(new RegExp(re))); |
| 245 assertTrue('\t'.contains(new RegExp(re))); |
| 246 assertTrue('\n'.contains(new RegExp(re))); |
| 247 assertTrue('a'.contains(new RegExp(re))); |
| 248 assertTrue('Z'.contains(new RegExp(re))); |
| 249 |
| 250 re = r"[^\s\S]"; |
| 251 assertFalse('-'.contains(new RegExp(re))); |
| 252 assertFalse(':'.contains(new RegExp(re))); |
| 253 assertFalse(' '.contains(new RegExp(re))); |
| 254 assertFalse('\t'.contains(new RegExp(re))); |
| 255 assertFalse('\n'.contains(new RegExp(re))); |
| 256 assertFalse('a'.contains(new RegExp(re))); |
| 257 assertFalse('Z'.contains(new RegExp(re))); |
| 258 |
| 259 // First - is treated as range operator, second as literal minus. |
| 260 // This follows the specification in parsing, but doesn't throw on |
| 261 // the \s at the beginning of the range. |
| 262 re = r"[\s-0-9]"; |
| 263 assertTrue(' '.contains(new RegExp(re))); |
| 264 assertTrue('\xA0'.contains(new RegExp(re))); |
| 265 assertTrue('-'.contains(new RegExp(re))); |
| 266 assertTrue('0'.contains(new RegExp(re))); |
| 267 assertTrue('9'.contains(new RegExp(re))); |
| 268 assertFalse('1'.contains(new RegExp(re))); |
| 269 |
| 270 // Test beginning and end of line assertions with or without the |
| 271 // multiline flag. |
| 272 re = r"^\d+"; |
| 273 assertFalse("asdf\n123".contains(new RegExp(re))); |
| 274 re = new RegExp(r"^\d+", multiLine: true); |
| 275 assertTrue("asdf\n123".contains(re)); |
| 276 |
| 277 re = r"\d+$"; |
| 278 assertFalse("123\nasdf".contains(new RegExp(re))); |
| 279 re = new RegExp(r"\d+$", multiLine: true); |
| 280 assertTrue("123\nasdf".contains(re)); |
| 281 |
| 282 // Test that empty matches are handled correctly for multiline global |
| 283 // regexps. |
| 284 re = new RegExp(r"^(.*)", multiLine: true); |
| 285 assertEquals(3, re.allMatches("a\n\rb").length); |
| 286 assertEquals("*a\n*b\r*c\n*\r*d\r*\n*e", |
| 287 "a\nb\rc\n\rd\r\ne".replaceAllMapped( |
| 288 re, (Match m) => "*${m.group(1)}")); |
| 289 |
| 290 // Test that empty matches advance one character |
| 291 re = new RegExp(""); |
| 292 assertEquals("xAx", "A".replaceAll(re, "x")); |
| 293 assertEquals(3, new String.fromCharCode(161).replaceAll(re, "x").length); |
| 294 |
| 295 // Check for lazy RegExp literal creation |
| 296 void lazyLiteral(doit) { |
| 297 if (doit) return "".replaceAll( |
| 298 new RegExp(r"foo(", caseSensitive: false), ""); |
| 299 return true; |
| 300 } |
| 301 |
| 302 assertTrue(lazyLiteral(false)); |
| 303 assertThrows(() => lazyLiteral(true)); |
| 304 |
| 305 // Check $01 and $10 |
| 306 re = new RegExp("(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)"); |
| 307 assertEquals("t", "123456789t".replaceAllMapped( |
| 308 re, (Match m) => m.group(10))); |
| 309 assertEquals("15", "123456789t".replaceAllMapped( |
| 310 re, (Match m) => "${m.group(1)}5")); |
| 311 assertEquals("1", "123456789t".replaceAllMapped( |
| 312 re, (Match m) => m.group(1))); |
| 313 |
| 314 assertFalse("football".contains(new RegExp(r"()foo$\1")), "football1"); |
| 315 assertFalse("football".contains(new RegExp(r"foo$(?=ball)")), "football2"); |
| 316 assertFalse("football".contains(new RegExp(r"foo$(?!bar)")), "football3"); |
| 317 assertTrue("foo".contains(new RegExp(r"()foo$\1")), "football4"); |
| 318 assertTrue("foo".contains(new RegExp(r"foo$(?=(ball)?)")), "football5"); |
| 319 assertTrue("foo".contains(new RegExp(r"()foo$(?!bar)")), "football6"); |
| 320 assertFalse("football".contains(new RegExp(r"(x?)foo$\1")), "football7"); |
| 321 assertFalse("football".contains(new RegExp(r"foo$(?=ball)")), "football8"); |
| 322 assertFalse("football".contains(new RegExp(r"foo$(?!bar)")), "football9"); |
| 323 assertTrue("foo".contains(new RegExp(r"(x?)foo$\1")), "football10"); |
| 324 assertTrue("foo".contains(new RegExp(r"foo$(?=(ball)?)")), "football11"); |
| 325 assertTrue("foo".contains(new RegExp(r"foo$(?!bar)")), "football12"); |
| 326 |
| 327 // Check that the back reference has two successors. See |
| 328 // BackReferenceNode::PropagateForward. |
| 329 assertFalse('foo'.contains(new RegExp(r"f(o)\b\1"))); |
| 330 assertTrue('foo'.contains(new RegExp(r"f(o)\B\1"))); |
| 331 |
| 332 // Back-reference, ignore case: |
| 333 // ASCII |
| 334 assertEquals("a", new RegExp(r"x(a)\1x", caseSensitive: false) |
| 335 .firstMatch("xaAx").group(1), "backref-ASCII"); |
| 336 assertFalse("xaaaaa".contains(new RegExp(r"x(...)\1", |
| 337 caseSensitive: false)), |
| 338 "backref-ASCII-short"); |
| 339 assertTrue("xx".contains(new RegExp(r"x((?:))\1\1x", caseSensitive: false)), |
| 340 "backref-ASCII-empty"); |
| 341 assertTrue("xabcx".contains( |
| 342 new RegExp(r"x(?:...|(...))\1x", caseSensitive: false)), |
| 343 "backref-ASCII-uncaptured"); |
| 344 assertTrue("xabcABCx".contains( |
| 345 new RegExp(r"x(?:...|(...))\1x", caseSensitive: false)), |
| 346 "backref-ASCII-backtrack"); |
| 347 assertEquals("aBc", new RegExp(r"x(...)\1\1x", caseSensitive: false) |
| 348 .firstMatch("xaBcAbCABCx").group(1), "backref-ASCII-twice"); |
| 349 |
| 350 for (var i = 0; i < 128; i++) { |
| 351 var testName = "backref-ASCII-char-$i,,${i^0x20}"; |
| 352 var test = new String.fromCharCodes([i, i ^ 0x20]) |
| 353 .contains(new RegExp(r"^(.)\1$", caseSensitive: false)); |
| 354 if (('A'.codeUnitAt(0) <= i && i <= 'Z'.codeUnitAt(0)) || |
| 355 ('a'.codeUnitAt(0) <= i && i <= 'z'.codeUnitAt(0))) { |
| 356 assertTrue(test, testName); |
| 357 } else { |
| 358 assertFalse(test, testName); |
| 359 } |
| 360 } |
| 361 |
| 362 assertFalse('foo'.contains(new RegExp(r"f(o)$\1")), |
| 363 "backref detects at_end"); |
| 364 |
| 365 // Check decimal escapes doesn't overflow. |
| 366 // (Note: \214 is interpreted as octal). |
| 367 assertEquals("\x8c7483648", new RegExp(r"\2147483648") |
| 368 .firstMatch("\x8c7483648").group(0), "Overflow decimal escape"); |
| 369 |
| 370 // Check numbers in quantifiers doesn't overflow and doesn't throw on |
| 371 // too large numbers. |
| 372 assertFalse('b'.contains( |
| 373 new RegExp(r"a{111111111111111111111111111111111111111111111}")), |
| 374 "overlarge1"); |
| 375 assertFalse('b'.contains( |
| 376 new RegExp(r"a{999999999999999999999999999999999999999999999}")), |
| 377 "overlarge2"); |
| 378 assertFalse('b'.contains( |
| 379 new RegExp(r"a{1,111111111111111111111111111111111111111111111}")), |
| 380 "overlarge3"); |
| 381 assertFalse('b'.contains( |
| 382 new RegExp(r"a{1,999999999999999999999999999999999999999999999}")), |
| 383 "overlarge4"); |
| 384 assertFalse('b'.contains(new RegExp(r"a{2147483648}")), |
| 385 "overlarge5"); |
| 386 assertFalse('b'.contains(new RegExp(r"a{21474836471}")), |
| 387 "overlarge6"); |
| 388 assertFalse('b'.contains(new RegExp(r"a{1,2147483648}")), |
| 389 "overlarge7"); |
| 390 assertFalse('b'.contains(new RegExp(r"a{1,21474836471}")), |
| 391 "overlarge8"); |
| 392 assertFalse('b'.contains(new RegExp(r"a{2147483648,2147483648}")), |
| 393 "overlarge9"); |
| 394 assertFalse('b'.contains(new RegExp(r"a{21474836471,21474836471}")), |
| 395 "overlarge10"); |
| 396 assertFalse('b'.contains(new RegExp(r"a{2147483647}")), |
| 397 "overlarge11"); |
| 398 assertFalse('b'.contains(new RegExp(r"a{1,2147483647}")), |
| 399 "overlarge12"); |
| 400 assertTrue('a'.contains(new RegExp(r"a{1,2147483647}")), |
| 401 "overlarge13"); |
| 402 assertFalse('a'.contains(new RegExp(r"a{2147483647,2147483647}")), |
| 403 "overlarge14"); |
| 404 |
| 405 // Check that we don't read past the end of the string. |
| 406 assertFalse('b'.contains(new RegExp(r"f"))); |
| 407 assertFalse('x'.contains(new RegExp(r"[abc]f"))); |
| 408 assertFalse('xa'.contains(new RegExp(r"[abc]f"))); |
| 409 assertFalse('x'.contains(new RegExp(r"[abc]<"))); |
| 410 assertFalse('xa'.contains(new RegExp(r"[abc]<"))); |
| 411 assertFalse('b'.contains(new RegExp(r"f", caseSensitive: false))); |
| 412 assertFalse('x'.contains(new RegExp(r"[abc]f", caseSensitive: false))); |
| 413 assertFalse('xa'.contains(new RegExp(r"[abc]f", caseSensitive: false))); |
| 414 assertFalse('x'.contains(new RegExp(r"[abc]<", caseSensitive: false))); |
| 415 assertFalse('xa'.contains(new RegExp(r"[abc]<", caseSensitive: false))); |
| 416 assertFalse('x'.contains(new RegExp(r"f[abc]"))); |
| 417 assertFalse('xa'.contains(new RegExp(r"f[abc]"))); |
| 418 assertFalse('x'.contains(new RegExp(r"<[abc]"))); |
| 419 assertFalse('xa'.contains(new RegExp(r"<[abc]"))); |
| 420 assertFalse('x'.contains(new RegExp(r"f[abc]", caseSensitive: false))); |
| 421 assertFalse('xa'.contains(new RegExp(r"f[abc]", caseSensitive: false))); |
| 422 assertFalse('x'.contains(new RegExp(r"<[abc]", caseSensitive: false))); |
| 423 assertFalse('xa'.contains(new RegExp(r"<[abc]", caseSensitive: false))); |
| 424 |
| 425 // Test that merging of quick test masks gets it right. |
| 426 assertFalse('x7%%y'.contains(new RegExp(r"x([0-7]%%x|[0-6]%%y)")), 'qt'); |
| 427 assertFalse('xy7%%%y'.contains( |
| 428 new RegExp(r"()x\1(y([0-7]%%%x|[0-6]%%%y)|dkjasldkas)")), 'qt2'); |
| 429 assertFalse('xy%%%y'.contains( |
| 430 new RegExp(r"()x\1(y([0-7]%%%x|[0-6]%%%y)|dkjasldkas)")), 'qt3'); |
| 431 assertFalse('xy7%%%y'.contains( |
| 432 new RegExp(r"()x\1y([0-7]%%%x|[0-6]%%%y)")), 'qt4'); |
| 433 assertFalse('xy%%%y'.contains( |
| 434 new RegExp(r"()x\1(y([0-7]%%%x|[0-6]%%%y)|dkjasldkas)")), 'qt5'); |
| 435 assertFalse('xy7%%%y'.contains( |
| 436 new RegExp(r"()x\1y([0-7]%%%x|[0-6]%%%y)")), 'qt6'); |
| 437 assertFalse('xy7%%%y'.contains( |
| 438 new RegExp(r"xy([0-7]%%%x|[0-6]%%%y)")), 'qt7'); |
| 439 assertFalse('x7%%%y'.contains(new RegExp(r"x([0-7]%%%x|[0-6]%%%y)")), |
| 440 'qt8'); |
| 441 |
| 442 |
| 443 // Don't hang on this one. |
| 444 "".contains(new RegExp(r"[^\xfe-\xff]*")); |
| 445 |
| 446 var longbuffer = new StringBuffer("a"); |
| 447 for (var i = 0; i < 100000; i++) { |
| 448 longbuffer.write("a?"); |
| 449 } |
| 450 var long = longbuffer.toString(); |
| 451 |
| 452 // Don't crash on this one, but maybe throw an exception. |
| 453 try { |
| 454 new RegExp(long).allMatches("a"); |
| 455 } catch (e) { |
| 456 assertTrue(String(e).indexOf("Stack overflow") >= 0, "overflow"); |
| 457 } |
| 458 |
| 459 |
| 460 // Test boundary-checks. |
| 461 void assertRegExpTest(re, input, test) { |
| 462 assertEquals(test, input.contains( |
| 463 new RegExp(re)), "test:" + re + ":" + input); |
| 464 } |
| 465 |
| 466 assertRegExpTest(r"b\b", "b", true); |
| 467 assertRegExpTest(r"b\b$", "b", true); |
| 468 assertRegExpTest(r"\bb", "b", true); |
| 469 assertRegExpTest(r"^\bb", "b", true); |
| 470 assertRegExpTest(r",\b", ",", false); |
| 471 assertRegExpTest(r",\b$", ",", false); |
| 472 assertRegExpTest(r"\b,", ",", false); |
| 473 assertRegExpTest(r"^\b,", ",", false); |
| 474 |
| 475 assertRegExpTest(r"b\B", "b", false); |
| 476 assertRegExpTest(r"b\B$", "b", false); |
| 477 assertRegExpTest(r"\Bb", "b", false); |
| 478 assertRegExpTest(r"^\Bb", "b", false); |
| 479 assertRegExpTest(r",\B", ",", true); |
| 480 assertRegExpTest(r",\B$", ",", true); |
| 481 assertRegExpTest(r"\B,", ",", true); |
| 482 assertRegExpTest(r"^\B,", ",", true); |
| 483 |
| 484 assertRegExpTest(r"b\b", "b,", true); |
| 485 assertRegExpTest(r"b\b", "ba", false); |
| 486 assertRegExpTest(r"b\B", "b,", false); |
| 487 assertRegExpTest(r"b\B", "ba", true); |
| 488 |
| 489 assertRegExpTest(r"b\Bb", "bb", true); |
| 490 assertRegExpTest(r"b\bb", "bb", false); |
| 491 |
| 492 assertRegExpTest(r"b\b[,b]", "bb", false); |
| 493 assertRegExpTest(r"b\B[,b]", "bb", true); |
| 494 assertRegExpTest(r"b\b[,b]", "b,", true); |
| 495 assertRegExpTest(r"b\B[,b]", "b,", false); |
| 496 |
| 497 assertRegExpTest(r"[,b]\bb", "bb", false); |
| 498 assertRegExpTest(r"[,b]\Bb", "bb", true); |
| 499 assertRegExpTest(r"[,b]\bb", ",b", true); |
| 500 assertRegExpTest(r"[,b]\Bb", ",b", false); |
| 501 |
| 502 assertRegExpTest(r"[,b]\b[,b]", "bb", false); |
| 503 assertRegExpTest(r"[,b]\B[,b]", "bb", true); |
| 504 assertRegExpTest(r"[,b]\b[,b]", ",b", true); |
| 505 assertRegExpTest(r"[,b]\B[,b]", ",b", false); |
| 506 assertRegExpTest(r"[,b]\b[,b]", "b,", true); |
| 507 assertRegExpTest(r"[,b]\B[,b]", "b,", false); |
| 508 |
| 509 // Skipped tests from V8: |
| 510 |
| 511 // Test that caching of result doesn't share result objects. |
| 512 // More iterations increases the chance of hitting a GC. |
| 513 |
| 514 // Test that we perform the spec required conversions in the correct order. |
| 515 |
| 516 // Check that properties of RegExp have the correct permissions. |
| 517 |
| 518 // Check that end-anchored regexps are optimized correctly. |
| 519 re = r"(?:a|bc)g$"; |
| 520 assertTrue("ag".contains(new RegExp(re))); |
| 521 assertTrue("bcg".contains(new RegExp(re))); |
| 522 assertTrue("abcg".contains(new RegExp(re))); |
| 523 assertTrue("zimbag".contains(new RegExp(re))); |
| 524 assertTrue("zimbcg".contains(new RegExp(re))); |
| 525 |
| 526 assertFalse("g".contains(new RegExp(re))); |
| 527 assertFalse("".contains(new RegExp(re))); |
| 528 |
| 529 // Global regexp (non-zero start). |
| 530 re = r"(?:a|bc)g$"; |
| 531 assertTrue("ag".contains(new RegExp(re))); |
| 532 // Near start of string. |
| 533 assertTrue(new RegExp(re).allMatches("zimbag", 1).isNotEmpty); |
| 534 // At end of string. |
| 535 assertTrue(new RegExp(re).allMatches("zimbag", 6).isEmpty); |
| 536 // Near end of string. |
| 537 assertTrue(new RegExp(re).allMatches("zimbag", 5).isEmpty); |
| 538 assertTrue(new RegExp(re).allMatches("zimbag", 4).isNotEmpty); |
| 539 |
| 540 // Anchored at both ends. |
| 541 re = r"^(?:a|bc)g$"; |
| 542 assertTrue("ag".contains(new RegExp(re))); |
| 543 assertTrue(new RegExp(re).allMatches("ag", 1).isEmpty); |
| 544 assertTrue(new RegExp(re).allMatches("zag", 1).isEmpty); |
| 545 |
| 546 // Long max_length of RegExp. |
| 547 re = r"VeryLongRegExp!{1,1000}$"; |
| 548 assertTrue("BahoolaVeryLongRegExp!!!!!!".contains(new RegExp(re))); |
| 549 assertFalse("VeryLongRegExp".contains(new RegExp(re))); |
| 550 assertFalse("!".contains(new RegExp(re))); |
| 551 |
| 552 // End anchor inside disjunction. |
| 553 re = r"(?:a$|bc$)"; |
| 554 assertTrue("a".contains(new RegExp(re))); |
| 555 assertTrue("bc".contains(new RegExp(re))); |
| 556 assertTrue("abc".contains(new RegExp(re))); |
| 557 assertTrue("zimzamzumba".contains(new RegExp(re))); |
| 558 assertTrue("zimzamzumbc".contains(new RegExp(re))); |
| 559 assertFalse("c".contains(new RegExp(re))); |
| 560 assertFalse("".contains(new RegExp(re))); |
| 561 |
| 562 // Only partially anchored. |
| 563 re = r"(?:a|bc$)"; |
| 564 assertTrue("a".contains(new RegExp(re))); |
| 565 assertTrue("bc".contains(new RegExp(re))); |
| 566 assertEquals("a", new RegExp(re).firstMatch("abc").group(0)); |
| 567 assertEquals(4, new RegExp(re).firstMatch("zimzamzumba").start); |
| 568 assertEquals("bc", new RegExp(re).firstMatch("zimzomzumbc").group(0)); |
| 569 assertFalse("c".contains(new RegExp(re))); |
| 570 assertFalse("".contains(new RegExp(re))); |
| 571 |
| 572 // Valid syntax in ES5. |
| 573 re = new RegExp("(?:x)*"); |
| 574 re = new RegExp("(x)*"); |
| 575 |
| 576 // Syntax extension relative to ES5, for matching JSC (and ES3). |
| 577 // Shouldn't throw. |
| 578 re = new RegExp("(?=x)*"); |
| 579 re = new RegExp("(?!x)*"); |
| 580 |
| 581 // Should throw. Shouldn't hit asserts in debug mode. |
| 582 assertThrows(() => new RegExp('(*)')); |
| 583 assertThrows(() => new RegExp('(?:*)')); |
| 584 assertThrows(() => new RegExp('(?=*)')); |
| 585 assertThrows(() => new RegExp('(?!*)')); |
| 586 |
| 587 // Test trimmed regular expression for RegExp.test(). |
| 588 assertTrue("abc".contains(new RegExp(r".*abc"))); |
| 589 assertFalse("q".contains(new RegExp(r".*\d+"))); |
| 590 |
| 591 // Tests skipped from V8: |
| 592 // Test that RegExp.prototype.toString() throws TypeError for |
| 593 // incompatible receivers (ES5 section 15.10.6 and 15.10.6.4). |
| 594 } |
OLD | NEW |