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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | 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 ArrayIndexOf; 12 var ArrayIndexOf;
13 var ArrayJoin; 13 var ArrayJoin;
14 var GlobalRegExp = global.RegExp; 14 var GlobalRegExp = global.RegExp;
15 var GlobalString = global.String; 15 var GlobalString = global.String;
16 var InternalArray = utils.InternalArray; 16 var InternalArray = utils.InternalArray;
17 var InternalPackedArray = utils.InternalPackedArray; 17 var InternalPackedArray = utils.InternalPackedArray;
18 var MathMax;
19 var MathMin;
20 var RegExpExec; 18 var RegExpExec;
21 var RegExpExecNoTests; 19 var RegExpExecNoTests;
22 var RegExpLastMatchInfo; 20 var RegExpLastMatchInfo;
23 var SymbolToString; 21 var SymbolToString;
24 var ToNumber; 22 var ToNumber;
25 var ToString; 23 var ToString;
26 24
27 utils.Import(function(from) { 25 utils.Import(function(from) {
28 ArrayIndexOf = from.ArrayIndexOf; 26 ArrayIndexOf = from.ArrayIndexOf;
29 ArrayJoin = from.ArrayJoin; 27 ArrayJoin = from.ArrayJoin;
30 MathMax = from.MathMax;
31 MathMin = from.MathMin;
32 RegExpExec = from.RegExpExec; 28 RegExpExec = from.RegExpExec;
33 RegExpExecNoTests = from.RegExpExecNoTests; 29 RegExpExecNoTests = from.RegExpExecNoTests;
34 RegExpLastMatchInfo = from.RegExpLastMatchInfo; 30 RegExpLastMatchInfo = from.RegExpLastMatchInfo;
35 SymbolToString = from.SymbolToString; 31 SymbolToString = from.SymbolToString;
36 ToNumber = from.ToNumber; 32 ToNumber = from.ToNumber;
37 ToString = from.ToString; 33 ToString = from.ToString;
38 }); 34 });
39 35
40 //------------------------------------------------------------------- 36 //-------------------------------------------------------------------
41 37
(...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after
979 975
980 var s = TO_STRING_INLINE(this); 976 var s = TO_STRING_INLINE(this);
981 977
982 if (IS_REGEXP(searchString)) { 978 if (IS_REGEXP(searchString)) {
983 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.startsWith"); 979 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.startsWith");
984 } 980 }
985 981
986 var ss = TO_STRING_INLINE(searchString); 982 var ss = TO_STRING_INLINE(searchString);
987 var pos = 0; 983 var pos = 0;
988 if (%_ArgumentsLength() > 1) { 984 if (%_ArgumentsLength() > 1) {
989 pos = %_Arguments(1); // position 985 var arg = %_Arguments(1); // position
990 pos = $toInteger(pos); 986 if (!IS_UNDEFINED(arg)) {
987 pos = $toInteger(arg);
988 }
991 } 989 }
992 990
993 var s_len = s.length; 991 var s_len = s.length;
994 var start = MathMin(MathMax(pos, 0), s_len); 992 if (pos < 0) pos = 0;
993 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
995 var ss_len = ss.length; 994 var ss_len = ss.length;
996 if (ss_len + start > s_len) { 995
996 if (ss_len + pos > s_len) {
997 return false; 997 return false;
998 } 998 }
999 999
1000 return %_SubString(s, start, start + ss_len) === ss; 1000 for (var i = 0; i < ss_len; i++) {
1001 if (%_StringCharCodeAt(s, pos + i) !== %_StringCharCodeAt(ss, i)) {
1002 return false;
1003 }
1004 }
1005
1006 return true;
1001 } 1007 }
1002 1008
1003 1009
1004 // ES6 draft 04-05-14, section 21.1.3.7 1010 // ES6 draft 04-05-14, section 21.1.3.7
1005 function StringEndsWith(searchString /* position */) { // length == 1 1011 function StringEndsWith(searchString /* position */) { // length == 1
1006 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith"); 1012 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith");
1007 1013
1008 var s = TO_STRING_INLINE(this); 1014 var s = TO_STRING_INLINE(this);
1009 1015
1010 if (IS_REGEXP(searchString)) { 1016 if (IS_REGEXP(searchString)) {
1011 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.endsWith"); 1017 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.endsWith");
1012 } 1018 }
1013 1019
1014 var ss = TO_STRING_INLINE(searchString); 1020 var ss = TO_STRING_INLINE(searchString);
1015 var s_len = s.length; 1021 var s_len = s.length;
1016 var pos = s_len; 1022 var pos = s_len;
1017 if (%_ArgumentsLength() > 1) { 1023 if (%_ArgumentsLength() > 1) {
1018 var arg = %_Arguments(1); // position 1024 var arg = %_Arguments(1); // position
1019 if (!IS_UNDEFINED(arg)) { 1025 if (!IS_UNDEFINED(arg)) {
1020 pos = $toInteger(arg); 1026 pos = $toInteger(arg);
1021 } 1027 }
1022 } 1028 }
1023 1029
1024 var end = MathMin(MathMax(pos, 0), s_len); 1030 if (pos < 0) pos = 0;
1031 if (pos > s_len) pos = s_len;
1025 var ss_len = ss.length; 1032 var ss_len = ss.length;
1026 var start = end - ss_len; 1033 pos = pos - ss_len;
1027 if (start < 0) { 1034
1035 if (pos < 0) {
1028 return false; 1036 return false;
1029 } 1037 }
Lasse Reichstein Nielsen 2015/09/08 10:24:57 The spec is really overspecifying here - if search
1030 1038
1031 return %_SubString(s, start, start + ss_len) === ss; 1039 for (var i = 0; i < ss_len; i++) {
1040 if (%_StringCharCodeAt(s, pos + i) !== %_StringCharCodeAt(ss, i)) {
1041 return false;
1042 }
1043 }
1044
1045 return true;
1032 } 1046 }
1033 1047
1034 1048
1035 // ES6 draft 04-05-14, section 21.1.3.6 1049 // ES6 draft 04-05-14, section 21.1.3.6
1036 function StringIncludes(searchString /* position */) { // length == 1 1050 function StringIncludes(searchString /* position */) { // length == 1
1037 CHECK_OBJECT_COERCIBLE(this, "String.prototype.includes"); 1051 CHECK_OBJECT_COERCIBLE(this, "String.prototype.includes");
1038 1052
1039 var string = TO_STRING_INLINE(this); 1053 var string = TO_STRING_INLINE(this);
1040 1054
1041 if (IS_REGEXP(searchString)) { 1055 if (IS_REGEXP(searchString)) {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 to.StringLastIndexOf = StringLastIndexOfJS; 1220 to.StringLastIndexOf = StringLastIndexOfJS;
1207 to.StringMatch = StringMatchJS; 1221 to.StringMatch = StringMatchJS;
1208 to.StringReplace = StringReplace; 1222 to.StringReplace = StringReplace;
1209 to.StringSlice = StringSlice; 1223 to.StringSlice = StringSlice;
1210 to.StringSplit = StringSplitJS; 1224 to.StringSplit = StringSplitJS;
1211 to.StringSubstr = StringSubstr; 1225 to.StringSubstr = StringSubstr;
1212 to.StringSubstring = StringSubstring; 1226 to.StringSubstring = StringSubstring;
1213 }); 1227 });
1214 1228
1215 }) 1229 })
OLDNEW
« 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