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 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 // Test String.prototype.match | 48 // Test String.prototype.match |
49 r = /a/; | 49 r = /a/; |
50 r.lastIndex = 1; | 50 r.lastIndex = 1; |
51 "zzzz".match(r); | 51 "zzzz".match(r); |
52 assertEquals(1, r.lastIndex); | 52 assertEquals(1, r.lastIndex); |
53 | 53 |
54 // Test String.prototype.replace with atomic regexp and empty string. | 54 // Test String.prototype.replace with atomic regexp and empty string. |
55 r = /a/; | 55 r = /a/; |
56 r.lastIndex = 1; | 56 r.lastIndex = 1; |
57 "zzzz".replace(r, ""); | 57 "zzzz".replace(r, ""); |
58 assertEquals(0, r.lastIndex); | 58 assertEquals(1, r.lastIndex); |
59 | 59 |
60 // Test String.prototype.replace with non-atomic regexp and empty string. | 60 // Test String.prototype.replace with non-atomic regexp and empty string. |
61 r = /\d/; | 61 r = /\d/; |
62 r.lastIndex = 1; | 62 r.lastIndex = 1; |
63 "zzzz".replace(r, ""); | 63 "zzzz".replace(r, ""); |
64 assertEquals(0, r.lastIndex); | 64 assertEquals(1, r.lastIndex); |
65 | 65 |
66 // Test String.prototype.replace with atomic regexp and non-empty string. | 66 // Test String.prototype.replace with atomic regexp and non-empty string. |
67 r = /a/; | 67 r = /a/; |
68 r.lastIndex = 1; | 68 r.lastIndex = 1; |
69 "zzzz".replace(r, "a"); | 69 "zzzz".replace(r, "a"); |
70 assertEquals(0, r.lastIndex); | 70 assertEquals(1, r.lastIndex); |
71 | 71 |
72 // Test String.prototype.replace with non-atomic regexp and non-empty string. | 72 // Test String.prototype.replace with non-atomic regexp and non-empty string. |
73 r = /\d/; | 73 r = /\d/; |
74 r.lastIndex = 1; | 74 r.lastIndex = 1; |
75 "zzzz".replace(r, "a"); | 75 "zzzz".replace(r, "a"); |
76 assertEquals(0, r.lastIndex); | 76 assertEquals(1, r.lastIndex); |
77 | 77 |
78 // Test String.prototype.replace with replacement function | 78 // Test String.prototype.replace with replacement function |
79 r = /a/; | 79 r = /a/; |
80 r.lastIndex = 1; | 80 r.lastIndex = 1; |
81 "zzzz".replace(r, function() { return ""; }); | 81 "zzzz".replace(r, function() { return ""; }); |
82 assertEquals(0, r.lastIndex); | 82 assertEquals(1, r.lastIndex); |
83 | 83 |
84 // Regexp functions that returns multiple results: | 84 // Regexp functions that returns multiple results: |
85 // A global regexp always resets lastIndex regardless of whether it matches. | 85 // A global regexp always resets lastIndex regardless of whether it matches. |
86 r = /a/g; | 86 r = /a/g; |
87 r.lastIndex = -1; | 87 r.lastIndex = -1; |
88 "0123abcd".replace(r, "x"); | 88 "0123abcd".replace(r, "x"); |
89 assertEquals(0, r.lastIndex); | 89 assertEquals(0, r.lastIndex); |
90 | 90 |
91 r.lastIndex = -1; | 91 r.lastIndex = -1; |
92 "01234567".replace(r, "x"); | 92 "01234567".replace(r, "x"); |
93 assertEquals(0, r.lastIndex); | 93 assertEquals(0, r.lastIndex); |
94 | 94 |
95 r.lastIndex = -1; | 95 r.lastIndex = -1; |
96 "0123abcd".match(r); | 96 "0123abcd".match(r); |
97 assertEquals(0, r.lastIndex); | 97 assertEquals(0, r.lastIndex); |
98 | 98 |
99 r.lastIndex = -1; | 99 r.lastIndex = -1; |
100 "01234567".match(r); | 100 "01234567".match(r); |
101 assertEquals(0, r.lastIndex); | 101 assertEquals(0, r.lastIndex); |
102 | 102 |
103 // A non-global regexp resets lastIndex iff it does not match. | 103 // A non-global regexp resets lastIndex iff it is sticky. |
104 r = /a/; | 104 r = /a/; |
105 r.lastIndex = -1; | 105 r.lastIndex = -1; |
106 "0123abcd".replace(r, "x"); | 106 "0123abcd".replace(r, "x"); |
107 assertEquals(-1, r.lastIndex); | 107 assertEquals(-1, r.lastIndex); |
108 | 108 |
109 r.lastIndex = -1; | 109 r.lastIndex = -1; |
110 "01234567".replace(r, "x"); | 110 "01234567".replace(r, "x"); |
111 assertEquals(0, r.lastIndex); | 111 assertEquals(-1, r.lastIndex); |
112 | 112 |
113 r.lastIndex = -1; | 113 r.lastIndex = -1; |
114 "0123abcd".match(r); | 114 "0123abcd".match(r); |
115 assertEquals(-1, r.lastIndex); | 115 assertEquals(-1, r.lastIndex); |
116 | 116 |
117 r.lastIndex = -1; | 117 r.lastIndex = -1; |
118 "01234567".match(r); | 118 "01234567".match(r); |
119 assertEquals(-1, r.lastIndex); | 119 assertEquals(-1, r.lastIndex); |
120 | 120 |
| 121 r = /a/y; |
| 122 r.lastIndex = -1; |
| 123 "0123abcd".replace(r, "x"); |
| 124 assertEquals(0, r.lastIndex); |
| 125 |
| 126 r.lastIndex = -1; |
| 127 "01234567".replace(r, "x"); |
| 128 assertEquals(0, r.lastIndex); |
| 129 |
| 130 |
121 // Also test RegExp.prototype.exec and RegExp.prototype.test | 131 // Also test RegExp.prototype.exec and RegExp.prototype.test |
122 r = /a/g; | 132 r = /a/g; |
123 r.lastIndex = 1; | 133 r.lastIndex = 1; |
124 r.exec("01234567"); | 134 r.exec("01234567"); |
125 assertEquals(0, r.lastIndex); | 135 assertEquals(0, r.lastIndex); |
126 | 136 |
127 r.lastIndex = 1; | 137 r.lastIndex = 1; |
128 r.exec("0123abcd"); | 138 r.exec("0123abcd"); |
129 assertEquals(5, r.lastIndex); | 139 assertEquals(5, r.lastIndex); |
130 | 140 |
(...skipping 16 matching lines...) Expand all Loading... |
147 assertEquals(5, r.lastIndex); | 157 assertEquals(5, r.lastIndex); |
148 | 158 |
149 r = /a/; | 159 r = /a/; |
150 r.lastIndex = 1; | 160 r.lastIndex = 1; |
151 r.test("01234567"); | 161 r.test("01234567"); |
152 assertEquals(1, r.lastIndex); | 162 assertEquals(1, r.lastIndex); |
153 | 163 |
154 r.lastIndex = 1; | 164 r.lastIndex = 1; |
155 r.test("0123abcd"); | 165 r.test("0123abcd"); |
156 assertEquals(1, r.lastIndex); | 166 assertEquals(1, r.lastIndex); |
OLD | NEW |