summaryrefslogtreecommitdiff
path: root/src/lobject.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-03-28 13:18:13 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-03-28 13:53:19 +0200
commit36f3247d47c1ad854fa89aabf17f6d954a6a6657 (patch)
tree20d48d5d73aa81f86998c33d439b1d45bf1ea510 /src/lobject.js
parentca46ad67f398f219cf838feb10127cd0cecc72d8 (diff)
downloadfengari-36f3247d47c1ad854fa89aabf17f6d954a6a6657.tar.gz
fengari-36f3247d47c1ad854fa89aabf17f6d954a6a6657.tar.bz2
fengari-36f3247d47c1ad854fa89aabf17f6d954a6a6657.zip
luaO_utf8esc
Diffstat (limited to 'src/lobject.js')
-rw-r--r--src/lobject.js22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/lobject.js b/src/lobject.js
index fca259d..966bdd8 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -499,6 +499,27 @@ const luaO_str2num = function(s) {
}
};
+const luaO_utf8esc = function(x) {
+ let buff = [];
+ let n = 1; /* number of bytes put in buffer (backwards) */
+ assert(x <= 0x10ffff);
+ if (x < 0x80) /* ascii? */
+ buff[UTF8BUFFSZ - 1] = x;
+ else { /* need continuation bytes */
+ let mfb = 0x3f; /* maximum that fits in first byte */
+ do { /* add continuation bytes */
+ buff[UTF8BUFFSZ - (n++)] = 0x80 | (x & 0x3f);
+ x >>= 6; /* remove added bits */
+ mfb >>= 1; /* now there is one less bit available in first byte */
+ } while (x > mfb); /* still needs continuation byte? */
+ buff[UTF8BUFFSZ - n] = (~mfb << 1) | x; /* add first byte */
+ }
+ return {
+ buff: buff,
+ n: n
+ };
+};
+
/*
** converts an integer to a "floating point byte", represented as
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
@@ -564,4 +585,5 @@ module.exports.luaO_hexavalue = luaO_hexavalue;
module.exports.luaO_int2fb = luaO_int2fb;
module.exports.luaO_str2num = luaO_str2num;
module.exports.luaO_utf8desc = luaO_utf8desc;
+module.exports.luaO_utf8esc = luaO_utf8esc;
module.exports.numarith = numarith;