From f2250406ab210d71ca8ae71db93facc34f3c9992 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 24 Mar 2017 09:34:56 +0100 Subject: TValue.jsstring can take start and end indexes --- src/lobject.js | 15 ++++++++------- src/lstrlib.js | 4 +++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/lobject.js b/src/lobject.js index c2950b6..d713183 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -118,31 +118,32 @@ class TValue { return this.ttisnil() || (this.ttisboolean() && this.value === false); } - jsstring() { + 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 = this.value[idx++]; + u0 = value[idx++]; if (!u0) return str; if (!(u0 & 0x80)) { str += String.fromCharCode(u0); continue; } - u1 = this.value[idx++] & 63; + u1 = value[idx++] & 63; if ((u0 & 0xE0) == 0xC0) { str += String.fromCharCode(((u0 & 31) << 6) | u1); continue; } - u2 = this.value[idx++] & 63; + u2 = value[idx++] & 63; if ((u0 & 0xF0) == 0xE0) { u0 = ((u0 & 15) << 12) | (u1 << 6) | u2; } else { - u3 = this.value[idx++] & 63; + u3 = value[idx++] & 63; if ((u0 & 0xF8) == 0xF0) { u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | u3; } else { - u4 = this.value[idx++] & 63; + u4 = value[idx++] & 63; if ((u0 & 0xFC) == 0xF8) { u0 = ((u0 & 3) << 24) | (u1 << 18) | (u2 << 12) | (u3 << 6) | u4; } else { - u5 = this.value[idx++] & 63; + u5 = value[idx++] & 63; u0 = ((u0 & 1) << 30) | (u1 << 24) | (u2 << 18) | (u3 << 12) | (u4 << 6) | u5; } } diff --git a/src/lstrlib.js b/src/lstrlib.js index c6c0dc3..1b62db1 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -33,13 +33,15 @@ const posrelat = function(pos, len) { const str_sub = function(L) { let s = lauxlib.luaL_checkstring(L, 1); + let ts = L.stack[lapi.index2addr_(L, 1)]; + s = ts.value; let l = s.length; let start = posrelat(lauxlib.luaL_checkinteger(L, 2), l); let end = posrelat(lauxlib.luaL_optinteger(L, 3, -1), l); if (start < 1) start = 1; if (end > l) end = l; if (start <= end) - lapi.lua_pushstring(L, s.slice(start - 1, (start - 1) + (end - start + 1))); + lapi.lua_pushstring(L, ts.jsstring(start - 1, (start - 1) + (end - start + 1))); else lapi.lua_pushliteral(L, ""); return 1; }; -- cgit v1.2.3-54-g00ecf