Skip to content

Commit 7921277

Browse files
René Scharfegitster
authored andcommitted
grep: add pmatch and eflags arguments to match_one_pattern()
Push pmatch and eflags to the callers of match_one_pattern(), which allows them to specify regex execution flags and to get the location of a match. Since we only use the first element of the matches array and aren't interested in submatches, no provision is made for callers to provide a larger array. eflags are ignored for fixed patterns, but that's OK, since they only have a meaning in connection with regular expressions containing ^ or $. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent d7eb527 commit 7921277

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

grep.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,11 @@ static struct {
309309
};
310310

311311
static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
312-
enum grep_context ctx)
312+
enum grep_context ctx,
313+
regmatch_t *pmatch, int eflags)
313314
{
314315
int hit = 0;
315316
int saved_ch = 0;
316-
regmatch_t pmatch[10];
317317

318318
if ((p->token != GREP_PATTERN) &&
319319
((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
@@ -332,14 +332,10 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
332332
}
333333

334334
again:
335-
if (!p->fixed) {
336-
regex_t *exp = &p->regexp;
337-
hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
338-
pmatch, 0);
339-
}
340-
else {
335+
if (p->fixed)
341336
hit = !fixmatch(p->pattern, bol, pmatch);
342-
}
337+
else
338+
hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
343339

344340
if (hit && p->word_regexp) {
345341
if ((pmatch[0].rm_so < 0) ||
@@ -385,10 +381,11 @@ static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
385381
enum grep_context ctx, int collect_hits)
386382
{
387383
int h = 0;
384+
regmatch_t match;
388385

389386
switch (x->node) {
390387
case GREP_NODE_ATOM:
391-
h = match_one_pattern(x->u.atom, bol, eol, ctx);
388+
h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
392389
break;
393390
case GREP_NODE_NOT:
394391
h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
@@ -427,12 +424,14 @@ static int match_line(struct grep_opt *opt, char *bol, char *eol,
427424
enum grep_context ctx, int collect_hits)
428425
{
429426
struct grep_pat *p;
427+
regmatch_t match;
428+
430429
if (opt->extended)
431430
return match_expr(opt, bol, eol, ctx, collect_hits);
432431

433432
/* we do not call with collect_hits without being extended */
434433
for (p = opt->pattern_list; p; p = p->next) {
435-
if (match_one_pattern(p, bol, eol, ctx))
434+
if (match_one_pattern(p, bol, eol, ctx, &match, 0))
436435
return 1;
437436
}
438437
return 0;

0 commit comments

Comments
 (0)