diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2020-09-07 19:44:21 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2020-09-07 19:49:33 -0700 |
commit | 9393b977015bf7944cec1d71ad3972c101bdb4b8 (patch) | |
tree | 75ec43b843346d5972f5eaace2d72ea8e37e11f3 /src/pcresearch.c | |
parent | 0ede35a6cd21093560de8bd9843263ba199abf1f (diff) | |
download | grep-9393b977015bf7944cec1d71ad3972c101bdb4b8.tar.gz |
Prefer rawmemchr to memchr when it’s easy
* bootstrap.conf (gnulib_modules): Add rawmemchr.
* src/dfasearch.c (GEAcompile, EGexecute):
* src/grep.c (update_patterns, prpending, prtext):
* src/kwsearch.c (Fcompile, Fexecute):
* src/pcresearch.c (Pcompile, Pexecute):
Simplify (and presumably speed up a little) by using rawmemchr
with a sentinel, instead of using memchr.
Diffstat (limited to 'src/pcresearch.c')
-rw-r--r-- | src/pcresearch.c | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/src/pcresearch.c b/src/pcresearch.c index 15a6a59b..2fcbf8e6 100644 --- a/src/pcresearch.c +++ b/src/pcresearch.c @@ -107,6 +107,9 @@ jit_exec (struct pcre_comp *pc, char const *subject, int search_bytes, } } +/* Compile the -P style PATTERN, containing SIZE bytes that are + followed by '\n'. Return a description of the compiled pattern. */ + void * Pcompile (char *pattern, size_t size, reg_syntax_t ignored) { @@ -120,7 +123,7 @@ Pcompile (char *pattern, size_t size, reg_syntax_t ignored) sizeof xprefix - 1 + sizeof xsuffix - 1); char *re = xnmalloc (4, size + (fix_len_max + 4 - 1) / 4); int flags = PCRE_DOLLAR_ENDONLY | (match_icase ? PCRE_CASELESS : 0); - char const *patlim = pattern + size; + char *patlim = pattern + size; char *n = re; char const *p; char const *pnul; @@ -134,7 +137,7 @@ Pcompile (char *pattern, size_t size, reg_syntax_t ignored) } /* FIXME: Remove this restriction. */ - if (memchr (pattern, '\n', size)) + if (rawmemchr (pattern, '\n') != patlim) die (EXIT_TROUBLE, 0, _("the -P option only supports a single pattern")); *n = '\0'; @@ -148,7 +151,8 @@ Pcompile (char *pattern, size_t size, reg_syntax_t ignored) replace each NUL byte in the pattern with the four characters "\000", removing a preceding backslash if there are an odd number of backslashes before the NUL. */ - for (p = pattern; (pnul = memchr (p, '\0', patlim - p)); p = pnul + 1) + *patlim = '\0'; + for (p = pattern; (pnul = p + strlen (p)) < patlim; p = pnul + 1) { memcpy (n, p, pnul - p); n += pnul - p; @@ -158,10 +162,10 @@ Pcompile (char *pattern, size_t size, reg_syntax_t ignored) strcpy (n, "\\000"); n += 4; } - - memcpy (n, p, patlim - p); + memcpy (n, p, patlim - p + 1); n += patlim - p; - *n = '\0'; + *patlim = '\n'; + if (match_words) strcpy (n, wsuffix); if (match_lines) @@ -219,7 +223,7 @@ Pexecute (void *vcp, char const *buf, size_t size, size_t *match_size, PCRE_MULTILINE for performance, the performance wasn't always better and the correctness issues were too puzzling. See Bug#22655. */ - line_end = memchr (p, eolbyte, buf + size - p); + line_end = rawmemchr (p, eolbyte); if (INT_MAX < line_end - p) die (EXIT_TROUBLE, 0, _("exceeded PCRE's line length limit")); |