shithub: libmujs

Download patch

ref: 7562052e872fe33f55834e1b72c8e4969fb83b1c
parent: fee803ad49d86d1e41e369bbe365fa5c6d3e3495
author: Tor Andersson <tor@ccxvii.net>
date: Wed Feb 26 10:30:46 EST 2014

Handle two-digit backreferences and captures.

--- a/jsregexp.c
+++ b/jsregexp.c
@@ -31,7 +31,7 @@
 
 void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text)
 {
-	Resub m[10];
+	Resub m[REG_MAXSUB];
 	unsigned int i;
 	int opts;
 
@@ -69,7 +69,7 @@
 {
 	js_Regexp *re;
 	const char *text;
-	Resub m[10];
+	Resub m[REG_MAXSUB];
 	int opts;
 
 	re = js_toregexp(J, 0);
--- a/jsstring.c
+++ b/jsstring.c
@@ -307,7 +307,7 @@
 static void Sp_match(js_State *J, unsigned int argc)
 {
 	js_Regexp *re;
-	Resub m[10];
+	Resub m[REG_MAXSUB];
 	const char *text;
 	unsigned int len;
 	const char *a, *b, *c, *e;
@@ -353,7 +353,7 @@
 static void Sp_search(js_State *J, unsigned int argc)
 {
 	js_Regexp *re;
-	Resub m[10];
+	Resub m[REG_MAXSUB];
 	const char *text;
 
 	text = js_tostring(J, 0);
@@ -376,7 +376,7 @@
 static void Sp_replace_regexp(js_State *J, unsigned int argc)
 {
 	js_Regexp *re;
-	Resub m[10];
+	Resub m[REG_MAXSUB];
 	const char *source, *s, *r;
 	js_Buffer *sb = NULL;
 	int n, x;
@@ -422,11 +422,19 @@
 				case '0': case '1': case '2': case '3': case '4':
 				case '5': case '6': case '7': case '8': case '9':
 					x = *r - '0';
-					if (m[x].sp) {
+					if (r[1] >= '0' && r[1] <= '9')
+						x = x * 10 + *(++r) - '0';
+					// TODO: use prog->nsub somehow
+					if (x > 0 && x < REG_MAXSUB && m[x].sp) {
 						sb_putm(&sb, m[x].sp, m[x].ep);
 					} else {
 						sb_putc(&sb, '$');
-						sb_putc(&sb, '0'+x);
+						if (x > 10) {
+							sb_putc(&sb, '0' + x / 10);
+							sb_putc(&sb, '0' + x % 10);
+						} else {
+							sb_putc(&sb, '0' + x);
+						}
 					}
 					break;
 				default:
@@ -536,7 +544,7 @@
 static void Sp_split_regexp(js_State *J, unsigned int argc)
 {
 	js_Regexp *re;
-	Resub m[10];
+	Resub m[REG_MAXSUB];
 	const char *text;
 	unsigned int limit, len, k;
 	const char *p, *a, *b, *c, *e;
--- a/regex.c
+++ b/regex.c
@@ -15,7 +15,7 @@
 
 #define REPINF 255
 #define MAXTHREAD 1000
-#define MAXSUB 10
+#define MAXSUB REG_MAXSUB
 
 typedef struct Reclass Reclass;
 typedef struct Renode Renode;
@@ -319,9 +319,12 @@
 		case 'D': newcclass(g); addranges_d(g); return L_NCCLASS;
 		case 'S': newcclass(g); addranges_s(g); return L_NCCLASS;
 		case 'W': newcclass(g); addranges_w(g); return L_NCCLASS;
+		case '0': g->yychar = 0; return L_CHAR;
 		}
 		if (g->yychar >= '0' && g->yychar <= '9') {
 			g->yychar -= '0';
+			if (*g->source >= '0' && *g->source <= '9')
+				g->yychar = g->yychar * 10 + *g->source++ - '0';
 			return L_REF;
 		}
 		return L_CHAR;
--- a/regex.h
+++ b/regex.h
@@ -23,6 +23,9 @@
 
 	/* regexec flags */
 	REG_NOTBOL = 4,
+
+	/* limits */
+	REG_MAXSUB = 16
 };
 
 #endif