提交 219e65b6 编写于 作者: Æ Ævar Arnfjörð Bjarmason 提交者: Junio C Hamano

grep: factor test for \0 in grep patterns into a function

Factor the test for \0 in grep patterns into a function. Since commit
9eceddee ("Use kwset in grep", 2011-08-21) any pattern containing a
\0 is considered fixed as regcomp() can't handle it.

This change makes later changes that make use of either has_null() or
is_fixed() (but not both) smaller.

While I'm at it make the comment conform to the style guide, i.e. add
an opening "/*\n".
Signed-off-by: NÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 e0b9f8ae
...@@ -321,6 +321,18 @@ static NORETURN void compile_regexp_failed(const struct grep_pat *p, ...@@ -321,6 +321,18 @@ static NORETURN void compile_regexp_failed(const struct grep_pat *p,
die("%s'%s': %s", where, p->pattern, error); die("%s'%s': %s", where, p->pattern, error);
} }
static int has_null(const char *s, size_t len)
{
/*
* regcomp cannot accept patterns with NULs so when using it
* we consider any pattern containing a NUL fixed.
*/
if (memchr(s, 0, len))
return 1;
return 0;
}
#ifdef USE_LIBPCRE #ifdef USE_LIBPCRE
static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt) static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
{ {
...@@ -394,12 +406,6 @@ static int is_fixed(const char *s, size_t len) ...@@ -394,12 +406,6 @@ static int is_fixed(const char *s, size_t len)
{ {
size_t i; size_t i;
/* regcomp cannot accept patterns with NULs so we
* consider any pattern containing a NUL fixed.
*/
if (memchr(s, 0, len))
return 1;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (is_regex_special(s[i])) if (is_regex_special(s[i]))
return 0; return 0;
...@@ -451,7 +457,9 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) ...@@ -451,7 +457,9 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
* simple string match using kws. p->fixed tells us if we * simple string match using kws. p->fixed tells us if we
* want to use kws. * want to use kws.
*/ */
if (opt->fixed || is_fixed(p->pattern, p->patternlen)) if (opt->fixed ||
has_null(p->pattern, p->patternlen) ||
is_fixed(p->pattern, p->patternlen))
p->fixed = !icase || ascii_only; p->fixed = !icase || ascii_only;
else else
p->fixed = 0; p->fixed = 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册