| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 // Tests captures in positive and negative look-ahead in regular expressions. |
| 29 |
| 30 function stringEscape(string) { |
| 31 // Converts string to source literal. |
| 32 return '"' + string.replace(/["\\]/g, "\\$1") + '"'; |
| 33 } |
| 34 |
| 35 function testRE(re, input, expected_result) { |
| 36 var testName = re + ".test(" + stringEscape(input) +")"; |
| 37 if (expected_result) { |
| 38 assertTrue(re.test(input), testName); |
| 39 } else { |
| 40 assertFalse(re.test(input), testName); |
| 41 } |
| 42 } |
| 43 |
| 44 function execRE(re, input, expected_result) { |
| 45 var testName = re + ".exec('" + stringEscape(input) +"')"; |
| 46 assertEquals(expected_result, re.exec(input), testName); |
| 47 } |
| 48 |
| 49 // Test of simple positive lookahead. |
| 50 |
| 51 var re = /^(?=a)/; |
| 52 testRE(re, "a", true); |
| 53 testRE(re, "b", false); |
| 54 execRE(re, "a", [""]); |
| 55 |
| 56 re = /^(?=\woo)f\w/; |
| 57 testRE(re, "foo", true); |
| 58 testRE(re, "boo", false); |
| 59 testRE(re, "fao", false); |
| 60 testRE(re, "foa", false); |
| 61 execRE(re, "foo", ["fo"]); |
| 62 |
| 63 re = /(?=\w).(?=\W)/; |
| 64 testRE(re, ".a! ", true); |
| 65 testRE(re, ".! ", false); |
| 66 testRE(re, ".ab! ", true); |
| 67 execRE(re, ".ab! ", ["b"]); |
| 68 |
| 69 re = /(?=f(?=[^f]o))../; |
| 70 testRE(re, ", foo!", true); |
| 71 testRE(re, ", fo!", false); |
| 72 testRE(re, ", ffo", false); |
| 73 execRE(re, ", foo!", ["fo"]); |
| 74 |
| 75 // Positive lookahead with captures. |
| 76 re = /^[^\'\"]*(?=([\'\"])).*\1(\w+)\1/; |
| 77 testRE(re, " 'foo' ", true); |
| 78 testRE(re, ' "foo" ', true); |
| 79 testRE(re, " \" 'foo' ", false); |
| 80 testRE(re, " ' \"foo\" ", false); |
| 81 testRE(re, " 'foo\" ", false); |
| 82 testRE(re, " \"foo' ", false); |
| 83 execRE(re, " 'foo' ", [" 'foo'", "'", "foo"]); |
| 84 execRE(re, ' "foo" ', [' "foo"', '"', 'foo']); |
| 85 |
| 86 // Captures are cleared on backtrack past the look-ahead. |
| 87 re = /^(?:(?=(.))a|b)\1$/; |
| 88 testRE(re, "aa", true); |
| 89 testRE(re, "b", true); |
| 90 testRE(re, "bb", false); |
| 91 testRE(re, "a", false); |
| 92 execRE(re, "aa", ["aa", "a"]); |
| 93 execRE(re, "b", ["b", undefined]); |
| 94 |
| 95 re = /^(?=(.)(?=(.)\1\2)\2\1)\1\2/; |
| 96 testRE(re, "abab", true); |
| 97 testRE(re, "ababxxxxxxxx", true); |
| 98 testRE(re, "aba", false); |
| 99 execRE(re, "abab", ["ab", "a", "b"]); |
| 100 |
| 101 re = /^(?:(?=(.))a|b|c)$/; |
| 102 testRE(re, "a", true); |
| 103 testRE(re, "b", true); |
| 104 testRE(re, "c", true); |
| 105 testRE(re, "d", false); |
| 106 execRE(re, "a", ["a", "a"]); |
| 107 execRE(re, "b", ["b", undefined]); |
| 108 execRE(re, "c", ["c", undefined]); |
| 109 |
| 110 execRE(/^(?=(b))b/, "b", ["b", "b"]); |
| 111 execRE(/^(?:(?=(b))|a)b/, "ab", ["ab", undefined]); |
| 112 execRE(/^(?:(?=(b)(?:(?=(c))|d))|)bd/, "bd", ["bd", "b", undefined]); |
| 113 |
| 114 |
| 115 |
| 116 // Test of Negative Look-Ahead. |
| 117 |
| 118 re = /(?!x)./; |
| 119 testRE(re, "y", true); |
| 120 testRE(re, "x", false); |
| 121 execRE(re, "y", ["y"]); |
| 122 |
| 123 re = /(?!(\d))|\d/; |
| 124 testRE(re, "4", true); |
| 125 execRE(re, "4", ["4", undefined]); |
| 126 execRE(re, "x", ["", undefined]); |
| 127 |
| 128 |
| 129 // Test mixed nested look-ahead with captures. |
| 130 |
| 131 re = /^(?=(x)(?=(y)))/; |
| 132 testRE(re, "xy", true); |
| 133 testRE(re, "xz", false); |
| 134 execRE(re, "xy", ["", "x", "y"]); |
| 135 |
| 136 re = /^(?!(x)(?!(y)))/; |
| 137 testRE(re, "xy", true); |
| 138 testRE(re, "xz", false); |
| 139 execRE(re, "xy", ["", undefined, undefined]); |
| 140 |
| 141 re = /^(?=(x)(?!(y)))/; |
| 142 testRE(re, "xz", true); |
| 143 testRE(re, "xy", false) |
| 144 execRE(re, "xz", ["", "x", undefined]); |
| 145 |
| 146 re = /^(?!(x)(?=(y)))/; |
| 147 testRE(re, "xz", true); |
| 148 testRE(re, "xy", false); |
| 149 execRE(re, "xz", ["", undefined, undefined]); |
| 150 |
| 151 re = /^(?=(x)(?!(y)(?=(z))))/; |
| 152 testRE(re, "xaz", true); |
| 153 testRE(re, "xya", true); |
| 154 testRE(re, "xyz", false); |
| 155 testRE(re, "a", false); |
| 156 execRE(re, "xaz", ["", "x", undefined, undefined]); |
| 157 execRE(re, "xya", ["", "x", undefined, undefined]); |
| 158 |
| 159 re = /^(?!(x)(?=(y)(?!(z))))/; |
| 160 testRE(re, "a", true); |
| 161 testRE(re, "xa", true); |
| 162 testRE(re, "xyz", true); |
| 163 testRE(re, "xya", false); |
| 164 execRE(re, "a", ["", undefined, undefined, undefined]); |
| 165 execRE(re, "xa", ["", undefined, undefined, undefined]); |
| 166 execRE(re, "xyz", ["", undefined, undefined, undefined]); |
| OLD | NEW |