regexp-zero-length-alternatives.js 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
// Copyright 2013 the V8 project authors. All rights reserved.
// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1.  Redistributions of source code must retain the above copyright
//     notice, this list of conditions and the following disclaimer.
// 2.  Redistributions in binary form must reproduce the above copyright
//     notice, this list of conditions and the following disclaimer in the
//     documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

description(
'Test regular expression processing with alternatives that match consuming no characters'
);

var emptyStr = "";
var s1 = "xxxx";
var s2 = "aaaa";
var s3 = "aax";
var s4 = "abab";
var s5 = "ab";
var s6 = "xabx";
var s7 = "g0";

// Non-capturing empty first alternative greedy '*'
var re1 = new RegExp(/(?:|a|z)*/);
shouldBe('emptyStr.match(re1)', '[""]');
shouldBe('s1.match(re1)', '[""]');
shouldBe('s2.match(re1)', '["aaaa"]');
shouldBe('s3.match(re1)', '["aa"]');

// Non-capturing empty middle alternative greedy '*'
var re2 = new RegExp(/(?:a||z)*/);
shouldBe('emptyStr.match(re2)', '[""]');
shouldBe('s1.match(re2)', '[""]');
shouldBe('s2.match(re2)', '["aaaa"]');
shouldBe('s3.match(re2)', '["aa"]');

// Non-capturing empty last alternative greedy '*'
var re3 = new RegExp(/(?:a|z|)*/);
shouldBe('emptyStr.match(re3)', '[""]');
shouldBe('s1.match(re3)', '[""]');
shouldBe('s2.match(re3)', '["aaaa"]');
shouldBe('s3.match(re3)', '["aa"]');

// Capturing empty first alternative greedy '*'
var re4 = new RegExp(/(|a|z)*/);
shouldBe('emptyStr.match(re4)', '["", undefined]');
shouldBe('s1.match(re4)', '["", undefined]');
shouldBe('s2.match(re4)', '["aaaa", "a"]');
shouldBe('s3.match(re4)', '["aa", "a"]');

// Capturing empty middle alternative greedy '*'
var re5 = new RegExp(/(a||z)*/);
shouldBe('emptyStr.match(re5)', '["", undefined]');
shouldBe('s1.match(re5)', '["", undefined]');
shouldBe('s2.match(re5)', '["aaaa", "a"]');
shouldBe('s3.match(re5)', '["aa", "a"]');

// Capturing empty last alternative greedy '*'
var re6 = new RegExp(/(a|z|)*/);
shouldBe('emptyStr.match(re6)', '["", undefined]');
shouldBe('s1.match(re6)', '["", undefined]');
shouldBe('s2.match(re6)', '["aaaa", "a"]');
shouldBe('s3.match(re6)', '["aa", "a"]');

// Non-capturing empty first alternative fixed-count
var re7 = new RegExp(/(?:|a|z){2,5}/);
shouldBe('emptyStr.match(re7)', '[""]');
shouldBe('s1.match(re7)', '[""]');
shouldBe('s2.match(re7)', '["aaa"]');
shouldBe('s3.match(re7)', '["aa"]');

// Non-capturing empty middle alternative fixed-count
var re8 = new RegExp(/(?:a||z){2,5}/);
shouldBe('emptyStr.match(re8)', '[""]');
shouldBe('s1.match(re8)', '[""]');
shouldBe('s2.match(re8)', '["aaaa"]');
shouldBe('s3.match(re8)', '["aa"]');

// Non-capturing empty last alternative fixed-count
var re9 = new RegExp(/(?:a|z|){2,5}/);
shouldBe('emptyStr.match(re9)', '[""]');
shouldBe('s1.match(re9)', '[""]');
shouldBe('s2.match(re9)', '["aaaa"]');
shouldBe('s3.match(re9)', '["aa"]');

// Non-capturing empty first alternative non-greedy '*'
var re10 = new RegExp(/(?:|a|z)*?/);
shouldBe('emptyStr.match(re10)', '[""]');
shouldBe('s1.match(re10)', '[""]');
shouldBe('s2.match(re10)', '[""]');
shouldBe('s3.match(re10)', '[""]');

// Non-capturing empty middle alternative non-greedy '*'
var re11 = new RegExp(/(?:a||z)*?/);
shouldBe('emptyStr.match(re11)', '[""]');
shouldBe('s1.match(re11)', '[""]');
shouldBe('s2.match(re11)', '[""]');
shouldBe('s3.match(re11)', '[""]');

// Non-capturing empty last alternative non-greedy '*'
var re12 = new RegExp(/(?:a|z|)*?/);
shouldBe('emptyStr.match(re12)', '[""]');
shouldBe('s1.match(re12)', '[""]');
shouldBe('s2.match(re12)', '[""]');
shouldBe('s3.match(re12)', '[""]');

// Capturing empty first alternative non-greedy '*'
var re13 = new RegExp(/(|a|z)*?/);
shouldBe('emptyStr.match(re13)', '["", undefined]');
shouldBe('s1.match(re13)', '["", undefined]');
shouldBe('s2.match(re13)', '["", undefined]');
shouldBe('s3.match(re13)', '["", undefined]');

// Capturing empty middle alternative non-greedy '*'
var re14 = new RegExp(/(a||z)*?/);
shouldBe('emptyStr.match(re14)', '["", undefined]');
shouldBe('s1.match(re14)', '["", undefined]');
shouldBe('s2.match(re14)', '["", undefined]');
shouldBe('s3.match(re14)', '["", undefined]');

// Capturing empty last alternative non-greedy '*'
var re15 = new RegExp(/(a|z|)*?/);
shouldBe('emptyStr.match(re15)', '["", undefined]');
shouldBe('s1.match(re15)', '["", undefined]');
shouldBe('s2.match(re15)', '["", undefined]');
shouldBe('s3.match(re15)', '["", undefined]');

// Non-capturing empty first alternative greedy '?'
var re16 = new RegExp(/(?:|a|z)?/);
shouldBe('emptyStr.match(re16)', '[""]');
shouldBe('s1.match(re16)', '[""]');
shouldBe('s2.match(re16)', '["a"]');
shouldBe('s3.match(re16)', '["a"]');

// Non-capturing empty middle alternative greedy '?'
var re17 = new RegExp(/(?:a||z)?/);
shouldBe('emptyStr.match(re17)', '[""]');
shouldBe('s1.match(re17)', '[""]');
shouldBe('s2.match(re17)', '["a"]');
shouldBe('s3.match(re17)', '["a"]');

// Non-capturing empty last alternative greedy '?'
var re18 = new RegExp(/(?:a|z|)?/);
shouldBe('emptyStr.match(re18)', '[""]');
shouldBe('s1.match(re18)', '[""]');
shouldBe('s2.match(re18)', '["a"]');
shouldBe('s3.match(re18)', '["a"]');

// Capturing empty first alternative greedy '?'
var re19 = new RegExp(/(|a|z)?/);
shouldBe('emptyStr.match(re19)', '["", undefined]');
shouldBe('s1.match(re19)', '["", undefined]');
shouldBe('s2.match(re19)', '["a", "a"]');
shouldBe('s3.match(re19)', '["a", "a"]');

// Capturing empty middle alternative greedy '?'
var re20 = new RegExp(/(a||z)?/);
shouldBe('emptyStr.match(re20)', '["", undefined]');
shouldBe('s1.match(re20)', '["", undefined]');
shouldBe('s2.match(re20)', '["a", "a"]');
shouldBe('s3.match(re20)', '["a", "a"]');

// Capturing empty last alternative greedy '?'
var re21 = new RegExp(/(a|z|)?/);
shouldBe('emptyStr.match(re21)', '["", undefined]');
shouldBe('s1.match(re21)', '["", undefined]');
shouldBe('s2.match(re21)', '["a", "a"]');
shouldBe('s3.match(re21)', '["a", "a"]');

// Non-capturing empty first alternative non-greedy '?'
var re22 = new RegExp(/(?:|a|z)??/);
shouldBe('emptyStr.match(re22)', '[""]');
shouldBe('s1.match(re22)', '[""]');
shouldBe('s2.match(re22)', '[""]');
shouldBe('s3.match(re22)', '[""]');

// Non-capturing empty middle alternative non-greedy '?'
var re23 = new RegExp(/(?:a||z)??/);
shouldBe('emptyStr.match(re23)', '[""]');
shouldBe('s1.match(re23)', '[""]');
shouldBe('s2.match(re23)', '[""]');
shouldBe('s3.match(re23)', '[""]');

// Non-capturing empty last alternative non-greedy '?'
var re24 = new RegExp(/(?:a|z|)??/);
shouldBe('emptyStr.match(re24)', '[""]');
shouldBe('s1.match(re24)', '[""]');
shouldBe('s2.match(re24)', '[""]');
shouldBe('s3.match(re24)', '[""]');

// Capturing empty first alternative non-greedy '?'
var re25 = new RegExp(/(|a|z)??/);
shouldBe('emptyStr.match(re25)', '["", undefined]');
shouldBe('s1.match(re25)', '["", undefined]');
shouldBe('s2.match(re25)', '["", undefined]');
shouldBe('s3.match(re25)', '["", undefined]');

// Capturing empty middle alternative non-greedy '?'
var re26 = new RegExp(/(a||z)??/);
shouldBe('emptyStr.match(re26)', '["", undefined]');
shouldBe('s1.match(re26)', '["", undefined]');
shouldBe('s2.match(re26)', '["", undefined]');
shouldBe('s3.match(re26)', '["", undefined]');

// Capturing empty last alternative non-greedy '?'
var re27 = new RegExp(/(a|z|)??/);
shouldBe('emptyStr.match(re27)', '["", undefined]');
shouldBe('s1.match(re27)', '["", undefined]');
shouldBe('s2.match(re27)', '["", undefined]');
shouldBe('s3.match(re27)', '["", undefined]');

// Non-capturing empty first alternative greedy '*' non-terminal
var re28 = new RegExp(/(?:|a|z)*x/);
shouldBe('emptyStr.match(re28)', 'null');
shouldBe('s1.match(re28)', '["x"]');
shouldBe('s2.match(re28)', 'null');
shouldBe('s3.match(re28)', '["aax"]');

// Non-capturing empty middle alternative greedy '*' non-terminal
var re29 = new RegExp(/(?:a||z)*x/);
shouldBe('emptyStr.match(re29)', 'null');
shouldBe('s1.match(re29)', '["x"]');
shouldBe('s2.match(re29)', 'null');
shouldBe('s3.match(re29)', '["aax"]');

// Non-capturing empty last alternative greedy '*' non-terminal
var re30 = new RegExp(/(?:a|z|)*x/);
shouldBe('emptyStr.match(re30)', 'null');
shouldBe('s1.match(re30)', '["x"]');
shouldBe('s2.match(re30)', 'null');
shouldBe('s3.match(re30)', '["aax"]');

// Non-capturing two possibly empty alternatives greedy '*'
var re31 = new RegExp(/(?:a*|b*)*/);
shouldBe('emptyStr.match(re31)', '[""]');
shouldBe('s1.match(re31)', '[""]');
shouldBe('s3.match(re31)', '["aa"]');
shouldBe('s4.match(re31)', '["abab"]');

// Non-capturing two possibly empty non-greedy alternatives non-greedy '*'
var re32 = new RegExp(/(?:a*?|b*?)*/);
shouldBe('emptyStr.match(re32)', '[""]');
shouldBe('s1.match(re32)', '[""]');
shouldBe('s2.match(re32)', '["aaaa"]');
shouldBe('s4.match(re32)', '["abab"]');
shouldBe('s5.match(re32)', '["ab"]');
shouldBe('s6.match(re32)', '[""]');

// Three possibly empty alternatives with greedy +
var re33 = new RegExp(/(?:(?:(?!))|g?|0*\*?)+/);
shouldBe('emptyStr.match(re33)', '[""]');
shouldBe('s1.match(re33)', '[""]');
shouldBe('s7.match(re33)', '["g0"]');

// first alternative zero length fixed count
var re34 = new RegExp(/(?:|a)/);
shouldBe('emptyStr.match(re34)', '[""]');
shouldBe('s1.match(re34)', '[""]');
shouldBe('s2.match(re34)', '[""]');
shouldBe('s3.match(re34)', '[""]');