diff options
Diffstat (limited to 'src/lstring.js')
-rw-r--r-- | src/lstring.js | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/src/lstring.js b/src/lstring.js index c8da56a..4769866 100644 --- a/src/lstring.js +++ b/src/lstring.js @@ -1,8 +1,12 @@ "use strict"; -const assert = require("assert"); - -const defs = require('./defs.js'); +const { + is_luastring, + luastring_eq, + luastring_from, + to_luastring +} = require('./defs.js'); +const { lua_assert } = require("./llimits.js"); class TString { @@ -22,20 +26,24 @@ class TString { } const luaS_eqlngstr = function(a, b) { - assert(a instanceof TString); - assert(b instanceof TString); - return a == b || defs.luastring_cmp(a.realstring, b.realstring); + lua_assert(a instanceof TString); + lua_assert(b instanceof TString); + return a == b || luastring_eq(a.realstring, b.realstring); }; /* converts strings (arrays) to a consistent map key make sure this doesn't conflict with any of the anti-collision strategies in ltable */ const luaS_hash = function(str) { - assert(defs.is_luastring(str)); - return '|'+str.join('|'); + lua_assert(is_luastring(str)); + let len = str.length; + let s = "|"; + for (let i=0; i<len; i++) + s += str[i].toString(16); + return s; }; const luaS_hashlongstr = function(ts) { - assert(ts instanceof TString); + lua_assert(ts instanceof TString); if(ts.hash === null) { ts.hash = luaS_hash(ts.getstr()); } @@ -44,18 +52,18 @@ const luaS_hashlongstr = function(ts) { /* variant that takes ownership of array */ const luaS_bless = function(L, str) { - assert(str instanceof Uint8Array); + lua_assert(str instanceof Uint8Array); return new TString(L, str); }; /* makes a copy */ const luaS_new = function(L, str) { - return luaS_bless(L, Uint8Array.from(str)); + return luaS_bless(L, luastring_from(str)); }; /* takes a js string */ const luaS_newliteral = function(L, str) { - return luaS_bless(L, defs.to_luastring(str)); + return luaS_bless(L, to_luastring(str)); }; module.exports.luaS_eqlngstr = luaS_eqlngstr; |