restructure main loop

This commit is contained in:
Fritz Grimpen 2025-02-06 22:32:22 +01:00
parent 6c163d8eb0
commit 0448ebed7a

30
regex.c
View file

@ -151,7 +151,18 @@ bool regex_match(char *haystack, char *needle)
if (!bv[st])
continue;
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
bv[st] = false;
@ -171,7 +182,7 @@ bool regex_match(char *haystack, char *needle)
fprintf(stderr, "quant %c\n", q);
fprintf(stderr, "suffix \"%s\"\n", needle + (q ? st + 2 : st + 1));
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
fprintf(stderr, "start of group: \"%s\"\n", nneedl);
@ -181,23 +192,12 @@ bool regex_match(char *haystack, char *needle)
else
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
{
// check if current needle is optional
nneedl = next_char(needle + st);
q = *nneedl;
if (q == '*' || q == '?')
if (strchr("?*", q))
bv[nneedl + 1 - needle] = true;
}
}
@ -218,7 +218,7 @@ bool regex_match(char *haystack, char *needle)
na = false;
if (q)
nneedl++;
if (q == '*' || q == '+')
if (strchr("*+", q))
bv[st-1] = true;
bv[nneedl - needle] = true;
}