aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2018-04-20 22:29:36 +1000
committerdaurnimator <quae@daurnimator.com>2018-04-20 22:35:37 +1000
commit3714f514793a190ac64b6f3c7d48e377749c7acb (patch)
tree0a59591eb21a8888de6e51c81ca74eb4fd89c6c4
parenta2e3d512a6a154853065c7dc51f749374aca6d0f (diff)
downloadfengari-3714f514793a190ac64b6f3c7d48e377749c7acb.tar.gz
fengari-3714f514793a190ac64b6f3c7d48e377749c7acb.tar.bz2
fengari-3714f514793a190ac64b6f3c7d48e377749c7acb.zip
src/l{str,os}lib.js: Use luastring_eq where sensible
-rw-r--r--src/loslib.js11
-rw-r--r--src/lstrlib.js31
2 files changed, 8 insertions, 34 deletions
diff --git a/src/loslib.js b/src/loslib.js
index 6419524..6e0a032 100644
--- a/src/loslib.js
+++ b/src/loslib.js
@@ -42,6 +42,7 @@ const {
luaL_pushresult
} = require('./lauxlib.js');
const {
+ luastring_eq,
luastring_indexOf,
to_jsstring,
to_luastring
@@ -101,14 +102,6 @@ const getfield = function(L, key, d, delta) {
return res;
};
-const array_cmp = function(a, ai, b, bi, len) {
- for (let i=0; i<len; i++) {
- if (a[ai+i] !== b[bi+i])
- return false;
- }
- return true;
-};
-
const checkoption = function(L, conv, i, buff) {
let option = LUA_STRFTIMEOPTIONS;
let o = 0;
@@ -116,7 +109,7 @@ const checkoption = function(L, conv, i, buff) {
for (; o < option.length && oplen <= (conv.length - i); o += oplen) {
if (option[o] === '|'.charCodeAt(0)) /* next block? */
oplen++; /* will check options with next length (+1) */
- else if (array_cmp(conv, i, option, o, oplen)) { /* match? */
+ else if (luastring_eq(conv.subarray(i, i+oplen), option.subarray(o, o+oplen))) { /* match? */
buff.set(conv.subarray(i, i+oplen)); /* copy valid option to buffer */
return i + oplen; /* return next item */
}
diff --git a/src/lstrlib.js b/src/lstrlib.js
index 50dbcf2..9da67c5 100644
--- a/src/lstrlib.js
+++ b/src/lstrlib.js
@@ -75,6 +75,7 @@ const {
} = require('./lauxlib.js');
const lualib = require('./lualib.js');
const {
+ luastring_eq,
luastring_indexOf,
to_jsstring,
to_luastring
@@ -1061,21 +1062,7 @@ const end_capture = function(ms, s, p) {
/* Compare the elements of arrays 'a' and 'b' to see if they contain the same elements */
const array_cmp = function(a, ai, b, bi, len) {
- if (len === 0)
- return true;
- let aj = ai+len;
- loop: for (;;) {
- ai = luastring_indexOf(a, b[bi], ai);
- if (ai === -1 || ai >= aj)
- return false;
- for (let j = 1; j < len; j++) {
- if (a[ai+j] !== b[bi+j]) {
- ai++;
- continue loop;
- }
- }
- return true;
- }
+ return luastring_eq(a.subarray(ai, ai+len), b.subarray(bi, bi+len));
};
const match_capture = function(ms, s, l) {
@@ -1249,17 +1236,11 @@ const find_subarray = function(arr, subarr, from_index) {
if (sl === 0)
return i;
- loop: for (;;) {
- i = arr.indexOf(subarr[0], i);
- if (i === -1) break;
- for (let j = 1; j < sl; j++) {
- if (arr[i+j] !== subarr[j]) {
- i++;
- continue loop;
- }
- }
- return i;
+ for (; (i = arr.indexOf(subarr[0], i)) !== -1; i++) {
+ if (luastring_eq(arr.subarray(i, i+sl), subarr))
+ return i;
}
+
return -1;
};