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

Side by Side Diff: src/string.js

Issue 20818005: Implemented String.prototype.repeat as per ES6 draft 07-15-13, section 15.5.3.21 (Closed) Base URL: git://github.com/v8/v8.git@master
Patch Set: Created 7 years, 4 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 | « src/messages.js ('k') | test/mjsunit/string-repeat.js » ('j') | 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 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 864 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 var two_byte = %NewString(n - i, NEW_TWO_BYTE_STRING); 875 var two_byte = %NewString(n - i, NEW_TWO_BYTE_STRING);
876 for (var j = 0; i < n; i++, j++) { 876 for (var j = 0; i < n; i++, j++) {
877 var code = %_Arguments(i); 877 var code = %_Arguments(i);
878 if (!%_IsSmi(code)) code = ToNumber(code) & 0xffff; 878 if (!%_IsSmi(code)) code = ToNumber(code) & 0xffff;
879 %_TwoByteSeqStringSetChar(two_byte, j, code); 879 %_TwoByteSeqStringSetChar(two_byte, j, code);
880 } 880 }
881 return one_byte + two_byte; 881 return one_byte + two_byte;
882 } 882 }
883 883
884 884
885 // ES6 draft 07-15-13, section 15.5.3.21
886 function StringRepeat(count) {
887 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
888 throw MakeTypeError("called_on_null_or_undefined",
889 ["String.prototype.repeat"]);
890 }
891
Sven Panne 2013/07/29 13:27:53 We have to call TO_STRING_INLINE here, not later,
892 if (IS_NULL_OR_UNDEFINED(count) && !IS_UNDETECTABLE(count)) {
Sven Panne 2013/07/29 13:27:53 Why test this? What should really happen at that p
893 throw MakeTypeError("called_on_null_or_undefined",
894 ["String.prototype.repeat"]);
895 }
896
897 var s = TO_STRING_INLINE(this);
898 if (s.length === 0) {
Sven Panne 2013/07/29 13:27:53 This optimization is not allowed by the spec, at l
899 return s;
900 }
901
902 var n = ToNumber(count);
903 if (NUMBER_IS_NAN(n) || n < 0 || !NUMBER_IS_FINITE(n)) {
904 throw MakeRangeError("invalid_count_value", []);
905 }
906
907 n = TO_INTEGER(n);
908 if (n === 0) {
Sven Panne 2013/07/29 13:27:53 I am not sure if a zero count is a common case, we
909 return "";
910 }
911
912 var elements = new InternalArray(n);
913 var i = 0;
Sven Panne 2013/07/29 13:27:53 Style nit: Use a for loop for readability.
914 while (i < n) {
915 elements[i++] = s;
916 }
917
918 return %StringBuilderConcat(elements, n, "");
919 }
920
921
885 // Helper function for very basic XSS protection. 922 // Helper function for very basic XSS protection.
886 function HtmlEscape(str) { 923 function HtmlEscape(str) {
887 return TO_STRING_INLINE(str).replace(/</g, "&lt;") 924 return TO_STRING_INLINE(str).replace(/</g, "&lt;")
888 .replace(/>/g, "&gt;") 925 .replace(/>/g, "&gt;")
889 .replace(/"/g, "&quot;") 926 .replace(/"/g, "&quot;")
890 .replace(/'/g, "&#039;"); 927 .replace(/'/g, "&#039;");
891 } 928 }
892 929
893 930
894 // Compatibility support for KJS. 931 // Compatibility support for KJS.
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1003 "fontcolor", StringFontcolor, 1040 "fontcolor", StringFontcolor,
1004 "fontsize", StringFontsize, 1041 "fontsize", StringFontsize,
1005 "big", StringBig, 1042 "big", StringBig,
1006 "blink", StringBlink, 1043 "blink", StringBlink,
1007 "bold", StringBold, 1044 "bold", StringBold,
1008 "fixed", StringFixed, 1045 "fixed", StringFixed,
1009 "italics", StringItalics, 1046 "italics", StringItalics,
1010 "small", StringSmall, 1047 "small", StringSmall,
1011 "strike", StringStrike, 1048 "strike", StringStrike,
1012 "sub", StringSub, 1049 "sub", StringSub,
1013 "sup", StringSup 1050 "sup", StringSup,
1051 "repeat", StringRepeat
1014 )); 1052 ));
1015 } 1053 }
1016 1054
1017 SetUpString(); 1055 SetUpString();
OLDNEW
« no previous file with comments | « src/messages.js ('k') | test/mjsunit/string-repeat.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698