Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 1 | // Copyright 2012 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 | // Summary of the spec: lastIndex is reset to 0 if |
| 29 | // - a regexp fails to match, regardless of global or non-global. |
| 30 | // - a global regexp is used in a function that returns multiple results, |
| 31 | // such as String.prototype.replace or String.prototype.match, since it |
| 32 | // repeats the regexp until it fails to match. |
| 33 | // Otherwise lastIndex is only set when a global regexp matches, to the index |
| 34 | // after the match. |
| 35 | |
| 36 | // Test Regexp.prototype.exec |
| 37 | r = /a/; |
| 38 | r.lastIndex = 1; |
| 39 | r.exec("zzzz"); |
| 40 | assertEquals(0, r.lastIndex); |
| 41 | |
| 42 | // Test Regexp.prototype.test |
| 43 | r = /a/; |
| 44 | r.lastIndex = 1; |
| 45 | r.test("zzzz"); |
| 46 | assertEquals(0, r.lastIndex); |
| 47 | |
| 48 | // Test String.prototype.match |
| 49 | r = /a/; |
| 50 | r.lastIndex = 1; |
| 51 | "zzzz".match(r); |
| 52 | assertEquals(0, r.lastIndex); |
| 53 | |
| 54 | // Test String.prototype.replace with atomic regexp and empty string. |
| 55 | r = /a/; |
| 56 | r.lastIndex = 1; |
| 57 | "zzzz".replace(r, ""); |
| 58 | assertEquals(0, r.lastIndex); |
| 59 | |
| 60 | // Test String.prototype.replace with non-atomic regexp and empty string. |
| 61 | r = /\d/; |
| 62 | r.lastIndex = 1; |
| 63 | "zzzz".replace(r, ""); |
| 64 | assertEquals(0, r.lastIndex); |
| 65 | |
| 66 | // Test String.prototype.replace with atomic regexp and non-empty string. |
| 67 | r = /a/; |
| 68 | r.lastIndex = 1; |
| 69 | "zzzz".replace(r, "a"); |
| 70 | assertEquals(0, r.lastIndex); |
| 71 | |
| 72 | // Test String.prototype.replace with non-atomic regexp and non-empty string. |
| 73 | r = /\d/; |
| 74 | r.lastIndex = 1; |
| 75 | "zzzz".replace(r, "a"); |
| 76 | assertEquals(0, r.lastIndex); |
| 77 | |
| 78 | // Test String.prototype.replace with replacement function |
| 79 | r = /a/; |
| 80 | r.lastIndex = 1; |
| 81 | "zzzz".replace(r, function() { return ""; }); |
| 82 | assertEquals(0, r.lastIndex); |
| 83 | |
| 84 | // Regexp functions that returns multiple results: |
| 85 | // A global regexp always resets lastIndex regardless of whether it matches. |
| 86 | r = /a/g; |
| 87 | r.lastIndex = -1; |
| 88 | "0123abcd".replace(r, "x"); |
| 89 | assertEquals(0, r.lastIndex); |
| 90 | |
| 91 | r.lastIndex = -1; |
| 92 | "01234567".replace(r, "x"); |
| 93 | assertEquals(0, r.lastIndex); |
| 94 | |
| 95 | r.lastIndex = -1; |
| 96 | "0123abcd".match(r); |
| 97 | assertEquals(0, r.lastIndex); |
| 98 | |
| 99 | r.lastIndex = -1; |
| 100 | "01234567".match(r); |
| 101 | assertEquals(0, r.lastIndex); |
| 102 | |
| 103 | // A non-global regexp resets lastIndex iff it does not match. |
| 104 | r = /a/; |
| 105 | r.lastIndex = -1; |
| 106 | "0123abcd".replace(r, "x"); |
| 107 | assertEquals(-1, r.lastIndex); |
| 108 | |
| 109 | r.lastIndex = -1; |
| 110 | "01234567".replace(r, "x"); |
| 111 | assertEquals(0, r.lastIndex); |
| 112 | |
| 113 | r.lastIndex = -1; |
| 114 | "0123abcd".match(r); |
| 115 | assertEquals(-1, r.lastIndex); |
| 116 | |
| 117 | r.lastIndex = -1; |
| 118 | "01234567".match(r); |
| 119 | assertEquals(0, r.lastIndex); |
| 120 | |
| 121 | // Also test RegExp.prototype.exec and RegExp.prototype.test |
| 122 | r = /a/g; |
| 123 | r.lastIndex = 1; |
| 124 | r.exec("01234567"); |
| 125 | assertEquals(0, r.lastIndex); |
| 126 | |
| 127 | r.lastIndex = 1; |
| 128 | r.exec("0123abcd"); |
| 129 | assertEquals(5, r.lastIndex); |
| 130 | |
| 131 | r = /a/; |
| 132 | r.lastIndex = 1; |
| 133 | r.exec("01234567"); |
| 134 | assertEquals(0, r.lastIndex); |
| 135 | |
| 136 | r.lastIndex = 1; |
| 137 | r.exec("0123abcd"); |
| 138 | assertEquals(1, r.lastIndex); |
| 139 | |
| 140 | r = /a/g; |
| 141 | r.lastIndex = 1; |
| 142 | r.test("01234567"); |
| 143 | assertEquals(0, r.lastIndex); |
| 144 | |
| 145 | r.lastIndex = 1; |
| 146 | r.test("0123abcd"); |
| 147 | assertEquals(5, r.lastIndex); |
| 148 | |
| 149 | r = /a/; |
| 150 | r.lastIndex = 1; |
| 151 | r.test("01234567"); |
| 152 | assertEquals(0, r.lastIndex); |
| 153 | |
| 154 | r.lastIndex = 1; |
| 155 | r.test("0123abcd"); |
| 156 | assertEquals(1, r.lastIndex); |