ref: 697eee89ad751841e3c60e116629989cb3dbea98
parent: fd0ff38ad0ded3512173425407e1cdceb1fcabf2
author: Tor Andersson <tor@ccxvii.net>
date: Tue Feb 25 11:19:29 EST 2014
Use macro for magic number indicating an infinite number of repeats.
--- a/regex.c
+++ b/regex.c
@@ -13,6 +13,8 @@
#define nelem(a) (sizeof (a) / sizeof (a)[0])
+#define REPINF 255
+
typedef struct Reclass Reclass;
typedef struct Renode Renode;
typedef struct Reinst Reinst;
@@ -134,13 +136,13 @@
g->yymin = g->yymin * 10 + dec(g, g->yychar);
nextrune(g);
}
- if (g->yymin >= USHRT_MAX)
+ if (g->yymin >= REPINF)
die(g, "numeric overflow");
if (g->yychar == ',') {
nextrune(g);
if (g->yychar == '}') {
- g->yymax = USHRT_MAX;
+ g->yymax = REPINF;
} else {
g->yymax = dec(g, g->yychar);
nextrune(g);
@@ -148,7 +150,7 @@
g->yymax = g->yymax * 10 + dec(g, g->yychar);
nextrune(g);
}
- if (g->yymax >= USHRT_MAX)
+ if (g->yymax >= REPINF)
die(g, "numeric overflow");
}
} else {
@@ -366,11 +368,10 @@
};
struct Renode {
- int type;
- Reclass *cc;
+ unsigned char type;
+ unsigned char ng, m, n;
Rune c;
- unsigned char ng;
- unsigned short m, n;
+ Reclass *cc;
Renode *x;
Renode *y;
};
@@ -498,8 +499,8 @@
die(g, "invalid quantifier");
return newrep(g, atom, accept(g, '?'), min, max);
}
- if (accept(g, '*')) return newrep(g, atom, accept(g, '?'), 0, USHRT_MAX);
- if (accept(g, '+')) return newrep(g, atom, accept(g, '?'), 1, USHRT_MAX);
+ if (accept(g, '*')) return newrep(g, atom, accept(g, '?'), 0, REPINF);
+ if (accept(g, '+')) return newrep(g, atom, accept(g, '?'), 1, REPINF);
if (accept(g, '?')) return newrep(g, atom, accept(g, '?'), 0, 1);
return atom;
}
@@ -543,10 +544,10 @@
};
struct Reinst {
- int opcode;
- Reclass *cc;
- Rune c;
+ unsigned char opcode;
unsigned char n;
+ Rune c;
+ Reclass *cc;
Reinst *x;
Reinst *y;
const char *p;
@@ -564,7 +565,7 @@
min = node->m;
max = node->n;
if (min == max) return count(node->x) * min;
- if (max < USHRT_MAX) return count(node->x) * max + (max - min);
+ if (max < REPINF) return count(node->x) * max + (max - min);
return count(node->x) * (min + 1) + 2;
case P_PAR: return count(node->x) + 2;
case P_PLA: return count(node->x) + 2;
@@ -587,7 +588,7 @@
static void compile(Reprog *prog, Renode *node)
{
Reinst *inst, *split, *jump;
- int i;
+ unsigned int i;
if (!node)
return;
@@ -615,7 +616,7 @@
}
if (node->m == node->n)
break;
- if (node->n < USHRT_MAX) {
+ if (node->n < REPINF) {
for (i = node->m; i < node->n; ++i) {
split = emit(prog, I_SPLIT);
compile(prog, node->x);