summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lstrlib.js55
1 files changed, 30 insertions, 25 deletions
diff --git a/src/lstrlib.js b/src/lstrlib.js
index c4dd1ae..362942d 100644
--- a/src/lstrlib.js
+++ b/src/lstrlib.js
@@ -965,12 +965,21 @@ 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;
- for (; ai < aj; ai++, bi++) {
- if (a[ai] !== b[bi])
+ loop: for (;;) {
+ ai = a.indexOf(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 true;
};
const match_capture = function(ms, s, l) {
@@ -1008,7 +1017,7 @@ const match = function(ms, s, p) {
gotodefault = true; /* no; go to default */
break;
}
- s = ms.src.slice(s).length === 0 ? s : null; /* check end of string */
+ s = (ms.src.length - s) === 0 ? s : null; /* check end of string */
break;
}
case L_ESC: { /* escaped sequences not in the format class[*+?-]? */
@@ -1113,22 +1122,11 @@ const push_captures = function(ms, s, e) {
};
const nospecials = function(p, l) {
- let upto = 0;
- do {
- let special = false;
- let supto = p.slice(upto);
- for (let i = 0; i < SPECIALS.length; i++) {
- if (supto.indexOf(SPECIALS[i]) > -1) {
- special = true;
- break;
- }
- }
-
- if (special)
- return false; /* pattern has a special character */
- upto = upto + 1; /* may have more after \0 */
- } while (upto <= l);
- return true; /* no special chars found */
+ for (let i=0; i<l; i++) {
+ if (SPECIALS.indexOf(p[i]) !== -1)
+ return false;
+ }
+ return true;
};
const prepstate = function(ms, L, s, ls, p, lp) {
@@ -1148,13 +1146,20 @@ const reprepstate = function(ms) {
const find_subarray = function(arr, subarr, from_index) {
var i = from_index >>> 0,
- sl = subarr.length,
- l = arr.length + 1 - sl;
+ sl = subarr.length;
- loop: for (; i < l; i++) {
- for (let j = 0; j < sl; j++)
- if (arr[i+j] !== subarr[j])
+ 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;
}
return -1;