Index: src/string.js |
diff --git a/src/string.js b/src/string.js |
index 6115930b6c8ff69568f425b8b996a821bc93cb87..b698a2af5ba351e584b3dc7143be044ffe82ba23 100644 |
--- a/src/string.js |
+++ b/src/string.js |
@@ -801,6 +801,13 @@ function StringTrimRight() { |
var static_charcode_array = new InternalArray(4); |
+ |
+function TruncateString(string, new_length) { |
+ if (new_length == 0) return ""; |
+ return %TruncateString(string, new_length); |
+} |
+ |
+ |
// ECMA-262, section 15.5.3.2 |
function StringFromCharCode(code) { |
var n = %_ArgumentsLength(); |
@@ -809,17 +816,24 @@ function StringFromCharCode(code) { |
return %_StringCharFromCode(code & 0xffff); |
} |
- // NOTE: This is not super-efficient, but it is necessary because we |
- // want to avoid converting to numbers from within the virtual |
- // machine. Maybe we can find another way of doing this? |
- var codes = static_charcode_array; |
- for (var i = 0; i < n; i++) { |
+ var one_byte = %NewString(n, true); |
Toon Verwaest
2012/12/05 14:29:12
Can we have a javascript macro that indicates that
|
+ var i; |
+ for (i = 0; i < n; i++) { |
var code = %_Arguments(i); |
- if (!%_IsSmi(code)) code = ToNumber(code); |
- codes[i] = code; |
+ if (!%_IsSmi(code)) code = ToNumber(code) & 0xffff; |
+ if (code > 0x7f) break; |
+ %_OneByteSeqStringSetChar(one_byte, i, code); |
+ } |
+ if (i == n) return one_byte; |
+ one_byte = TruncateString(one_byte, i); |
+ |
+ var two_byte = %NewString(n - i, false); |
+ for (var j = 0; i < n; i++, j++) { |
+ var code = %_Arguments(i); |
+ if (!%_IsSmi(code)) code = ToNumber(code) & 0xffff; |
+ %_TwoByteSeqStringSetChar(two_byte, j, code); |
} |
- codes.length = n; |
- return %StringFromCharCodeArray(codes); |
+ return one_byte + two_byte; |
} |