diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-03-24 14:28:18 +0100 |
---|---|---|
committer | Benoit Giannangeli <giann008@gmail.com> | 2017-03-24 14:50:27 +0100 |
commit | 379b86fdea9f01c4bbff7fe66dec5a659ae82b27 (patch) | |
tree | e8283b52597b47b493ede5a3df661cee25dbbc0c /src/lobject.js | |
parent | f893be002aebd215b499b0c72803dd1346f4eb05 (diff) | |
download | fengari-379b86fdea9f01c4bbff7fe66dec5a659ae82b27.tar.gz fengari-379b86fdea9f01c4bbff7fe66dec5a659ae82b27.tar.bz2 fengari-379b86fdea9f01c4bbff7fe66dec5a659ae82b27.zip |
string.format operate on byte array instead of js strings
Diffstat (limited to 'src/lobject.js')
-rw-r--r-- | src/lobject.js | 75 |
1 files changed, 40 insertions, 35 deletions
diff --git a/src/lobject.js b/src/lobject.js index bdbccb9..32a7573 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -122,48 +122,52 @@ class TValue { } jsstring(from, to) { - let u0, u1, u2, u3, u4, u5; - let idx = 0; - let value = this.value.slice(from ? from : 0, to); - - var str = ''; - while (1) { - // 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 - u0 = value[idx++]; - if (u0 === 0) { str += "\0"; continue; } // Lua string embed '\0' - if (!u0) return str; - if (!(u0 & 0x80)) { str += String.fromCharCode(u0); continue; } - u1 = value[idx++] & 63; - if ((u0 & 0xE0) == 0xC0) { str += String.fromCharCode(((u0 & 31) << 6) | u1); continue; } - u2 = value[idx++] & 63; - if ((u0 & 0xF0) == 0xE0) { - u0 = ((u0 & 15) << 12) | (u1 << 6) | u2; + return jsstring(this.value, from, to); + } + +} + +const jsstring = function(value, from, to) { + let u0, u1, u2, u3, u4, u5; + let idx = 0; + value = value.slice(from ? from : 0, to); + + var str = ''; + while (1) { + // 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 + u0 = value[idx++]; + if (u0 === 0) { str += "\0"; continue; } // Lua string embed '\0' + if (!u0) return str; + if (!(u0 & 0x80)) { str += String.fromCharCode(u0); continue; } + u1 = value[idx++] & 63; + if ((u0 & 0xE0) == 0xC0) { str += String.fromCharCode(((u0 & 31) << 6) | u1); continue; } + u2 = value[idx++] & 63; + if ((u0 & 0xF0) == 0xE0) { + u0 = ((u0 & 15) << 12) | (u1 << 6) | u2; + } else { + u3 = value[idx++] & 63; + if ((u0 & 0xF8) == 0xF0) { + u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | u3; } else { - u3 = value[idx++] & 63; - if ((u0 & 0xF8) == 0xF0) { - u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | u3; + u4 = value[idx++] & 63; + if ((u0 & 0xFC) == 0xF8) { + u0 = ((u0 & 3) << 24) | (u1 << 18) | (u2 << 12) | (u3 << 6) | u4; } else { - u4 = value[idx++] & 63; - if ((u0 & 0xFC) == 0xF8) { - u0 = ((u0 & 3) << 24) | (u1 << 18) | (u2 << 12) | (u3 << 6) | u4; - } else { - u5 = value[idx++] & 63; - u0 = ((u0 & 1) << 30) | (u1 << 24) | (u2 << 18) | (u3 << 12) | (u4 << 6) | u5; - } + u5 = value[idx++] & 63; + u0 = ((u0 & 1) << 30) | (u1 << 24) | (u2 << 18) | (u3 << 12) | (u4 << 6) | u5; } } - if (u0 < 0x10000) { - str += String.fromCharCode(u0); - } else { - var ch = u0 - 0x10000; - str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF)); - } } - - return str; + if (u0 < 0x10000) { + str += String.fromCharCode(u0); + } else { + var ch = u0 - 0x10000; + str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF)); + } } -} + return str; +}; const nil = new TValue(CT.LUA_TNIL, null); @@ -466,6 +470,7 @@ module.exports.TValue = TValue; module.exports.Table = Table; module.exports.UTF8BUFFSZ = UTF8BUFFSZ; module.exports.intarith = intarith; +module.exports.jsstring = jsstring; module.exports.luaO_chunkid = luaO_chunkid; module.exports.luaO_hexavalue = luaO_hexavalue; module.exports.luaO_int2fb = luaO_int2fb; |