Skip to content

Commit 219e65b

Browse files
avargitster
authored andcommitted
grep: factor test for \0 in grep patterns into a function
Factor the test for \0 in grep patterns into a function. Since commit 9ecedde ("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: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e0b9f8a commit 219e65b

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

grep.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,18 @@ static NORETURN void compile_regexp_failed(const struct grep_pat *p,
321321
die("%s'%s': %s", where, p->pattern, error);
322322
}
323323

324+
static int has_null(const char *s, size_t len)
325+
{
326+
/*
327+
* regcomp cannot accept patterns with NULs so when using it
328+
* we consider any pattern containing a NUL fixed.
329+
*/
330+
if (memchr(s, 0, len))
331+
return 1;
332+
333+
return 0;
334+
}
335+
324336
#ifdef USE_LIBPCRE
325337
static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
326338
{
@@ -394,12 +406,6 @@ static int is_fixed(const char *s, size_t len)
394406
{
395407
size_t i;
396408

397-
/* regcomp cannot accept patterns with NULs so we
398-
* consider any pattern containing a NUL fixed.
399-
*/
400-
if (memchr(s, 0, len))
401-
return 1;
402-
403409
for (i = 0; i < len; i++) {
404410
if (is_regex_special(s[i]))
405411
return 0;
@@ -451,7 +457,9 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
451457
* simple string match using kws. p->fixed tells us if we
452458
* want to use kws.
453459
*/
454-
if (opt->fixed || is_fixed(p->pattern, p->patternlen))
460+
if (opt->fixed ||
461+
has_null(p->pattern, p->patternlen) ||
462+
is_fixed(p->pattern, p->patternlen))
455463
p->fixed = !icase || ascii_only;
456464
else
457465
p->fixed = 0;

0 commit comments

Comments
 (0)