summaryrefslogtreecommitdiff
path: root/src/lstring.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-05-08 12:32:08 +1000
committerdaurnimator <quae@daurnimator.com>2017-05-08 15:31:29 +1000
commit2977fe002407b6f86efa4ba5216f567082f33e45 (patch)
treeda7e0127cd26534d01655e7b6a5c8732fb4920a0 /src/lstring.js
parent8c8585a0bba0e05b6382f2f4f063515b5988639c (diff)
downloadfengari-2977fe002407b6f86efa4ba5216f567082f33e45.tar.gz
fengari-2977fe002407b6f86efa4ba5216f567082f33e45.tar.bz2
fengari-2977fe002407b6f86efa4ba5216f567082f33e45.zip
Move string functions to centralised lstring.js
Diffstat (limited to 'src/lstring.js')
-rw-r--r--src/lstring.js33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/lstring.js b/src/lstring.js
new file mode 100644
index 0000000..34f988e
--- /dev/null
+++ b/src/lstring.js
@@ -0,0 +1,33 @@
+"use strict";
+
+const defs = require('./defs.js');
+
+const luaS_eqlngstr = function(a, b) {
+ return a == b || (a.length == b.length && a.join() == b.join());
+};
+
+/* converts strings (arrays) to a consistent map key */
+const luaS_hash = function(str) {
+ return str.map(e => `${e}|`).join('');
+};
+
+/* variant that takes ownership of array */
+const luaS_bless = function(L, str) {
+ return str;
+};
+
+/* makes a copy */
+const luaS_new = function(L, str) {
+ return luaS_bless(L, str.slice(0));
+};
+
+/* takes a js string */
+const luaS_newliteral = function(L, str) {
+ return luaS_bless(L, defs.to_luastring(str));
+};
+
+module.exports.luaS_eqlngstr = luaS_eqlngstr;
+module.exports.luaS_hash = luaS_hash;
+module.exports.luaS_bless = luaS_bless;
+module.exports.luaS_new = luaS_new;
+module.exports.luaS_newliteral = luaS_newliteral;