Handle String.replace() when the search matches the empty string.

This commit is contained in:
Tor Andersson
2014-02-04 15:05:04 +01:00
parent b749e23cb4
commit 450a0ef35e
2 changed files with 11 additions and 0 deletions
+4
View File
@@ -19,6 +19,8 @@ int js_RegExp_prototype_exec(js_State *J, int idx, const char *text)
if (flags & JS_REGEXP_I) opts |= REG_ICASE;
if (flags & JS_REGEXP_M) opts |= REG_NEWLINE;
// TODO: global and lastIndex
if (!regexec(prog, text, nelem(m), m, opts)) {
js_newarray(J);
for (i = 0; i < nelem(m) && m[i].rm_so >= 0; ++i) {
@@ -166,6 +168,8 @@ static int Rp_test(js_State *J, int argc)
prog = js_toregexp(J, 0, &flags);
text = js_tostring(J, 1);
// TODO: global and lastIndex
js_pushboolean(J, !regexec(prog, text, 0, NULL, 0));
return 1;
}
+7
View File
@@ -429,10 +429,17 @@ loop:
if (flags & JS_REGEXP_G) {
source = source + m[0].rm_eo;
if (n == 0) {
if (*source)
sb = sb_putc(sb, *source++);
else
goto end;
}
if (!regexec(prog, source, nelem(m), m, REG_NOTBOL))
goto loop;
}
end:
sb = sb_puts(sb, s + n);
sb = sb_putc(sb, 0);