commit bb8d6330c84e9863c286eab715ac19773cf7076d
parent 7790fb401e46238565b651c05d019988bd549ae3
Author: Jacob R. Edwards <n/a>
Date: Sun, 11 Dec 2022 17:58:48 -0600
Add regex negation flag
By appending an exclaimation point ('!') to a regular expression
(/regex/!) it's result is inverted. This is a very useful ability
(even if regular expressions themselves should have the functionality);
well worth the addition.
Diffstat:
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/aps/find.c b/aps/find.c
@@ -86,10 +86,11 @@ prepsearch(struct search *state, struct item *start, char *pattern)
c = *pattern;
pattern += 1;
p = strrchr(pattern, c);
- if (!p || p[1])
+ if (!p || (p[1] && (p[1] != '!' || p[2])))
return "Unterminated regular expression";
if (p - pattern >= sizeof(reg))
return "Regular expression too long";
+ state->inv = p[1] == '!';
memcpy(reg, pattern, p - pattern);
reg[p - pattern] = 0;
if (!reg[0])
@@ -122,6 +123,7 @@ prepsearch(struct search *state, struct item *start, char *pattern)
*/
return "Ranges spanning from negative to positive indexes not yet implemented";
}
+ state->inv = 0;
state->inc = nextitem;
state->reg = 0;
}
@@ -163,9 +165,12 @@ findnext(struct search *state)
} else if (state->reg) { /* One could do something like "match = state->match(state->cur);" instead. */
switch (regexec(&state->u.reg, state->cur->path, 0, NULL, 0)) {
case 0:
- match = state->cur;
+ if (!state->inv)
+ match = state->cur;
break;
case REG_NOMATCH:
+ if (state->inv)
+ match = state->cur;
break;
default:
bug("Regex failed to execute");
diff --git a/aps/find.h b/aps/find.h
@@ -2,6 +2,7 @@ struct search {
int fin;
int new;
int reg;
+ int inv;
struct item *(*inc)(struct item *);
struct item *cur, *end;
union {