Chromium Code Reviews| Index: src/string.js |
| =================================================================== |
| --- src/string.js (revision 5973) |
| +++ src/string.js (working copy) |
| @@ -101,28 +101,28 @@ |
| // ECMA-262 section 15.5.4.7 |
| -function StringIndexOf(searchString /* position */) { // length == 1 |
| - var subject_str = TO_STRING_INLINE(this); |
| - var pattern_str = TO_STRING_INLINE(searchString); |
| - var subject_str_len = subject_str.length; |
| - var pattern_str_len = pattern_str.length; |
| +function StringIndexOf(pattern /* position */) { // length == 1 |
| + var subject = TO_STRING_INLINE(this); |
| + var pattern = TO_STRING_INLINE(pattern); |
| + var subject_len = subject.length; |
| + var pattern_len = pattern.length; |
| var index = 0; |
| if (%_ArgumentsLength() > 1) { |
| var arg1 = %_Arguments(1); // position |
| index = TO_INTEGER(arg1); |
| } |
| if (index < 0) index = 0; |
| - if (index > subject_str_len) index = subject_str_len; |
| - if (pattern_str_len + index > subject_str_len) return -1; |
| - return %StringIndexOf(subject_str, pattern_str, index); |
| + if (index > subject_len) index = subject_len; |
| + if (pattern_len + index > subject_len) return -1; |
| + return %StringIndexOf(subject, pattern, index); |
| } |
| // ECMA-262 section 15.5.4.8 |
| -function StringLastIndexOf(searchString /* position */) { // length == 1 |
| +function StringLastIndexOf(pat /* position */) { // length == 1 |
| var sub = TO_STRING_INLINE(this); |
| var subLength = sub.length; |
| - var pat = TO_STRING_INLINE(searchString); |
| + var pat = TO_STRING_INLINE(pat); |
| var patLength = pat.length; |
| var index = subLength - patLength; |
| if (%_ArgumentsLength() > 1) { |
| @@ -150,10 +150,8 @@ |
| // do anything locale specific. |
| function StringLocaleCompare(other) { |
| if (%_ArgumentsLength() === 0) return 0; |
| - |
| - var this_str = TO_STRING_INLINE(this); |
| - var other_str = TO_STRING_INLINE(other); |
| - return %StringLocaleCompare(this_str, other_str); |
| + return %StringLocaleCompare(TO_STRING_INLINE(this), |
| + TO_STRING_INLINE(other)); |
| } |
| @@ -177,9 +175,7 @@ |
| // otherwise we call the runtime system. |
| function SubString(string, start, end) { |
| // Use the one character string cache. |
| - if (start + 1 == end) { |
| - return %_StringCharAt(string, start); |
| - } |
| + if (start + 1 == end) return %_StringCharAt(string, start); |
| return %_SubString(string, start, end); |
| } |
| @@ -208,7 +204,10 @@ |
| replace); |
| } |
| } else { |
| - return StringReplaceRegExp(subject, search, replace); |
| + return %StringReplaceRegExpWithString(subject, |
| + search, |
| + TO_STRING_INLINE(replace), |
| + lastMatchInfo); |
| } |
| } |
| @@ -224,7 +223,11 @@ |
| // Compute the string to replace with. |
| if (IS_FUNCTION(replace)) { |
| - builder.add(replace.call(null, search, start, subject)); |
| + builder.add(%_CallFunction(%GetGlobalReceiver(), |
| + search, |
| + start, |
| + subject, |
| + replace)); |
| } else { |
| reusableMatchInfo[CAPTURE0] = start; |
| reusableMatchInfo[CAPTURE1] = end; |
| @@ -239,15 +242,6 @@ |
| } |
| -// Helper function for regular expressions in String.prototype.replace. |
| -function StringReplaceRegExp(subject, regexp, replace) { |
| - return %StringReplaceRegExpWithString(subject, |
| - regexp, |
| - TO_STRING_INLINE(replace), |
| - lastMatchInfo); |
| -} |
| - |
| - |
| // Expand the $-expressions in the string and return a new string with |
| // the result. |
| function ExpandReplacement(string, subject, matchInfo, builder) { |
| @@ -408,9 +402,7 @@ |
| lastMatchInfoOverride = override; |
| var func_result = |
| %_CallFunction(receiver, elem, match_start, subject, replace); |
| - if (!IS_STRING(func_result)) { |
| - func_result = NonStringToString(func_result); |
| - } |
| + func_result = TO_STRING_INLINE(func_result); |
| res[i] = func_result; |
| match_start += elem.length; |
| } |
| @@ -424,9 +416,7 @@ |
| // Use the apply argument as backing for global RegExp properties. |
| lastMatchInfoOverride = elem; |
| var func_result = replace.apply(null, elem); |
| - if (!IS_STRING(func_result)) { |
| - func_result = NonStringToString(func_result); |
| - } |
| + func_result = TO_STRING_INLINE(func_result); |
| res[i] = func_result; |
| } |
| i++; |
| @@ -487,8 +477,7 @@ |
| } else { |
| regexp = new $RegExp(re); |
| } |
| - var s = TO_STRING_INLINE(this); |
| - var match = DoRegExpExec(regexp, s, 0); |
| + var match = DoRegExpExec(regexp, TO_STRING_INLINE(this), 0); |
| if (match) { |
| return match[CAPTURE0]; |
| } |
| @@ -576,14 +565,14 @@ |
| while (true) { |
| if (startIndex === length) { |
| - result[result.length] = subject.slice(currentIndex, length); |
| + result.push(subject.slice(currentIndex, length)); |
| break; |
| } |
| var matchInfo = splitMatch(separator, subject, currentIndex, startIndex); |
| if (IS_NULL(matchInfo)) { |
| - result[result.length] = subject.slice(currentIndex, length); |
| + result.push(subject.slice(currentIndex, length)); |
| break; |
| } |
| @@ -595,17 +584,22 @@ |
| continue; |
| } |
| - result[result.length] = SubString(subject, currentIndex, matchInfo[CAPTURE0]); |
| + result.push(SubString(subject, currentIndex, matchInfo[CAPTURE0])); |
| if (result.length === limit) break; |
| - var num_captures = NUMBER_OF_CAPTURES(matchInfo); |
| - for (var i = 2; i < num_captures; i += 2) { |
| - var start = matchInfo[CAPTURE(i)]; |
| - var end = matchInfo[CAPTURE(i + 1)]; |
| - if (start != -1 && end != -1) { |
| - result[result.length] = SubString(subject, start, end); |
| + var num_captures_plus3 = |
|
Lasse Reichstein
2010/12/13 12:08:58
"_plus3" might be a little too specific.
How about
sandholm
2010/12/13 12:18:15
Done.
|
| + NUMBER_OF_CAPTURES(matchInfo) + REGEXP_FIRST_CAPTURE; |
| + for (var i = REGEXP_FIRST_CAPTURE + 2; i < num_captures_plus3; ) { |
| + var start = matchInfo[i++]; |
| + var end = matchInfo[i++]; |
| + if (end != -1) { |
| + if (start + 1 == end) { |
| + result.push(%_StringCharAt(subject, start)); |
| + } else { |
| + result.push(%_SubString(subject, start, end)); |
| + } |
| } else { |
| - result[result.length] = void 0; |
| + result.push(void 0); |
| } |
| if (result.length === limit) break outer_loop; |
| } |
| @@ -656,7 +650,9 @@ |
| } |
| } |
| - return SubString(s, start_i, end_i); |
| + return (start_i + 1 == end_i |
| + ? %_StringCharAt(s, start_i) |
| + : %_SubString(s, start_i, end_i)); |
| } |
| @@ -694,7 +690,9 @@ |
| var end = start + len; |
| if (end > s.length) end = s.length; |
| - return SubString(s, start, end); |
| + return (start + 1 == end |
| + ? %_StringCharAt(s, start) |
| + : %_SubString(s, start, end)); |
| } |