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>
René Scharfe committed
Mar 7, 2009 at 13:30 UTC
79212772ce127cc32a67d50eaaa7f1c102ce7d5d
1 file changed
+10
-11
grep.c
+10
-11
@@ -309,11 +309,11 @@ static struct {
309
};
310
311
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)
314
{
315
int hit = 0;
316
int saved_ch = 0;
316
- regmatch_t pmatch[10];
317
318
if ((p->token != GREP_PATTERN) &&
319
((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,
332
}
333
334
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)
336
hit = !fixmatch(p->pattern, bol, pmatch);
342
- }
337
+ else
338
+ hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
339
340
if (hit && p->word_regexp) {
341
if ((pmatch[0].rm_so < 0) ||
@@ -385,10 +381,11 @@ static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
381
enum grep_context ctx, int collect_hits)
382
{
383
int h = 0;
384
+ regmatch_t match;
385
386
switch (x->node) {
387
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);
389
break;
390
case GREP_NODE_NOT:
391
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,
424
enum grep_context ctx, int collect_hits)
425
{
426
struct grep_pat *p;
427
+ regmatch_t match;
428
+
429
if (opt->extended)
430
return match_expr(opt, bol, eol, ctx, collect_hits);
431
432
/* we do not call with collect_hits without being extended */
433
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))
435
return 1;
436
}
437
return 0;