grep: remove grep_opt argument from match_expr_eval()

The only use of the struct grep_opt argument of match_expr_eval()
is to pass the option word_regexp to match_one_pattern().  By adding
a pattern flag for it we can reduce the number of function arguments
of these two functions, as a cleanup and preparation for adding more
in the next patch.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe 2009-03-07 13:28:40 +01:00 коммит произвёл Junio C Hamano
Родитель 252d560d21
Коммит d7eb527d73
2 изменённых файлов: 18 добавлений и 17 удалений

34
grep.c
Просмотреть файл

@ -39,6 +39,8 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
{
int err;
p->word_regexp = opt->word_regexp;
if (opt->fixed || is_fixed(p->pattern))
p->fixed = 1;
if (opt->regflags & REG_ICASE)
@ -306,7 +308,8 @@ static struct {
{ "committer ", 10 },
};
static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol, enum grep_context ctx)
static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
enum grep_context ctx)
{
int hit = 0;
int saved_ch = 0;
@ -338,7 +341,7 @@ static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol
hit = !fixmatch(p->pattern, bol, pmatch);
}
if (hit && opt->word_regexp) {
if (hit && p->word_regexp) {
if ((pmatch[0].rm_so < 0) ||
(eol - bol) <= pmatch[0].rm_so ||
(pmatch[0].rm_eo < 0) ||
@ -378,35 +381,32 @@ static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol
return hit;
}
static int match_expr_eval(struct grep_opt *o,
struct grep_expr *x,
char *bol, char *eol,
enum grep_context ctx,
int collect_hits)
static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
enum grep_context ctx, int collect_hits)
{
int h = 0;
switch (x->node) {
case GREP_NODE_ATOM:
h = match_one_pattern(o, x->u.atom, bol, eol, ctx);
h = match_one_pattern(x->u.atom, bol, eol, ctx);
break;
case GREP_NODE_NOT:
h = !match_expr_eval(o, x->u.unary, bol, eol, ctx, 0);
h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
break;
case GREP_NODE_AND:
if (!match_expr_eval(o, x->u.binary.left, bol, eol, ctx, 0))
if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
return 0;
h = match_expr_eval(o, x->u.binary.right, bol, eol, ctx, 0);
h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
break;
case GREP_NODE_OR:
if (!collect_hits)
return (match_expr_eval(o, x->u.binary.left,
return (match_expr_eval(x->u.binary.left,
bol, eol, ctx, 0) ||
match_expr_eval(o, x->u.binary.right,
match_expr_eval(x->u.binary.right,
bol, eol, ctx, 0));
h = match_expr_eval(o, x->u.binary.left, bol, eol, ctx, 0);
h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
x->u.binary.left->hit |= h;
h |= match_expr_eval(o, x->u.binary.right, bol, eol, ctx, 1);
h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
break;
default:
die("Unexpected node type (internal error) %d", x->node);
@ -420,7 +420,7 @@ static int match_expr(struct grep_opt *opt, char *bol, char *eol,
enum grep_context ctx, int collect_hits)
{
struct grep_expr *x = opt->pattern_expression;
return match_expr_eval(opt, x, bol, eol, ctx, collect_hits);
return match_expr_eval(x, bol, eol, ctx, collect_hits);
}
static int match_line(struct grep_opt *opt, char *bol, char *eol,
@ -432,7 +432,7 @@ static int match_line(struct grep_opt *opt, char *bol, char *eol,
/* we do not call with collect_hits without being extended */
for (p = opt->pattern_list; p; p = p->next) {
if (match_one_pattern(opt, p, bol, eol, ctx))
if (match_one_pattern(p, bol, eol, ctx))
return 1;
}
return 0;

1
grep.h
Просмотреть файл

@ -31,6 +31,7 @@ struct grep_pat {
enum grep_header_field field;
regex_t regexp;
unsigned fixed:1;
unsigned word_regexp:1;
};
enum grep_expr_node {