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