Skip to content

Commit f96e567

Browse files
René Scharfegitster
René Scharfe
authored andcommitted
grep: use REG_STARTEND for all matching if available
Refactor REG_STARTEND handling inlook_ahead() into a new helper, regmatch(), and use it for line matching, too. This allows regex matching beyond NUL characters if regexec() supports the flag. NUL characters themselves are not matched in any way, though. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 52d799a commit f96e567

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

grep.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,17 @@ static int fixmatch(const char *pattern, char *line, char *eol,
356356
}
357357
}
358358

359+
static int regmatch(const regex_t *preg, char *line, char *eol,
360+
regmatch_t *match, int eflags)
361+
{
362+
#ifdef REG_STARTEND
363+
match->rm_so = 0;
364+
match->rm_eo = eol - line;
365+
eflags |= REG_STARTEND;
366+
#endif
367+
return regexec(preg, line, 1, match, eflags);
368+
}
369+
359370
static int strip_timestamp(char *bol, char **eol_p)
360371
{
361372
char *eol = *eol_p;
@@ -408,7 +419,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
408419
if (p->fixed)
409420
hit = !fixmatch(p->pattern, bol, eol, p->ignore_case, pmatch);
410421
else
411-
hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
422+
hit = !regmatch(&p->regexp, bol, eol, pmatch, eflags);
412423

413424
if (hit && p->word_regexp) {
414425
if ((pmatch[0].rm_so < 0) ||
@@ -735,15 +746,8 @@ static int look_ahead(struct grep_opt *opt,
735746
if (p->fixed) {
736747
hit = !fixmatch(p->pattern, bol, bol + *left_p,
737748
p->ignore_case, &m);
738-
} else {
739-
#ifdef REG_STARTEND
740-
m.rm_so = 0;
741-
m.rm_eo = *left_p;
742-
hit = !regexec(&p->regexp, bol, 1, &m, REG_STARTEND);
743-
#else
744-
hit = !regexec(&p->regexp, bol, 1, &m, 0);
745-
#endif
746-
}
749+
} else
750+
hit = !regmatch(&p->regexp, bol, bol + *left_p, &m, 0);
747751
if (!hit || m.rm_so < 0 || m.rm_eo < 0)
748752
continue;
749753
if (earliest < 0 || m.rm_so < earliest)

t/t7008-grep-binary.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,14 @@ test_expect_success 'git grep -Fi iLE a' '
5959
git grep -Fi iLE a
6060
'
6161

62+
# This test actually passes on platforms where regexec() supports the
63+
# flag REG_STARTEND.
64+
test_expect_failure 'git grep ile a' '
65+
git grep ile a
66+
'
67+
68+
test_expect_failure 'git grep .fi a' '
69+
git grep .fi a
70+
'
71+
6272
test_done

0 commit comments

Comments
 (0)