| 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 'v8_regexp_utils.dart'; | |
| 30 import 'package:expect/expect.dart'; | |
| 31 | |
| 32 void main() { | |
| 33 var str = "ABX X"; | |
| 34 str = str.replaceAll(new RegExp(r"(\w)?X"), "c"); | |
| 35 assertEquals("Ac c", str); | |
| 36 | |
| 37 // Test zero-length matches. | |
| 38 str = "Als Gregor Samsa eines Morgens"; | |
| 39 str = str.replaceAll(new RegExp(r"\b"), "/"); | |
| 40 assertEquals("/Als/ /Gregor/ /Samsa/ /eines/ /Morgens/", str); | |
| 41 | |
| 42 // Test zero-length matches that have non-zero-length sub-captures. | |
| 43 str = "It was a pleasure to burn."; | |
| 44 str = str.replaceAllMapped(new RegExp(r"(?=(\w+))\b"), (Match m) => m.group(1)
.length.toString()); | |
| 45 assertEquals("2It 3was 1a 8pleasure 2to 4burn.", str); | |
| 46 | |
| 47 // Test multiple captures. | |
| 48 str = "Try not. Do, or do not. There is no try."; | |
| 49 str = str.replaceAllMapped(new RegExp(r"(not?)|(do)|(try)", caseSensitive: fal
se), | |
| 50 (m) { | |
| 51 if (m.group(1) != null) return "-"; | |
| 52 if (m.group(2) != null) return "+"; | |
| 53 if (m.group(3) != null) return "="; | |
| 54 }); | |
| 55 assertEquals("= -. +, or + -. There is - =.", str); | |
| 56 | |
| 57 // Test multiple alternate captures. | |
| 58 str = "FOUR LEGS GOOD, TWO LEGS BAD!"; | |
| 59 str = str.replaceAllMapped(new RegExp(r"(FOUR|TWO) LEGS (GOOD|BAD)"), | |
| 60 (m) { | |
| 61 if (m.group(1) == "FOUR") assertTrue(m.group(2) == "GOOD")
; | |
| 62 if (m.group(1) == "TWO") assertTrue(m.group(2) == "BAD"); | |
| 63 return m.group(0).length - 10; | |
| 64 }); | |
| 65 assertEquals("4, 2!", str); | |
| 66 | |
| 67 | |
| 68 // The same tests with UC16. | |
| 69 | |
| 70 //Test that an optional capture is cleared between two matches. | |
| 71 str = "AB\u1234 \u1234"; | |
| 72 str = str.replaceAll(new RegExp(r"(\w)?\u1234"), "c"); | |
| 73 assertEquals("Ac c", str); | |
| 74 | |
| 75 // Test zero-length matches. | |
| 76 str = "Als \u2623\u2642 eines Morgens"; | |
| 77 str = str.replaceAll(new RegExp(r"\b"), "/"); | |
| 78 | |
| 79 // Test zero-length matches that have non-zero-length sub-captures. | |
| 80 str = "It was a pleasure to \u70e7."; | |
| 81 str = str.replaceAllMapped(new RegExp(r"(?=(\w+))\b"), (m) => "${m.group(1).le
ngth}"); | |
| 82 assertEquals("2It 3was 1a 8pleasure 2to \u70e7.", str); | |
| 83 | |
| 84 // Test multiple captures. | |
| 85 str = "Try not. D\u26aa, or d\u26aa not. There is no try."; | |
| 86 str = str.replaceAllMapped(new RegExp(r"(not?)|(d\u26aa)|(try)", caseSensitive
: false), | |
| 87 (m) { | |
| 88 if (m.group(1) != null) return "-"; | |
| 89 if (m.group(2) != null) return "+"; | |
| 90 if (m.group(3) != null) return "="; | |
| 91 }); | |
| 92 assertEquals("= -. +, or + -. There is - =.", str); | |
| 93 | |
| 94 // Test multiple alternate captures. | |
| 95 str = "FOUR \u817f GOOD, TWO \u817f BAD!"; | |
| 96 str = str.replaceAllMapped(new RegExp(r"(FOUR|TWO) \u817f (GOOD|BAD)"), | |
| 97 (m) { | |
| 98 if (m.group(1) == "FOUR") assertTrue(m.group(2) == "GOOD")
; | |
| 99 if (m.group(1) == "TWO") assertTrue(m.group(2) == "BAD"); | |
| 100 return m.group(0).length - 7; | |
| 101 }); | |
| 102 assertEquals("4, 2!", str); | |
| 103 | |
| 104 // Test capture that is a real substring. | |
| 105 str = "Beasts of England, beasts of Ireland"; | |
| 106 str = str.replaceAll(new RegExp(r"(.*)"), '~'); | |
| 107 assertEquals("~~", str); | |
| 108 | |
| 109 // Test zero-length matches that have non-zero-length sub-captures that do not | |
| 110 // start at the match start position. | |
| 111 str = "up up up up"; | |
| 112 str = str.replaceAllMapped(new RegExp(r"\b(?=u(p))"), (m) => "${m.group(1).len
gth}"); | |
| 113 | |
| 114 assertEquals("1up 1up 1up 1up", str); | |
| 115 | |
| 116 | |
| 117 // Create regexp that has a *lot* of captures. | |
| 118 var re_string = "(a)"; | |
| 119 for (var i = 0; i < 500; i++) { | |
| 120 re_string = "(" + re_string + ")"; | |
| 121 } | |
| 122 re_string = re_string + "1"; | |
| 123 // re_string = "(((...((a))...)))1" | |
| 124 | |
| 125 var regexps = new List(); | |
| 126 var last_match_expectations = new List(); | |
| 127 var first_capture_expectations = new List(); | |
| 128 | |
| 129 // Atomic regexp. | |
| 130 regexps.add(new RegExp(r"a1")); | |
| 131 last_match_expectations.add("a1"); | |
| 132 first_capture_expectations.add(""); | |
| 133 // Small regexp (no capture); | |
| 134 regexps.add(new RegExp(r"\w1")); | |
| 135 last_match_expectations.add("a1"); | |
| 136 first_capture_expectations.add(""); | |
| 137 // Small regexp (one capture). | |
| 138 regexps.add(new RegExp(r"(a)1")); | |
| 139 last_match_expectations.add("a1"); | |
| 140 first_capture_expectations.add("a"); | |
| 141 // Large regexp (a lot of captures). | |
| 142 regexps.add(new RegExp(re_string)); | |
| 143 last_match_expectations.add("a1"); | |
| 144 first_capture_expectations.add("a"); | |
| 145 | |
| 146 dynamic test_replace(result_expectation, | |
| 147 subject, | |
| 148 regexp, | |
| 149 replacement) { | |
| 150 for (var i = 0; i < regexps.length; i++) { | |
| 151 // Conduct tests. | |
| 152 assertEquals(result_expectation, subject.replaceAll(regexps[i], replacemen
t)); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 | |
| 157 // Test for different number of matches. | |
| 158 for (var m = 0; m < 33; m++) { | |
| 159 // Create string that matches m times. | |
| 160 var subject = ""; | |
| 161 var test_1_expectation = ""; | |
| 162 var test_2_expectation = ""; | |
| 163 var test_3_expectation = (m == 0) ? null : new List(); | |
| 164 for (var i = 0; i < m; i++) { | |
| 165 subject += "a11"; | |
| 166 test_1_expectation += "x1"; | |
| 167 test_2_expectation += "1"; | |
| 168 test_3_expectation.add("a1"); | |
| 169 } | |
| 170 | |
| 171 // Test 1a: String.replace with string. | |
| 172 test_replace(test_1_expectation, subject, new RegExp(r"a1"), "x"); | |
| 173 | |
| 174 // Test 2a: String.replace with empty string. | |
| 175 test_replace(test_2_expectation, subject, new RegExp(r"a1"), ""); | |
| 176 } | |
| 177 | |
| 178 | |
| 179 // Test String hashing (compiling regular expression includes hashing). | |
| 180 var crosscheck = "\x80"; | |
| 181 for (var i = 0; i < 12; i++) crosscheck += crosscheck; | |
| 182 new RegExp(crosscheck); | |
| 183 | |
| 184 var subject = "ascii~only~string~here~"; | |
| 185 var replacement = "\x80"; | |
| 186 var result = subject.replaceAll(new RegExp(r"~"), replacement); | |
| 187 for (var i = 0; i < 5; i++) result += result; | |
| 188 new RegExp(result); | |
| 189 } | |
| OLD | NEW |