OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 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 |
28 // Test Regexp.prototype.exec | 36 // Test Regexp.prototype.exec |
29 r = /a/; | 37 r = /a/; |
30 r.lastIndex = 1; | 38 r.lastIndex = 1; |
31 r.exec("zzzz"); | 39 r.exec("zzzz"); |
32 assertEquals(0, r.lastIndex); | 40 assertEquals(0, r.lastIndex); |
33 | 41 |
34 // Test Regexp.prototype.test | 42 // Test Regexp.prototype.test |
35 r = /a/; | 43 r = /a/; |
36 r.lastIndex = 1; | 44 r.lastIndex = 1; |
37 r.test("zzzz"); | 45 r.test("zzzz"); |
(...skipping 28 matching lines...) Expand all Loading... |
66 r.lastIndex = 1; | 74 r.lastIndex = 1; |
67 "zzzz".replace(r, "a"); | 75 "zzzz".replace(r, "a"); |
68 assertEquals(0, r.lastIndex); | 76 assertEquals(0, r.lastIndex); |
69 | 77 |
70 // Test String.prototype.replace with replacement function | 78 // Test String.prototype.replace with replacement function |
71 r = /a/; | 79 r = /a/; |
72 r.lastIndex = 1; | 80 r.lastIndex = 1; |
73 "zzzz".replace(r, function() { return ""; }); | 81 "zzzz".replace(r, function() { return ""; }); |
74 assertEquals(0, r.lastIndex); | 82 assertEquals(0, r.lastIndex); |
75 | 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); |
OLD | NEW |