- Simplify the fixed string pattern preprocessing code
- Improve readability Approved by: delphij (mentor) Obtained from: The NetBSD Project
This commit is contained in:
parent
acac8baf59
commit
d841ecb30d
@ -81,27 +81,25 @@ fastcomp(fastgrep_t *fg, const char *pat)
|
||||
int hasDot = 0;
|
||||
int lastHalfDot = 0;
|
||||
int shiftPatternLen;
|
||||
bool bol = false;
|
||||
bool eol = false;
|
||||
|
||||
/* Initialize. */
|
||||
fg->len = strlen(pat);
|
||||
fg->bol = false;
|
||||
fg->eol = false;
|
||||
fg->reversed = false;
|
||||
fg->word = wflag;
|
||||
|
||||
/* Remove end-of-line character ('$'). */
|
||||
if (fg->len > 0 && pat[fg->len - 1] == '$') {
|
||||
eol = true;
|
||||
fg->eol = true;
|
||||
fg->len--;
|
||||
}
|
||||
|
||||
/* Remove beginning-of-line character ('^'). */
|
||||
if (pat[0] == '^') {
|
||||
bol = true;
|
||||
fg->bol = true;
|
||||
fg->len--;
|
||||
pat++;
|
||||
}
|
||||
|
||||
if (fg->len >= 14 &&
|
||||
@ -110,7 +108,7 @@ fastcomp(fastgrep_t *fg, const char *pat)
|
||||
fg->len -= 14;
|
||||
pat += 7;
|
||||
/* Word boundary is handled separately in util.c */
|
||||
wflag = true;
|
||||
fg->word = true;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -149,7 +147,7 @@ fastcomp(fastgrep_t *fg, const char *pat)
|
||||
* Determine if a reverse search would be faster based on the placement
|
||||
* of the dots.
|
||||
*/
|
||||
if ((!(lflag || cflag)) && ((!(bol || eol)) &&
|
||||
if ((!(lflag || cflag)) && ((!(fg->bol || fg->eol)) &&
|
||||
((lastHalfDot) && ((firstHalfDot < 0) ||
|
||||
((fg->len - (lastHalfDot + 1)) < (size_t)firstHalfDot)))) &&
|
||||
!oflag && !color) {
|
||||
|
@ -102,6 +102,7 @@ typedef struct {
|
||||
bool bol;
|
||||
bool eol;
|
||||
bool reversed;
|
||||
bool word;
|
||||
} fastgrep_t;
|
||||
|
||||
/* Flags passed to regcomp() and regexec() */
|
||||
|
@ -55,14 +55,15 @@ static int procline(struct str *l, int);
|
||||
bool
|
||||
file_matching(const char *fname)
|
||||
{
|
||||
char *fname_base;
|
||||
bool ret;
|
||||
|
||||
ret = finclude ? false : true;
|
||||
fname_base = basename(fname);
|
||||
|
||||
for (unsigned int i = 0; i < fpatterns; ++i) {
|
||||
if (fnmatch(fpattern[i].pat,
|
||||
fname, 0) == 0 || fnmatch(fpattern[i].pat,
|
||||
basename(fname), 0) == 0) {
|
||||
if (fnmatch(fpattern[i].pat, fname, 0) == 0 ||
|
||||
fnmatch(fpattern[i].pat, fname_base, 0) == 0) {
|
||||
if (fpattern[i].mode == EXCL_PAT)
|
||||
return (false);
|
||||
else
|
||||
@ -277,7 +278,7 @@ procfile(const char *fn)
|
||||
* matches. The matching lines are passed to printline() to display the
|
||||
* appropriate output.
|
||||
*/
|
||||
static inline int
|
||||
static int
|
||||
procline(struct str *l, int nottext)
|
||||
{
|
||||
regmatch_t matches[MAX_LINE_MATCHES];
|
||||
@ -318,7 +319,8 @@ procline(struct str *l, int nottext)
|
||||
(size_t)pmatch.rm_eo != l->len)
|
||||
r = REG_NOMATCH;
|
||||
/* Check for whole word match */
|
||||
if (r == 0 && wflag && pmatch.rm_so != 0) {
|
||||
if (r == 0 && fg_pattern[i].word &&
|
||||
pmatch.rm_so != 0) {
|
||||
wint_t wbegin, wend;
|
||||
|
||||
wbegin = wend = L' ';
|
||||
|
Loading…
x
Reference in New Issue
Block a user