From be24bbcb8c6172b16cd4590400aa3bf59b081f29 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 1 May 2022 09:43:18 -0700 Subject: Refactor: move regexec calls into function. --- pw.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'pw.c') diff --git a/pw.c b/pw.c index 9e2a32a..29b6ecf 100644 --- a/pw.c +++ b/pw.c @@ -236,6 +236,12 @@ static void usage(const char *name) exit(EXIT_FAILURE); } +static int grexec(grep *gr, const char *line) +{ + int match = regexec(&gr->rx, line, 0, NULL, 0) == 0; + return match != gr->inv; +} + static void clrline() { printf("\r\033[J"); @@ -757,12 +763,9 @@ int main(int argc, char **argv) line = addch(line, 0); if ((stat & stat_grep)) { int i; - for (i = 0; i < ngrep; i++) { - grep *gr = &grepstack[i]; - int match = regexec(&gr->rx, line, 0, NULL, 0) == 0; - if (match == gr->inv) + for (i = 0; i < ngrep; i++) + if (!grexec(&grepstack[i], line)) break; - } if (i < ngrep) { dsdrop(line); line = 0; @@ -779,12 +782,9 @@ int main(int argc, char **argv) int trig = 1; for (int i = 0; i < lim; i++) { grep *gr = triglist[i]; - if (gr) { - int match = regexec(&gr->rx, circbuf[i], 0, NULL, 0) == 0; - if (match == gr->inv) { - trig = 0; - break; - } + if (gr && !grexec(gr, circbuf[i])) { + trig = 0; + break; } } if (trig) @@ -793,12 +793,9 @@ int main(int argc, char **argv) int trig = 1; for (int j = nlines - 1, i = 0; j >= 0 && i < maxtrig; j--, i++) { grep *gr = triglist[i]; - if (gr) { - int match = regexec(&gr->rx, circbuf[j], 0, NULL, 0) == 0; - if (match == gr->inv) { - trig = 0; - break; - } + if (gr && !grexec(gr, circbuf[j])) { + trig = 0; + break; } } if (trig) -- cgit v1.2.3