diff options
author | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-03-06 15:58:11 +0100 |
---|---|---|
committer | Benoit Giannangeli <giann@users.noreply.github.com> | 2017-03-13 11:03:24 +0100 |
commit | e96d75a87d879f7f455e4b9c6457bf6580743fa5 (patch) | |
tree | a468f1c71ef765b0fc6acbfb32a49dc8ad9a0d65 /src/lua.js | |
parent | 342a7db19607446720770cd2cfc5d275ce3f2ca9 (diff) | |
download | fengari-e96d75a87d879f7f455e4b9c6457bf6580743fa5.tar.gz fengari-e96d75a87d879f7f455e4b9c6457bf6580743fa5.tar.bz2 fengari-e96d75a87d879f7f455e4b9c6457bf6580743fa5.zip |
String are represented by Array of 8-bit numbers
Diffstat (limited to 'src/lua.js')
-rw-r--r-- | src/lua.js | 58 |
1 files changed, 57 insertions, 1 deletions
@@ -132,6 +132,61 @@ class lua_Debug { } +const to_luastring = function(str, maxBytesToWrite) { + let outU8Array = new Array(maxBytesToWrite); + + if (!(maxBytesToWrite > 0)) // Parameter maxBytesToWrite is not optional. Negative values, 0, null, undefined and false each don't write out any bytes. + return 0; + + let outIdx = 0; + let startIdx = 0; + let endIdx = maxBytesToWrite - 1; // -1 for string null terminator. + for (let i = 0; i < str.length; ++i) { + // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code unit, not a Unicode code point of the character! So decode UTF16->UTF32->UTF8. + // 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.charCodeAt(i); // possibly a lead surrogate + if (u >= 0xD800 && u <= 0xDFFF) u = 0x10000 + ((u & 0x3FF) << 10) | (str.charCodeAt(++i) & 0x3FF); + if (u <= 0x7F) { + if (outIdx >= endIdx) break; + outU8Array[outIdx++] = u; + } else if (u <= 0x7FF) { + if (outIdx + 1 >= endIdx) break; + outU8Array[outIdx++] = 0xC0 | (u >> 6); + outU8Array[outIdx++] = 0x80 | (u & 63); + } else if (u <= 0xFFFF) { + if (outIdx + 2 >= endIdx) break; + outU8Array[outIdx++] = 0xE0 | (u >> 12); + outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); + outU8Array[outIdx++] = 0x80 | (u & 63); + } else if (u <= 0x1FFFFF) { + if (outIdx + 3 >= endIdx) break; + outU8Array[outIdx++] = 0xF0 | (u >> 18); + outU8Array[outIdx++] = 0x80 | ((u >> 12) & 63); + outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); + outU8Array[outIdx++] = 0x80 | (u & 63); + } else if (u <= 0x3FFFFFF) { + if (outIdx + 4 >= endIdx) break; + outU8Array[outIdx++] = 0xF8 | (u >> 24); + outU8Array[outIdx++] = 0x80 | ((u >> 18) & 63); + outU8Array[outIdx++] = 0x80 | ((u >> 12) & 63); + outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); + outU8Array[outIdx++] = 0x80 | (u & 63); + } else { + if (outIdx + 5 >= endIdx) break; + outU8Array[outIdx++] = 0xFC | (u >> 30); + outU8Array[outIdx++] = 0x80 | ((u >> 24) & 63); + outU8Array[outIdx++] = 0x80 | ((u >> 18) & 63); + outU8Array[outIdx++] = 0x80 | ((u >> 12) & 63); + outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); + outU8Array[outIdx++] = 0x80 | (u & 63); + } + } + // Null-terminate the pointer to the buffer. + outU8Array[outIdx] = 0; + return outU8Array; +}; + module.exports.CT = CT; module.exports.FENGARI_AUTHORS = FENGARI_AUTHORS; module.exports.FENGARI_COPYRIGHT = FENGARI_COPYRIGHT; @@ -181,4 +236,5 @@ module.exports.constant_types = constant_types; module.exports.lua_Debug = lua_Debug; module.exports.lua_upvalueindex = lua_upvalueindex; module.exports.print_version = print_version; -module.exports.thread_status = thread_status;
\ No newline at end of file +module.exports.thread_status = thread_status; +module.exports.to_luastring = to_luastring;
\ No newline at end of file |