grep: factor test for \0 in grep patterns into a function
Factor the test for \0 in grep patterns into a function. Since commit 9eceddeec6 ("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 <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
May 25, 2017 at 19:45 UTC
219e65b65ca0f1352f5fbd4233520b6fc5be3726
1 file changed
+15
-7
grep.c
+15
-7
@@ -321,6 +321,18 @@ static NORETURN void compile_regexp_failed(const struct grep_pat *p,
321
die("%s'%s': %s", where, p->pattern, error);
322
}
323
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
+
336
#ifdef USE_LIBPCRE
337
static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
338
{
@@ -394,12 +406,6 @@ static int is_fixed(const char *s, size_t len)
406
{
407
size_t i;
408
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
-
409
for (i = 0; i < len; i++) {
410
if (is_regex_special(s[i]))
411
return 0;
@@ -451,7 +457,9 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
457
* simple string match using kws. p->fixed tells us if we
458
* want to use kws.
459
*/
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))
463
p->fixed = !icase || ascii_only;
464
else
465
p->fixed = 0;