Issue 150: Fix regexp.exec bugs.

This commit is contained in:
Tor Andersson
2021-07-08 18:13:35 +02:00
parent c3ce563aa9
commit d4a599edea

View File

@@ -53,25 +53,27 @@ void js_newregexp(js_State *J, const char *pattern, int flags)
void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text) void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text)
{ {
const char *haystack;
int result; int result;
int i; int i;
int opts; int opts;
Resub m; Resub m;
haystack = text;
opts = 0; opts = 0;
if (re->flags & JS_REGEXP_G) { if (re->flags & JS_REGEXP_G) {
if (re->last > strlen(text)) { if (re->last > strlen(haystack)) {
re->last = 0; re->last = 0;
js_pushnull(J); js_pushnull(J);
return; return;
} }
if (re->last > 0) { if (re->last > 0) {
text += re->last; haystack = text + re->last;
opts |= REG_NOTBOL; opts |= REG_NOTBOL;
} }
} }
result = js_regexec(re->prog, text, &m, opts); result = js_regexec(re->prog, haystack, &m, opts);
if (result < 0) if (result < 0)
js_error(J, "regexec failed"); js_error(J, "regexec failed");
if (result == 0) { if (result == 0) {
@@ -85,7 +87,7 @@ void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text)
js_setindex(J, -2, i); js_setindex(J, -2, i);
} }
if (re->flags & JS_REGEXP_G) if (re->flags & JS_REGEXP_G)
re->last = re->last + (m.sub[0].ep - text); re->last = m.sub[0].ep - text;
return; return;
} }