From 450a0ef35e3872c247801fd4548dffd8e3d06c1d Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 4 Feb 2014 14:45:59 +0100 Subject: [PATCH] Handle String.replace() when the search matches the empty string. --- jsregexp.c | 4 ++++ jsstring.c | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/jsregexp.c b/jsregexp.c index 7205a23..eef2ac2 100644 --- a/jsregexp.c +++ b/jsregexp.c @@ -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; } diff --git a/jsstring.c b/jsstring.c index b3d4c1b..8420d13 100644 --- a/jsstring.c +++ b/jsstring.c @@ -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);