Skip to content

Commit 97e7778

Browse files
mkiedrowiczgitster
authored andcommitted
grep: Put calls to fixmatch() and regmatch() into patmatch()
Both match_one_pattern() and look_ahead() use fixmatch() and regmatch() in the same way. They really want to match a pattern againt a string, but now they need to know if the pattern is fixed or regexp. This change cleans this up by introducing patmatch() (from "pattern match") and also simplifies inserting other ways of matching a string. Signed-off-by: Michał Kiedrowicz <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5a69eaf commit 97e7778

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

grep.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,19 @@ static int regmatch(const regex_t *preg, char *line, char *eol,
412412
return regexec(preg, line, 1, match, eflags);
413413
}
414414

415+
static int patmatch(struct grep_pat *p, char *line, char *eol,
416+
regmatch_t *match, int eflags)
417+
{
418+
int hit;
419+
420+
if (p->fixed)
421+
hit = !fixmatch(p, line, eol, match);
422+
else
423+
hit = !regmatch(&p->regexp, line, eol, match, eflags);
424+
425+
return hit;
426+
}
427+
415428
static int strip_timestamp(char *bol, char **eol_p)
416429
{
417430
char *eol = *eol_p;
@@ -461,10 +474,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
461474
}
462475

463476
again:
464-
if (p->fixed)
465-
hit = !fixmatch(p, bol, eol, pmatch);
466-
else
467-
hit = !regmatch(&p->regexp, bol, eol, pmatch, eflags);
477+
hit = patmatch(p, bol, eol, pmatch, eflags);
468478

469479
if (hit && p->word_regexp) {
470480
if ((pmatch[0].rm_so < 0) ||
@@ -791,10 +801,7 @@ static int look_ahead(struct grep_opt *opt,
791801
int hit;
792802
regmatch_t m;
793803

794-
if (p->fixed)
795-
hit = !fixmatch(p, bol, bol + *left_p, &m);
796-
else
797-
hit = !regmatch(&p->regexp, bol, bol + *left_p, &m, 0);
804+
hit = patmatch(p, bol, bol + *left_p, &m, 0);
798805
if (!hit || m.rm_so < 0 || m.rm_eo < 0)
799806
continue;
800807
if (earliest < 0 || m.rm_so < earliest)

0 commit comments

Comments
 (0)