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

Unified Diff: src/string.js

Issue 1324353002: [es6] Optimize String{Starts, Ends}With (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: nits Created 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/string.js
diff --git a/src/string.js b/src/string.js
index 1db65bb0bad25b677c7fa38144976c6a39f4ca18..97ed3cdb6e7c2b502f19d16b60528522f25c77e2 100644
--- a/src/string.js
+++ b/src/string.js
@@ -15,8 +15,6 @@ var GlobalRegExp = global.RegExp;
var GlobalString = global.String;
var InternalArray = utils.InternalArray;
var InternalPackedArray = utils.InternalPackedArray;
-var MathMax;
-var MathMin;
var RegExpExec;
var RegExpExecNoTests;
var RegExpLastMatchInfo;
@@ -27,8 +25,6 @@ var ToString;
utils.Import(function(from) {
ArrayIndexOf = from.ArrayIndexOf;
ArrayJoin = from.ArrayJoin;
- MathMax = from.MathMax;
- MathMin = from.MathMin;
RegExpExec = from.RegExpExec;
RegExpExecNoTests = from.RegExpExecNoTests;
RegExpLastMatchInfo = from.RegExpLastMatchInfo;
@@ -986,18 +982,28 @@ function StringStartsWith(searchString /* position */) { // length == 1
var ss = TO_STRING_INLINE(searchString);
var pos = 0;
if (%_ArgumentsLength() > 1) {
- pos = %_Arguments(1); // position
- pos = $toInteger(pos);
+ var arg = %_Arguments(1); // position
+ if (!IS_UNDEFINED(arg)) {
+ pos = $toInteger(arg);
+ }
}
var s_len = s.length;
- var start = MathMin(MathMax(pos, 0), s_len);
+ if (pos < 0) pos = 0;
+ if (pos > s_len) pos = s_len;
Lasse Reichstein Nielsen 2015/09/08 10:24:57 DBC: Would it be useful to make this an "else" of
var ss_len = ss.length;
- if (ss_len + start > s_len) {
+
+ if (ss_len + pos > s_len) {
return false;
}
- return %_SubString(s, start, start + ss_len) === ss;
+ for (var i = 0; i < ss_len; i++) {
+ if (%_StringCharCodeAt(s, pos + i) !== %_StringCharCodeAt(ss, i)) {
+ return false;
+ }
+ }
+
+ return true;
}
@@ -1021,14 +1027,22 @@ function StringEndsWith(searchString /* position */) { // length == 1
}
}
- var end = MathMin(MathMax(pos, 0), s_len);
+ if (pos < 0) pos = 0;
+ if (pos > s_len) pos = s_len;
var ss_len = ss.length;
- var start = end - ss_len;
- if (start < 0) {
+ pos = pos - ss_len;
+
+ if (pos < 0) {
return false;
}
Lasse Reichstein Nielsen 2015/09/08 10:24:57 The spec is really overspecifying here - if search
- return %_SubString(s, start, start + ss_len) === ss;
+ for (var i = 0; i < ss_len; i++) {
+ if (%_StringCharCodeAt(s, pos + i) !== %_StringCharCodeAt(ss, i)) {
+ return false;
+ }
+ }
+
+ return true;
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698