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

Side by Side Diff: src/string.js

Issue 11348349: Improve array to string conversion. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years 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 | Annotate | Revision Log
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 783 matching lines...) Expand 10 before | Expand all | Expand 10 after
794 function StringTrimRight() { 794 function StringTrimRight() {
795 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 795 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
796 throw MakeTypeError("called_on_null_or_undefined", 796 throw MakeTypeError("called_on_null_or_undefined",
797 ["String.prototype.trimRight"]); 797 ["String.prototype.trimRight"]);
798 } 798 }
799 return %StringTrim(TO_STRING_INLINE(this), false, true); 799 return %StringTrim(TO_STRING_INLINE(this), false, true);
800 } 800 }
801 801
802 var static_charcode_array = new InternalArray(4); 802 var static_charcode_array = new InternalArray(4);
803 803
804
805 function TruncateString(string, new_length) {
806 if (new_length == 0) return "";
807 return %TruncateString(string, new_length);
808 }
809
810
804 // ECMA-262, section 15.5.3.2 811 // ECMA-262, section 15.5.3.2
805 function StringFromCharCode(code) { 812 function StringFromCharCode(code) {
806 var n = %_ArgumentsLength(); 813 var n = %_ArgumentsLength();
807 if (n == 1) { 814 if (n == 1) {
808 if (!%_IsSmi(code)) code = ToNumber(code); 815 if (!%_IsSmi(code)) code = ToNumber(code);
809 return %_StringCharFromCode(code & 0xffff); 816 return %_StringCharFromCode(code & 0xffff);
810 } 817 }
811 818
812 // NOTE: This is not super-efficient, but it is necessary because we 819 var one_byte = %NewString(n, true);
Toon Verwaest 2012/12/05 14:29:12 Can we have a javascript macro that indicates that
813 // want to avoid converting to numbers from within the virtual 820 var i;
814 // machine. Maybe we can find another way of doing this? 821 for (i = 0; i < n; i++) {
815 var codes = static_charcode_array;
816 for (var i = 0; i < n; i++) {
817 var code = %_Arguments(i); 822 var code = %_Arguments(i);
818 if (!%_IsSmi(code)) code = ToNumber(code); 823 if (!%_IsSmi(code)) code = ToNumber(code) & 0xffff;
819 codes[i] = code; 824 if (code > 0x7f) break;
825 %_OneByteSeqStringSetChar(one_byte, i, code);
820 } 826 }
821 codes.length = n; 827 if (i == n) return one_byte;
822 return %StringFromCharCodeArray(codes); 828 one_byte = TruncateString(one_byte, i);
829
830 var two_byte = %NewString(n - i, false);
831 for (var j = 0; i < n; i++, j++) {
832 var code = %_Arguments(i);
833 if (!%_IsSmi(code)) code = ToNumber(code) & 0xffff;
834 %_TwoByteSeqStringSetChar(two_byte, j, code);
835 }
836 return one_byte + two_byte;
823 } 837 }
824 838
825 839
826 // Helper function for very basic XSS protection. 840 // Helper function for very basic XSS protection.
827 function HtmlEscape(str) { 841 function HtmlEscape(str) {
828 return TO_STRING_INLINE(str).replace(/</g, "&lt;") 842 return TO_STRING_INLINE(str).replace(/</g, "&lt;")
829 .replace(/>/g, "&gt;") 843 .replace(/>/g, "&gt;")
830 .replace(/"/g, "&quot;") 844 .replace(/"/g, "&quot;")
831 .replace(/'/g, "&#039;"); 845 .replace(/'/g, "&#039;");
832 } 846 }
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
983 "fixed", StringFixed, 997 "fixed", StringFixed,
984 "italics", StringItalics, 998 "italics", StringItalics,
985 "small", StringSmall, 999 "small", StringSmall,
986 "strike", StringStrike, 1000 "strike", StringStrike,
987 "sub", StringSub, 1001 "sub", StringSub,
988 "sup", StringSup 1002 "sup", StringSup
989 )); 1003 ));
990 } 1004 }
991 1005
992 SetUpString(); 1006 SetUpString();
OLDNEW
« src/json-stringifier.h ('K') | « src/runtime.cc ('k') | src/uri.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698