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

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: Use codestyle from function 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 942 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 pos = %_Arguments(1); // position
990 pos = $toInteger(pos); 986 pos = $toInteger(pos);
991 } 987 }
992 988
993 var s_len = s.length; 989 var s_len = s.length;
994 var start = MathMin(MathMax(pos, 0), s_len); 990 if (pos < 0) pos = 0;
991 if (pos > s_len) pos = s_len;
995 var ss_len = ss.length; 992 var ss_len = ss.length;
996 if (ss_len + start > s_len) { 993
994 if (ss_len + pos > s_len) {
997 return false; 995 return false;
998 } 996 }
999 997
1000 return %_SubString(s, start, start + ss_len) === ss; 998 for (var i = 0; i < ss_len; i++) {
999 if (%_StringCharCodeAt(s, pos + i) !== %_StringCharCodeAt(ss, i))
1000 return false;
1001 }
1002
1003 return true;
1001 } 1004 }
1002 1005
1003 1006
1004 // ES6 draft 04-05-14, section 21.1.3.7 1007 // ES6 draft 04-05-14, section 21.1.3.7
1005 function StringEndsWith(searchString /* position */) { // length == 1 1008 function StringEndsWith(searchString /* position */) { // length == 1
1006 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith"); 1009 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith");
1007 1010
1008 var s = TO_STRING_INLINE(this); 1011 var s = TO_STRING_INLINE(this);
1009 1012
1010 if (IS_REGEXP(searchString)) { 1013 if (IS_REGEXP(searchString)) {
1011 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.endsWith"); 1014 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.endsWith");
1012 } 1015 }
1013 1016
1014 var ss = TO_STRING_INLINE(searchString); 1017 var ss = TO_STRING_INLINE(searchString);
1015 var s_len = s.length; 1018 var s_len = s.length;
1016 var pos = s_len; 1019 var pos = s_len;
1017 if (%_ArgumentsLength() > 1) { 1020 if (%_ArgumentsLength() > 1) {
1018 var arg = %_Arguments(1); // position 1021 var arg = %_Arguments(1); // position
1019 if (!IS_UNDEFINED(arg)) { 1022 if (!IS_UNDEFINED(arg)) {
1020 pos = $toInteger(arg); 1023 pos = $toInteger(arg);
1021 } 1024 }
1022 } 1025 }
1023 1026
1024 var end = MathMin(MathMax(pos, 0), s_len); 1027 if (pos < 0) pos = 0;
1028 if (pos > s_len) pos = s_len;
1025 var ss_len = ss.length; 1029 var ss_len = ss.length;
1026 var start = end - ss_len; 1030 pos = pos - ss_len;
1027 if (start < 0) { 1031
1032 if (pos < 0) {
1028 return false; 1033 return false;
1029 } 1034 }
1030 1035
1031 return %_SubString(s, start, start + ss_len) === ss; 1036 for (var i = 0; i < ss_len; i++) {
1037 if (%_StringCharCodeAt(s, pos + i) !== %_StringCharCodeAt(ss, i))
1038 return false;
1039 }
1040
1041 return true;
Dan Ehrenberg 2015/09/08 07:27:58 There's some code duplication between these two fu
Jakob Kummerow 2015/09/08 08:13:03 How do you mark a JavaScript function inline? :-)
1032 } 1042 }
1033 1043
1034 1044
1035 // ES6 draft 04-05-14, section 21.1.3.6 1045 // ES6 draft 04-05-14, section 21.1.3.6
1036 function StringIncludes(searchString /* position */) { // length == 1 1046 function StringIncludes(searchString /* position */) { // length == 1
1037 CHECK_OBJECT_COERCIBLE(this, "String.prototype.includes"); 1047 CHECK_OBJECT_COERCIBLE(this, "String.prototype.includes");
1038 1048
1039 var string = TO_STRING_INLINE(this); 1049 var string = TO_STRING_INLINE(this);
1040 1050
1041 if (IS_REGEXP(searchString)) { 1051 if (IS_REGEXP(searchString)) {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 to.StringLastIndexOf = StringLastIndexOfJS; 1216 to.StringLastIndexOf = StringLastIndexOfJS;
1207 to.StringMatch = StringMatchJS; 1217 to.StringMatch = StringMatchJS;
1208 to.StringReplace = StringReplace; 1218 to.StringReplace = StringReplace;
1209 to.StringSlice = StringSlice; 1219 to.StringSlice = StringSlice;
1210 to.StringSplit = StringSplitJS; 1220 to.StringSplit = StringSplitJS;
1211 to.StringSubstr = StringSubstr; 1221 to.StringSubstr = StringSubstr;
1212 to.StringSubstring = StringSubstring; 1222 to.StringSubstring = StringSubstring;
1213 }); 1223 });
1214 1224
1215 }) 1225 })
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