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 <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
May 22, 2010 at 23:35 UTC
f96e56733ab3e3ce5c79c27c673c746af1519a86
2 files changed
+24
-10
grep.c
+14
-10
@@ -356,6 +356,17 @@ static int fixmatch(const char *pattern, char *line, char *eol,
356
}
357
}
358
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
+
370
static int strip_timestamp(char *bol, char **eol_p)
371
{
372
char *eol = *eol_p;
@@ -408,7 +419,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
419
if (p->fixed)
420
hit = !fixmatch(p->pattern, bol, eol, p->ignore_case, pmatch);
421
else
411
- hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
422
+ hit = !regmatch(&p->regexp, bol, eol, pmatch, eflags);
423
424
if (hit && p->word_regexp) {
425
if ((pmatch[0].rm_so < 0) ||
@@ -735,15 +746,8 @@ static int look_ahead(struct grep_opt *opt,
746
if (p->fixed) {
747
hit = !fixmatch(p->pattern, bol, bol + *left_p,
748
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);
751
if (!hit || m.rm_so < 0 || m.rm_eo < 0)
752
continue;
753
if (earliest < 0 || m.rm_so < earliest)
t/t7008-grep-binary.sh
+10
@@ -59,4 +59,14 @@ test_expect_success 'git grep -Fi iLE a' '
59
git grep -Fi iLE a
60
'
61
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
+
72
test_done