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( |
| 30 'Test regular expression processing with alternatives that match consuming no
characters' |
| 31 ); |
| 32 |
| 33 var emptyStr = ""; |
| 34 var s1 = "xxxx"; |
| 35 var s2 = "aaaa"; |
| 36 var s3 = "aax"; |
| 37 var s4 = "abab"; |
| 38 var s5 = "ab"; |
| 39 var s6 = "xabx"; |
| 40 var s7 = "g0"; |
| 41 |
| 42 // Non-capturing empty first alternative greedy '*' |
| 43 var re1 = new RegExp(r"(?:|a|z)*"); |
| 44 shouldBe(firstMatch(emptyStr, re1), [""]); |
| 45 shouldBe(firstMatch(s1, re1), [""]); |
| 46 shouldBe(firstMatch(s2, re1), ["aaaa"]); |
| 47 shouldBe(firstMatch(s3, re1), ["aa"]); |
| 48 |
| 49 // Non-capturing empty middle alternative greedy '*' |
| 50 var re2 = new RegExp(r"(?:a||z)*"); |
| 51 shouldBe(firstMatch(emptyStr, re2), [""]); |
| 52 shouldBe(firstMatch(s1, re2), [""]); |
| 53 shouldBe(firstMatch(s2, re2), ["aaaa"]); |
| 54 shouldBe(firstMatch(s3, re2), ["aa"]); |
| 55 |
| 56 // Non-capturing empty last alternative greedy '*' |
| 57 var re3 = new RegExp(r"(?:a|z|)*"); |
| 58 shouldBe(firstMatch(emptyStr, re3), [""]); |
| 59 shouldBe(firstMatch(s1, re3), [""]); |
| 60 shouldBe(firstMatch(s2, re3), ["aaaa"]); |
| 61 shouldBe(firstMatch(s3, re3), ["aa"]); |
| 62 |
| 63 // Capturing empty first alternative greedy '*' |
| 64 var re4 = new RegExp(r"(|a|z)*"); |
| 65 shouldBe(firstMatch(emptyStr, re4), ["", null]); |
| 66 shouldBe(firstMatch(s1, re4), ["", null]); |
| 67 shouldBe(firstMatch(s2, re4), ["aaaa", "a"]); |
| 68 shouldBe(firstMatch(s3, re4), ["aa", "a"]); |
| 69 |
| 70 // Capturing empty middle alternative greedy '*' |
| 71 var re5 = new RegExp(r"(a||z)*"); |
| 72 shouldBe(firstMatch(emptyStr, re5), ["", null]); |
| 73 shouldBe(firstMatch(s1, re5), ["", null]); |
| 74 shouldBe(firstMatch(s2, re5), ["aaaa", "a"]); |
| 75 shouldBe(firstMatch(s3, re5), ["aa", "a"]); |
| 76 |
| 77 // Capturing empty last alternative greedy '*' |
| 78 var re6 = new RegExp(r"(a|z|)*"); |
| 79 shouldBe(firstMatch(emptyStr, re6), ["", null]); |
| 80 shouldBe(firstMatch(s1, re6), ["", null]); |
| 81 shouldBe(firstMatch(s2, re6), ["aaaa", "a"]); |
| 82 shouldBe(firstMatch(s3, re6), ["aa", "a"]); |
| 83 |
| 84 // Non-capturing empty first alternative fixed-count |
| 85 var re7 = new RegExp(r"(?:|a|z){2,5}"); |
| 86 shouldBe(firstMatch(emptyStr, re7), [""]); |
| 87 shouldBe(firstMatch(s1, re7), [""]); |
| 88 shouldBe(firstMatch(s2, re7), ["aaa"]); |
| 89 shouldBe(firstMatch(s3, re7), ["aa"]); |
| 90 |
| 91 // Non-capturing empty middle alternative fixed-count |
| 92 var re8 = new RegExp(r"(?:a||z){2,5}"); |
| 93 shouldBe(firstMatch(emptyStr, re8), [""]); |
| 94 shouldBe(firstMatch(s1, re8), [""]); |
| 95 shouldBe(firstMatch(s2, re8), ["aaaa"]); |
| 96 shouldBe(firstMatch(s3, re8), ["aa"]); |
| 97 |
| 98 // Non-capturing empty last alternative fixed-count |
| 99 var re9 = new RegExp(r"(?:a|z|){2,5}"); |
| 100 shouldBe(firstMatch(emptyStr, re9), [""]); |
| 101 shouldBe(firstMatch(s1, re9), [""]); |
| 102 shouldBe(firstMatch(s2, re9), ["aaaa"]); |
| 103 shouldBe(firstMatch(s3, re9), ["aa"]); |
| 104 |
| 105 // Non-capturing empty first alternative non-greedy '*' |
| 106 var re10 = new RegExp(r"(?:|a|z)*?"); |
| 107 shouldBe(firstMatch(emptyStr, re10), [""]); |
| 108 shouldBe(firstMatch(s1, re10), [""]); |
| 109 shouldBe(firstMatch(s2, re10), [""]); |
| 110 shouldBe(firstMatch(s3, re10), [""]); |
| 111 |
| 112 // Non-capturing empty middle alternative non-greedy '*' |
| 113 var re11 = new RegExp(r"(?:a||z)*?"); |
| 114 shouldBe(firstMatch(emptyStr, re11), [""]); |
| 115 shouldBe(firstMatch(s1, re11), [""]); |
| 116 shouldBe(firstMatch(s2, re11), [""]); |
| 117 shouldBe(firstMatch(s3, re11), [""]); |
| 118 |
| 119 // Non-capturing empty last alternative non-greedy '*' |
| 120 var re12 = new RegExp(r"(?:a|z|)*?"); |
| 121 shouldBe(firstMatch(emptyStr, re12), [""]); |
| 122 shouldBe(firstMatch(s1, re12), [""]); |
| 123 shouldBe(firstMatch(s2, re12), [""]); |
| 124 shouldBe(firstMatch(s3, re12), [""]); |
| 125 |
| 126 // Capturing empty first alternative non-greedy '*' |
| 127 var re13 = new RegExp(r"(|a|z)*?"); |
| 128 shouldBe(firstMatch(emptyStr, re13), ["", null]); |
| 129 shouldBe(firstMatch(s1, re13), ["", null]); |
| 130 shouldBe(firstMatch(s2, re13), ["", null]); |
| 131 shouldBe(firstMatch(s3, re13), ["", null]); |
| 132 |
| 133 // Capturing empty middle alternative non-greedy '*' |
| 134 var re14 = new RegExp(r"(a||z)*?"); |
| 135 shouldBe(firstMatch(emptyStr, re14), ["", null]); |
| 136 shouldBe(firstMatch(s1, re14), ["", null]); |
| 137 shouldBe(firstMatch(s2, re14), ["", null]); |
| 138 shouldBe(firstMatch(s3, re14), ["", null]); |
| 139 |
| 140 // Capturing empty last alternative non-greedy '*' |
| 141 var re15 = new RegExp(r"(a|z|)*?"); |
| 142 shouldBe(firstMatch(emptyStr, re15), ["", null]); |
| 143 shouldBe(firstMatch(s1, re15), ["", null]); |
| 144 shouldBe(firstMatch(s2, re15), ["", null]); |
| 145 shouldBe(firstMatch(s3, re15), ["", null]); |
| 146 |
| 147 // Non-capturing empty first alternative greedy '?' |
| 148 var re16 = new RegExp(r"(?:|a|z)?"); |
| 149 shouldBe(firstMatch(emptyStr, re16), [""]); |
| 150 shouldBe(firstMatch(s1, re16), [""]); |
| 151 shouldBe(firstMatch(s2, re16), ["a"]); |
| 152 shouldBe(firstMatch(s3, re16), ["a"]); |
| 153 |
| 154 // Non-capturing empty middle alternative greedy '?' |
| 155 var re17 = new RegExp(r"(?:a||z)?"); |
| 156 shouldBe(firstMatch(emptyStr, re17), [""]); |
| 157 shouldBe(firstMatch(s1, re17), [""]); |
| 158 shouldBe(firstMatch(s2, re17), ["a"]); |
| 159 shouldBe(firstMatch(s3, re17), ["a"]); |
| 160 |
| 161 // Non-capturing empty last alternative greedy '?' |
| 162 var re18 = new RegExp(r"(?:a|z|)?"); |
| 163 shouldBe(firstMatch(emptyStr, re18), [""]); |
| 164 shouldBe(firstMatch(s1, re18), [""]); |
| 165 shouldBe(firstMatch(s2, re18), ["a"]); |
| 166 shouldBe(firstMatch(s3, re18), ["a"]); |
| 167 |
| 168 // Capturing empty first alternative greedy '?' |
| 169 var re19 = new RegExp(r"(|a|z)?"); |
| 170 shouldBe(firstMatch(emptyStr, re19), ["", null]); |
| 171 shouldBe(firstMatch(s1, re19), ["", null]); |
| 172 shouldBe(firstMatch(s2, re19), ["a", "a"]); |
| 173 shouldBe(firstMatch(s3, re19), ["a", "a"]); |
| 174 |
| 175 // Capturing empty middle alternative greedy '?' |
| 176 var re20 = new RegExp(r"(a||z)?"); |
| 177 shouldBe(firstMatch(emptyStr, re20), ["", null]); |
| 178 shouldBe(firstMatch(s1, re20), ["", null]); |
| 179 shouldBe(firstMatch(s2, re20), ["a", "a"]); |
| 180 shouldBe(firstMatch(s3, re20), ["a", "a"]); |
| 181 |
| 182 // Capturing empty last alternative greedy '?' |
| 183 var re21 = new RegExp(r"(a|z|)?"); |
| 184 shouldBe(firstMatch(emptyStr, re21), ["", null]); |
| 185 shouldBe(firstMatch(s1, re21), ["", null]); |
| 186 shouldBe(firstMatch(s2, re21), ["a", "a"]); |
| 187 shouldBe(firstMatch(s3, re21), ["a", "a"]); |
| 188 |
| 189 // Non-capturing empty first alternative non-greedy '?' |
| 190 var re22 = new RegExp(r"(?:|a|z)??"); |
| 191 shouldBe(firstMatch(emptyStr, re22), [""]); |
| 192 shouldBe(firstMatch(s1, re22), [""]); |
| 193 shouldBe(firstMatch(s2, re22), [""]); |
| 194 shouldBe(firstMatch(s3, re22), [""]); |
| 195 |
| 196 // Non-capturing empty middle alternative non-greedy '?' |
| 197 var re23 = new RegExp(r"(?:a||z)??"); |
| 198 shouldBe(firstMatch(emptyStr, re23), [""]); |
| 199 shouldBe(firstMatch(s1, re23), [""]); |
| 200 shouldBe(firstMatch(s2, re23), [""]); |
| 201 shouldBe(firstMatch(s3, re23), [""]); |
| 202 |
| 203 // Non-capturing empty last alternative non-greedy '?' |
| 204 var re24 = new RegExp(r"(?:a|z|)??"); |
| 205 shouldBe(firstMatch(emptyStr, re24), [""]); |
| 206 shouldBe(firstMatch(s1, re24), [""]); |
| 207 shouldBe(firstMatch(s2, re24), [""]); |
| 208 shouldBe(firstMatch(s3, re24), [""]); |
| 209 |
| 210 // Capturing empty first alternative non-greedy '?' |
| 211 var re25 = new RegExp(r"(|a|z)??"); |
| 212 shouldBe(firstMatch(emptyStr, re25), ["", null]); |
| 213 shouldBe(firstMatch(s1, re25), ["", null]); |
| 214 shouldBe(firstMatch(s2, re25), ["", null]); |
| 215 shouldBe(firstMatch(s3, re25), ["", null]); |
| 216 |
| 217 // Capturing empty middle alternative non-greedy '?' |
| 218 var re26 = new RegExp(r"(a||z)??"); |
| 219 shouldBe(firstMatch(emptyStr, re26), ["", null]); |
| 220 shouldBe(firstMatch(s1, re26), ["", null]); |
| 221 shouldBe(firstMatch(s2, re26), ["", null]); |
| 222 shouldBe(firstMatch(s3, re26), ["", null]); |
| 223 |
| 224 // Capturing empty last alternative non-greedy '?' |
| 225 var re27 = new RegExp(r"(a|z|)??"); |
| 226 shouldBe(firstMatch(emptyStr, re27), ["", null]); |
| 227 shouldBe(firstMatch(s1, re27), ["", null]); |
| 228 shouldBe(firstMatch(s2, re27), ["", null]); |
| 229 shouldBe(firstMatch(s3, re27), ["", null]); |
| 230 |
| 231 // Non-capturing empty first alternative greedy '*' non-terminal |
| 232 var re28 = new RegExp(r"(?:|a|z)*x"); |
| 233 shouldBe(firstMatch(emptyStr, re28), null); |
| 234 shouldBe(firstMatch(s1, re28), ["x"]); |
| 235 shouldBe(firstMatch(s2, re28), null); |
| 236 shouldBe(firstMatch(s3, re28), ["aax"]); |
| 237 |
| 238 // Non-capturing empty middle alternative greedy '*' non-terminal |
| 239 var re29 = new RegExp(r"(?:a||z)*x"); |
| 240 shouldBe(firstMatch(emptyStr, re29), null); |
| 241 shouldBe(firstMatch(s1, re29), ["x"]); |
| 242 shouldBe(firstMatch(s2, re29), null); |
| 243 shouldBe(firstMatch(s3, re29), ["aax"]); |
| 244 |
| 245 // Non-capturing empty last alternative greedy '*' non-terminal |
| 246 var re30 = new RegExp(r"(?:a|z|)*x"); |
| 247 shouldBe(firstMatch(emptyStr, re30), null); |
| 248 shouldBe(firstMatch(s1, re30), ["x"]); |
| 249 shouldBe(firstMatch(s2, re30), null); |
| 250 shouldBe(firstMatch(s3, re30), ["aax"]); |
| 251 |
| 252 // Non-capturing two possibly empty alternatives greedy '*' |
| 253 var re31 = new RegExp(r"(?:a*|b*)*"); |
| 254 shouldBe(firstMatch(emptyStr, re31), [""]); |
| 255 shouldBe(firstMatch(s1, re31), [""]); |
| 256 shouldBe(firstMatch(s3, re31), ["aa"]); |
| 257 shouldBe(firstMatch(s4, re31), ["abab"]); |
| 258 |
| 259 // Non-capturing two possibly empty non-greedy alternatives non-greedy '*' |
| 260 var re32 = new RegExp(r"(?:a*?|b*?)*"); |
| 261 shouldBe(firstMatch(emptyStr, re32), [""]); |
| 262 shouldBe(firstMatch(s1, re32), [""]); |
| 263 shouldBe(firstMatch(s2, re32), ["aaaa"]); |
| 264 shouldBe(firstMatch(s4, re32), ["abab"]); |
| 265 shouldBe(firstMatch(s5, re32), ["ab"]); |
| 266 shouldBe(firstMatch(s6, re32), [""]); |
| 267 |
| 268 // Three possibly empty alternatives with greedy + |
| 269 var re33 = new RegExp(r"(?:(?:(?!))|g?|0*\*?)+"); |
| 270 shouldBe(firstMatch(emptyStr, re33), [""]); |
| 271 shouldBe(firstMatch(s1, re33), [""]); |
| 272 shouldBe(firstMatch(s7, re33), ["g0"]); |
| 273 |
| 274 // first alternative zero length fixed count |
| 275 var re34 = new RegExp(r"(?:|a)"); |
| 276 shouldBe(firstMatch(emptyStr, re34), [""]); |
| 277 shouldBe(firstMatch(s1, re34), [""]); |
| 278 shouldBe(firstMatch(s2, re34), [""]); |
| 279 shouldBe(firstMatch(s3, re34), [""]); |
| 280 } |
OLD | NEW |