Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(303)

Side by Side Diff: src/js/string.js

Issue 2332333002: [regexp/string] Merge ExpandReplacement and GetSubstitution (Closed)
Patch Set: Fix test failures Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/js/regexp.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function(global, utils) { 5 (function(global, utils) {
6 6
7 %CheckIsBootstrapping(); 7 %CheckIsBootstrapping();
8 8
9 // ------------------------------------------------------------------- 9 // -------------------------------------------------------------------
10 // Imports 10 // Imports
11 11
12 var ArrayJoin; 12 var ArrayJoin;
13 var GetSubstitution;
13 var GlobalRegExp = global.RegExp; 14 var GlobalRegExp = global.RegExp;
14 var GlobalString = global.String; 15 var GlobalString = global.String;
15 var IsRegExp; 16 var IsRegExp;
16 var MaxSimple; 17 var MaxSimple;
17 var MinSimple; 18 var MinSimple;
18 var RegExpInitialize; 19 var RegExpInitialize;
19 var matchSymbol = utils.ImportNow("match_symbol"); 20 var matchSymbol = utils.ImportNow("match_symbol");
20 var replaceSymbol = utils.ImportNow("replace_symbol"); 21 var replaceSymbol = utils.ImportNow("replace_symbol");
21 var searchSymbol = utils.ImportNow("search_symbol"); 22 var searchSymbol = utils.ImportNow("search_symbol");
22 var splitSymbol = utils.ImportNow("split_symbol"); 23 var splitSymbol = utils.ImportNow("split_symbol");
23 24
24 utils.Import(function(from) { 25 utils.Import(function(from) {
25 ArrayJoin = from.ArrayJoin; 26 ArrayJoin = from.ArrayJoin;
27 GetSubstitution = from.GetSubstitution;
26 IsRegExp = from.IsRegExp; 28 IsRegExp = from.IsRegExp;
27 MaxSimple = from.MaxSimple; 29 MaxSimple = from.MaxSimple;
28 MinSimple = from.MinSimple; 30 MinSimple = from.MinSimple;
29 RegExpInitialize = from.RegExpInitialize; 31 RegExpInitialize = from.RegExpInitialize;
30 }); 32 });
31 33
32 //------------------------------------------------------------------- 34 //-------------------------------------------------------------------
33 35
34 // ECMA-262, section 15.5.4.6 36 // ECMA-262, section 15.5.4.6
35 function StringConcat(other /* and more */) { // length == 1 37 function StringConcat(other /* and more */) { // length == 1
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 102
101 var subject = TO_STRING(this); 103 var subject = TO_STRING(this);
102 104
103 // Equivalent to RegExpCreate (ES#sec-regexpcreate) 105 // Equivalent to RegExpCreate (ES#sec-regexpcreate)
104 var regexp = %_NewObject(GlobalRegExp, GlobalRegExp); 106 var regexp = %_NewObject(GlobalRegExp, GlobalRegExp);
105 RegExpInitialize(regexp, pattern); 107 RegExpInitialize(regexp, pattern);
106 return regexp[matchSymbol](subject); 108 return regexp[matchSymbol](subject);
107 } 109 }
108 110
109 111
110 // This has the same size as the RegExpLastMatchInfo array, and can be used
111 // for functions that expect that structure to be returned. It is used when
112 // the needle is a string rather than a regexp. In this case we can't update
113 // lastMatchArray without erroneously affecting the properties on the global
114 // RegExp object.
115 var reusableMatchInfo = [2, "", "", -1, -1];
116
117
118 // ES6, section 21.1.3.14 112 // ES6, section 21.1.3.14
119 function StringReplace(search, replace) { 113 function StringReplace(search, replace) {
120 CHECK_OBJECT_COERCIBLE(this, "String.prototype.replace"); 114 CHECK_OBJECT_COERCIBLE(this, "String.prototype.replace");
121 115
122 // Decision tree for dispatch 116 // Decision tree for dispatch
123 // .. regexp search (in src/js/regexp.js, RegExpReplace) 117 // .. regexp search (in src/js/regexp.js, RegExpReplace)
124 // .... string replace 118 // .... string replace
125 // ...... non-global search 119 // ...... non-global search
126 // ........ empty string replace 120 // ........ empty string replace
127 // ........ non-empty string replace (with $-expansion) 121 // ........ non-empty string replace (with $-expansion)
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 var start = %StringIndexOf(subject, search, 0); 153 var start = %StringIndexOf(subject, search, 0);
160 if (start < 0) return subject; 154 if (start < 0) return subject;
161 var end = start + search.length; 155 var end = start + search.length;
162 156
163 var result = %_SubString(subject, 0, start); 157 var result = %_SubString(subject, 0, start);
164 158
165 // Compute the string to replace with. 159 // Compute the string to replace with.
166 if (IS_CALLABLE(replace)) { 160 if (IS_CALLABLE(replace)) {
167 result += replace(search, start, subject); 161 result += replace(search, start, subject);
168 } else { 162 } else {
169 reusableMatchInfo[CAPTURE0] = start; 163 // In this case, we don't have any capture groups and can get away with
170 reusableMatchInfo[CAPTURE1] = end; 164 // faking the captures object by simply setting its length to 1.
171 result = ExpandReplacement(TO_STRING(replace), 165 const captures = { length: 1 };
172 subject, 166 const matched = %_SubString(subject, start, end);
173 reusableMatchInfo, 167 result += GetSubstitution(matched, subject, start, captures,
174 result); 168 TO_STRING(replace));
175 } 169 }
176 170
177 return result + %_SubString(subject, end, subject.length); 171 return result + %_SubString(subject, end, subject.length);
178 } 172 }
179 173
180 174
181 // Expand the $-expressions in the string and return a new string with
182 // the result.
183 function ExpandReplacement(string, subject, matchInfo, result) {
184 var length = string.length;
185 var next = %StringIndexOf(string, '$', 0);
186 if (next < 0) {
187 if (length > 0) result += string;
188 return result;
189 }
190
191 if (next > 0) result += %_SubString(string, 0, next);
192
193 while (true) {
194 var expansion = '$';
195 var position = next + 1;
196 if (position < length) {
197 var peek = %_StringCharCodeAt(string, position);
198 if (peek == 36) { // $$
199 ++position;
200 result += '$';
201 } else if (peek == 38) { // $& - match
202 ++position;
203 result +=
204 %_SubString(subject, matchInfo[CAPTURE0], matchInfo[CAPTURE1]);
205 } else if (peek == 96) { // $` - prefix
206 ++position;
207 result += %_SubString(subject, 0, matchInfo[CAPTURE0]);
208 } else if (peek == 39) { // $' - suffix
209 ++position;
210 result += %_SubString(subject, matchInfo[CAPTURE1], subject.length);
211 } else if (peek >= 48 && peek <= 57) {
212 // Valid indices are $1 .. $9, $01 .. $09 and $10 .. $99
213 var scaled_index = (peek - 48) << 1;
214 var advance = 1;
215 var number_of_captures = NUMBER_OF_CAPTURES(matchInfo);
216 if (position + 1 < string.length) {
217 var next = %_StringCharCodeAt(string, position + 1);
218 if (next >= 48 && next <= 57) {
219 var new_scaled_index = scaled_index * 10 + ((next - 48) << 1);
220 if (new_scaled_index < number_of_captures) {
221 scaled_index = new_scaled_index;
222 advance = 2;
223 }
224 }
225 }
226 if (scaled_index != 0 && scaled_index < number_of_captures) {
227 var start = matchInfo[CAPTURE(scaled_index)];
228 if (start >= 0) {
229 result +=
230 %_SubString(subject, start, matchInfo[CAPTURE(scaled_index + 1)]);
231 }
232 position += advance;
233 } else {
234 result += '$';
235 }
236 } else {
237 result += '$';
238 }
239 } else {
240 result += '$';
241 }
242
243 // Go the the next $ in the string.
244 next = %StringIndexOf(string, '$', position);
245
246 // Return if there are no more $ characters in the string. If we
247 // haven't reached the end, we need to append the suffix.
248 if (next < 0) {
249 if (position < length) {
250 result += %_SubString(string, position, length);
251 }
252 return result;
253 }
254
255 // Append substring between the previous and the next $ character.
256 if (next > position) {
257 result += %_SubString(string, position, next);
258 }
259 }
260 return result;
261 }
262
263
264 // ES6 21.1.3.15. 175 // ES6 21.1.3.15.
265 function StringSearch(pattern) { 176 function StringSearch(pattern) {
266 CHECK_OBJECT_COERCIBLE(this, "String.prototype.search"); 177 CHECK_OBJECT_COERCIBLE(this, "String.prototype.search");
267 178
268 if (!IS_NULL_OR_UNDEFINED(pattern)) { 179 if (!IS_NULL_OR_UNDEFINED(pattern)) {
269 var searcher = pattern[searchSymbol]; 180 var searcher = pattern[searchSymbol];
270 if (!IS_UNDEFINED(searcher)) { 181 if (!IS_UNDEFINED(searcher)) {
271 return %_Call(searcher, pattern, this); 182 return %_Call(searcher, pattern, this);
272 } 183 }
273 } 184 }
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after
729 "small", StringSmall, 640 "small", StringSmall,
730 "strike", StringStrike, 641 "strike", StringStrike,
731 "sub", StringSub, 642 "sub", StringSub,
732 "sup", StringSup 643 "sup", StringSup
733 ]); 644 ]);
734 645
735 // ------------------------------------------------------------------- 646 // -------------------------------------------------------------------
736 // Exports 647 // Exports
737 648
738 utils.Export(function(to) { 649 utils.Export(function(to) {
739 to.ExpandReplacement = ExpandReplacement;
740 to.StringIndexOf = StringIndexOf; 650 to.StringIndexOf = StringIndexOf;
741 to.StringLastIndexOf = StringLastIndexOf; 651 to.StringLastIndexOf = StringLastIndexOf;
742 to.StringMatch = StringMatchJS; 652 to.StringMatch = StringMatchJS;
743 to.StringReplace = StringReplace; 653 to.StringReplace = StringReplace;
744 to.StringSlice = StringSlice; 654 to.StringSlice = StringSlice;
745 to.StringSplit = StringSplitJS; 655 to.StringSplit = StringSplitJS;
746 to.StringSubstr = StringSubstr; 656 to.StringSubstr = StringSubstr;
747 to.StringSubstring = StringSubstring; 657 to.StringSubstring = StringSubstring;
748 }); 658 });
749 659
750 }) 660 })
OLDNEW
« no previous file with comments | « src/js/regexp.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698