Compare commits

...

2 commits

Author SHA1 Message Date
0448ebed7a restructure main loop 2025-02-06 22:32:22 +01:00
6c163d8eb0 fix next_char 2025-02-06 22:32:09 +01:00

38
regex.c
View file

@ -73,14 +73,12 @@ char *next_char(char *needle)
int db; int db;
if (*needle && *needle != '[') if (*needle && *needle != '[')
return needle + 1; return needle + 1;
for (db = (*needle == '[' ? 2 : 0); for (db = 1;
*needle && (*needle != ']' || db); *needle && (*needle != ']' || db);
needle++) needle++)
{ if (db)
if (db > 1)
db--; db--;
} return needle + 1;
return needle;
} }
// Find first character behind the closing parenthesis of the current group, or end of string // Find first character behind the closing parenthesis of the current group, or end of string
@ -153,7 +151,18 @@ bool regex_match(char *haystack, char *needle)
if (!bv[st]) if (!bv[st])
continue; continue;
fprintf(stderr, "prepr %lu: \"%s\"\n", st, needle + st); fprintf(stderr, "prepr %lu: \"%s\"\n", st, needle + st);
if (needle[st] == '|') if (needle[st] == '(')
{
// at start of group, look at variants and quantifier
for (var = needle + st + 1; *var && *var != ')'; var = next_var(var))
bv[var - needle] = true;
if (*var == ')')
var++;
q = quant(*var);
if (strchr("*?", q))
bv[var + 1 - needle] = true;
}
else if (needle[st] == '|')
{ {
// at end of variant, look at end of group // at end of variant, look at end of group
bv[st] = false; bv[st] = false;
@ -173,7 +182,7 @@ bool regex_match(char *haystack, char *needle)
fprintf(stderr, "quant %c\n", q); fprintf(stderr, "quant %c\n", q);
fprintf(stderr, "suffix \"%s\"\n", needle + (q ? st + 2 : st + 1)); fprintf(stderr, "suffix \"%s\"\n", needle + (q ? st + 2 : st + 1));
nneedl = start_of_group(needle, needle + st); nneedl = start_of_group(needle, needle + st);
if ((q == '+' || q == '*') && !bv[nneedl - needle]) if (strchr("+*", q) && !bv[nneedl - needle])
{ {
// spicy: if the quantifier allows multiple occurences, we have to activate a previous state // spicy: if the quantifier allows multiple occurences, we have to activate a previous state
fprintf(stderr, "start of group: \"%s\"\n", nneedl); fprintf(stderr, "start of group: \"%s\"\n", nneedl);
@ -183,23 +192,12 @@ bool regex_match(char *haystack, char *needle)
else else
bv[nneedl - needle] = false; bv[nneedl - needle] = false;
} }
else if (needle[st] == '(')
{
// at start of group, look at variants and quantifier
for (var = needle + st + 1; *var && *var != ')'; var = next_var(var))
bv[var - needle] = true;
if (*var == ')')
var++;
q = quant(*var);
if (q == '*' || q == '?')
bv[var + 1 - needle] = true;
}
else else
{ {
// check if current needle is optional // check if current needle is optional
nneedl = next_char(needle + st); nneedl = next_char(needle + st);
q = *nneedl; q = *nneedl;
if (q == '*' || q == '?') if (strchr("?*", q))
bv[nneedl + 1 - needle] = true; bv[nneedl + 1 - needle] = true;
} }
} }
@ -220,7 +218,7 @@ bool regex_match(char *haystack, char *needle)
na = false; na = false;
if (q) if (q)
nneedl++; nneedl++;
if (q == '*' || q == '+') if (strchr("*+", q))
bv[st-1] = true; bv[st-1] = true;
bv[nneedl - needle] = true; bv[nneedl - needle] = true;
} }