shithub: libmujs

Download patch

ref: 450a0ef35e3872c247801fd4548dffd8e3d06c1d
parent: b749e23cb4ff42e06d9e1e75196745be4ca40a59
author: Tor Andersson <tor@ccxvii.net>
date: Tue Feb 4 09:45:59 EST 2014

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

--- a/jsregexp.c
+++ b/jsregexp.c
@@ -19,6 +19,8 @@
 	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) {
@@ -165,6 +167,8 @@
 
 	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;
--- a/jsstring.c
+++ b/jsstring.c
@@ -429,10 +429,17 @@
 
 	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);