Skip to contentSkip to conversation

Fix "grep -w"

RFC/PATCH Junio C Hamano 2 participants 4 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

We used to find the first match of the pattern and then if the match is not for the entire word, declared that the whole line does not match.

But that is wrong. The command "git grep -w -e mmap" should find that a line "foo_mmap bar mmap baz" matches, by tring the second instance of pattern "mmap" on the same line.

---

 builtin-grep.c  |   10 ++++++++++
 t/t7002-grep.sh |   45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 0 deletions(-)
builtin-grep.c +10 -0
index 69b7c48..b5feda4 100644
@@ -412,6 +412,7 @@ static int match_one_pattern(struct grep
 	int hit = 0;
 	regmatch_t pmatch[10];
 
+ again:
 	if (!opt->fixed) {
 		regex_t *exp = &p->regexp;
 		hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
@@ -438,6 +439,15 @@ static int match_one_pattern(struct grep
 		if (pmatch[0].rm_eo != (eol-bol) &&
 		    word_char(bol[pmatch[0].rm_eo]))
 			hit = 0;
+
+		if (!hit && pmatch[0].rm_eo + bol < eol) {
+			/* there could be more than one match on the
+			 * line, and the first match might not be
+			 * strict word match.  But later ones could be!
+			 */
+			bol += pmatch[0].rm_eo;
+			goto again;
+		}
 	}
 	return hit;
 }
t/t7002-grep.sh new file +45 -0

45 lines not on this page · raw message

#1 lore raw

1. Are you sure that going to the end of the first match is correct? It seems to
me that this will skip matches. Say you search for ".*" on a line that reads " xxx".

2. What about "^"?

3. What about empty matches? That could take a while...

Morten

#2 lore raw
Quoted 4 lines · Morten Welinder

1. Are you sure that going to the end of the first match is correct? It seems to me that this will skip matches. Say you search for ".*" on a line that reads
" xxx".

It is fine for your example, I think. .* matches the entire line the first time, and BOL and EOL are defined to be word boundaries. But you are right. If the pattern is "x xx* x" and the line is "x x xx x", the first round would match the first 5 bytes, we find that 6th byte 'x' makes it not a word boundary, and redoing the match starting at 6th is a wrong thing to do. We should find "x xx x" starting at the 3rd byte.

Quoted 1 line

2. What about "^"?

The pattern would not match the second time anyway, so I do not think it is such a big deal.

But there is another bug I just spotted. git grep -w -e '^x' matches line "xxx" (when not cheating with external grep).

Quoted 1 line

3. What about empty matches? That could take a while...

True. So we would need to make sure we advance at least one.

#3 lore raw

We used to find the first match of the pattern and then if the match is not for the entire word, declared that the whole line does not match.

But that is wrong. The command "git grep -w -e mmap" should find that a line "foo_mmap bar mmap baz" matches, by tring the second instance of pattern "mmap" on the same line.

Problems an earlier round of "fix" had were pointed out by Morten Welinder, which have been incorporated in the t7002 tests.

---

* Second try.

 builtin-grep.c  |   35 ++++++++++++++++-------
 t/t7002-grep.sh |   85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+), 10 deletions(-)
builtin-grep.c +25 -10
index 69b7c48..93b7e07 100644
@@ -410,8 +410,10 @@ static int fixmatch(const char *pattern,
 static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol)
 {
 	int hit = 0;
+	int at_true_bol = 1;
 	regmatch_t pmatch[10];
 
+ again:
 	if (!opt->fixed) {
 		regex_t *exp = &p->regexp;
 		hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
@@ -422,22 +424,35 @@ static int match_one_pattern(struct grep
 	}
 
 	if (hit && opt->word_regexp) {
-		/* Match beginning must be either
-		 * beginning of the line, or at word
-		 * boundary (i.e. the last char must
-		 * not be alnum or underscore).
-		 */
 		if ((pmatch[0].rm_so < 0) ||
 		    (eol - bol) <= pmatch[0].rm_so ||
 		    (pmatch[0].rm_eo < 0) ||
 		    (eol - bol) < pmatch[0].rm_eo)
 			die("regexp returned nonsense");
-		if (pmatch[0].rm_so != 0 &&
-		    word_char(bol[pmatch[0].rm_so-1]))
-			hit = 0;
-		if (pmatch[0].rm_eo != (eol-bol) &&
-		    word_char(bol[pmatch[0].rm_eo]))
+
+		/* Match beginning must be either beginning of the
+		 * line, or at word boundary (i.e. the last char must
+		 * not be a word char).  Similarly, match end must be
+		 * either end of the line, or at word boundary
+		 * (i.e. the next char must not be a word char).
+		 */
+		if ( ((pmatch[0].rm_so == 0 && at_true_bol) ||
+		      !word_char(bol[pmatch[0].rm_so-1])) &&
+		     ((pmatch[0].rm_eo == (eol-bol)) ||
+		      !word_char(bol[pmatch[0].rm_eo])) )
+			;
+		else
 			hit = 0;
+
+		if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
+			/* There could be more than one match on the
+			 * line, and the first match might not be
+			 * strict word match.  But later ones could be!
+			 */
+			bol = pmatch[0].rm_so + bol + 1;
+			at_true_bol = 0;
+			goto again;
+		}
 	}
 	return hit;
 }
t/t7002-grep.sh new file +85 -0

85 lines not on this page · raw message

Signature · 1 line
1.4.2.rc3.g19a8a