OLD | NEW |
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 MakeRangeError; | 18 var MakeRangeError; |
19 var MakeTypeError; | 19 var MakeTypeError; |
| 20 var MathMax; |
| 21 var MathMin; |
20 var RegExpExec; | 22 var RegExpExec; |
21 var RegExpExecNoTests; | 23 var RegExpExecNoTests; |
22 var RegExpLastMatchInfo; | 24 var RegExpLastMatchInfo; |
23 var splitSymbol = utils.ImportNow("split_symbol"); | 25 var splitSymbol = utils.ImportNow("split_symbol"); |
24 | 26 |
25 utils.Import(function(from) { | 27 utils.Import(function(from) { |
26 ArrayIndexOf = from.ArrayIndexOf; | 28 ArrayIndexOf = from.ArrayIndexOf; |
27 ArrayJoin = from.ArrayJoin; | 29 ArrayJoin = from.ArrayJoin; |
28 MakeRangeError = from.MakeRangeError; | 30 MakeRangeError = from.MakeRangeError; |
29 MakeTypeError = from.MakeTypeError; | 31 MakeTypeError = from.MakeTypeError; |
| 32 MathMax = from.MathMax; |
| 33 MathMin = from.MathMin; |
30 RegExpExec = from.RegExpExec; | 34 RegExpExec = from.RegExpExec; |
31 RegExpExecNoTests = from.RegExpExecNoTests; | 35 RegExpExecNoTests = from.RegExpExecNoTests; |
32 RegExpLastMatchInfo = from.RegExpLastMatchInfo; | 36 RegExpLastMatchInfo = from.RegExpLastMatchInfo; |
33 }); | 37 }); |
34 | 38 |
35 //------------------------------------------------------------------- | 39 //------------------------------------------------------------------- |
36 | 40 |
37 // ECMA-262 section 15.5.4.2 | 41 // ECMA-262 section 15.5.4.2 |
38 function StringToString() { | 42 function StringToString() { |
39 if (!IS_STRING(this) && !IS_STRING_WRAPPER(this)) { | 43 if (!IS_STRING(this) && !IS_STRING_WRAPPER(this)) { |
(...skipping 832 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
872 var ss = TO_STRING(searchString); | 876 var ss = TO_STRING(searchString); |
873 var pos = 0; | 877 var pos = 0; |
874 if (%_ArgumentsLength() > 1) { | 878 if (%_ArgumentsLength() > 1) { |
875 var arg = %_Arguments(1); // position | 879 var arg = %_Arguments(1); // position |
876 if (!IS_UNDEFINED(arg)) { | 880 if (!IS_UNDEFINED(arg)) { |
877 pos = TO_INTEGER(arg); | 881 pos = TO_INTEGER(arg); |
878 } | 882 } |
879 } | 883 } |
880 | 884 |
881 var s_len = s.length; | 885 var s_len = s.length; |
882 if (pos < 0) pos = 0; | 886 var start = MathMin(MathMax(pos, 0), s_len); |
883 if (pos > s_len) pos = s_len; | |
884 var ss_len = ss.length; | 887 var ss_len = ss.length; |
885 | 888 if (ss_len + start > s_len) { |
886 if (ss_len + pos > s_len) { | |
887 return false; | 889 return false; |
888 } | 890 } |
889 | 891 |
890 for (var i = 0; i < ss_len; i++) { | 892 return %_SubString(s, start, start + ss_len) === ss; |
891 if (%_StringCharCodeAt(s, pos + i) !== %_StringCharCodeAt(ss, i)) { | |
892 return false; | |
893 } | |
894 } | |
895 | |
896 return true; | |
897 } | 893 } |
898 | 894 |
899 | 895 |
900 // ES6 draft 04-05-14, section 21.1.3.7 | 896 // ES6 draft 04-05-14, section 21.1.3.7 |
901 function StringEndsWith(searchString /* position */) { // length == 1 | 897 function StringEndsWith(searchString /* position */) { // length == 1 |
902 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith"); | 898 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith"); |
903 | 899 |
904 var s = TO_STRING(this); | 900 var s = TO_STRING(this); |
905 | 901 |
906 if (IS_REGEXP(searchString)) { | 902 if (IS_REGEXP(searchString)) { |
907 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.endsWith"); | 903 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.endsWith"); |
908 } | 904 } |
909 | 905 |
910 var ss = TO_STRING(searchString); | 906 var ss = TO_STRING(searchString); |
911 var s_len = s.length; | 907 var s_len = s.length; |
912 var pos = s_len; | 908 var pos = s_len; |
913 if (%_ArgumentsLength() > 1) { | 909 if (%_ArgumentsLength() > 1) { |
914 var arg = %_Arguments(1); // position | 910 var arg = %_Arguments(1); // position |
915 if (!IS_UNDEFINED(arg)) { | 911 if (!IS_UNDEFINED(arg)) { |
916 pos = TO_INTEGER(arg); | 912 pos = TO_INTEGER(arg); |
917 } | 913 } |
918 } | 914 } |
919 | 915 |
920 if (pos < 0) pos = 0; | 916 var end = MathMin(MathMax(pos, 0), s_len); |
921 if (pos > s_len) pos = s_len; | |
922 var ss_len = ss.length; | 917 var ss_len = ss.length; |
923 pos = pos - ss_len; | 918 var start = end - ss_len; |
924 | 919 if (start < 0) { |
925 if (pos < 0) { | |
926 return false; | 920 return false; |
927 } | 921 } |
928 | 922 |
929 for (var i = 0; i < ss_len; i++) { | 923 return %_SubString(s, start, start + ss_len) === ss; |
930 if (%_StringCharCodeAt(s, pos + i) !== %_StringCharCodeAt(ss, i)) { | |
931 return false; | |
932 } | |
933 } | |
934 | |
935 return true; | |
936 } | 924 } |
937 | 925 |
938 | 926 |
939 // ES6 draft 04-05-14, section 21.1.3.6 | 927 // ES6 draft 04-05-14, section 21.1.3.6 |
940 function StringIncludes(searchString /* position */) { // length == 1 | 928 function StringIncludes(searchString /* position */) { // length == 1 |
941 CHECK_OBJECT_COERCIBLE(this, "String.prototype.includes"); | 929 CHECK_OBJECT_COERCIBLE(this, "String.prototype.includes"); |
942 | 930 |
943 var string = TO_STRING(this); | 931 var string = TO_STRING(this); |
944 | 932 |
945 if (IS_REGEXP(searchString)) { | 933 if (IS_REGEXP(searchString)) { |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1109 to.StringLastIndexOf = StringLastIndexOfJS; | 1097 to.StringLastIndexOf = StringLastIndexOfJS; |
1110 to.StringMatch = StringMatchJS; | 1098 to.StringMatch = StringMatchJS; |
1111 to.StringReplace = StringReplace; | 1099 to.StringReplace = StringReplace; |
1112 to.StringSlice = StringSlice; | 1100 to.StringSlice = StringSlice; |
1113 to.StringSplit = StringSplitJS; | 1101 to.StringSplit = StringSplitJS; |
1114 to.StringSubstr = StringSubstr; | 1102 to.StringSubstr = StringSubstr; |
1115 to.StringSubstring = StringSubstring; | 1103 to.StringSubstring = StringSubstring; |
1116 }); | 1104 }); |
1117 | 1105 |
1118 }) | 1106 }) |
OLD | NEW |