From 0b903fef1bfe08d0fe573f4bdb02a6c90fd39ab2 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 12 Nov 2017 14:47:52 +1100 Subject: src/defs.js: minor optimisation of to_luastring --- src/defs.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/defs.js') diff --git a/src/defs.js b/src/defs.js index 6fb7d3f..5ebaad9 100644 --- a/src/defs.js +++ b/src/defs.js @@ -187,13 +187,12 @@ const to_luastring = function(str, cache) { if (is_luastring(cached)) return cached; } - let outU8Array = []; + let outU8Array = Array(str.length); /* array is at *least* going to be length of string */ let outIdx = 0; for (let i = 0; i < str.length; ++i) { // See http://unicode.org/faq/utf_bom.html#utf16-3 // For UTF8 byte structure, see http://en.wikipedia.org/wiki/UTF-8#Description and https://www.ietf.org/rfc/rfc2279.txt and https://tools.ietf.org/html/rfc3629 let u = str.codePointAt(i); - if (u >= 0x10000) i++; // It was a surrogate pair and hence used up two bytes if (u <= 0x7F) { outU8Array[outIdx++] = u; } else if (u <= 0x7FF) { @@ -204,6 +203,7 @@ const to_luastring = function(str, cache) { outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); outU8Array[outIdx++] = 0x80 | (u & 63); } else { + i++; /* It was a surrogate pair and hence used up two javascript chars */ outU8Array[outIdx++] = 0xF0 | (u >> 18); outU8Array[outIdx++] = 0x80 | ((u >> 12) & 63); outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); -- cgit v1.2.3-54-g00ecf