Skip to contentSkip to conversation

git-grep colouring bug with -w option.

PATCH In review Dmitry Gryazin 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

I've found that "git-grep --color -w VirtualKeybord" works not correct for string like:

vkbd = RVirtualKeyboard(main_window, "VirtualKeyboard")

My script in attachment reproduces this bug.

#1 lore raw

PATCH 1/1 grep: fix word-regexp colouring

As noticed by Dmitry Gryazin: When a pattern is found but it doesn't start and end at word boundaries, bol is forwarded to after the match and the pattern is searched again. When a pattern is finally found between word boundaries, the match offsets are off by the number of characters that have been skipped.

This patch corrects the offsets to be relative to the value of bol as passed to match_one_pattern() by its caller.

---
 grep.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
grep.c +5 -0
index 04c777a..a649f06 100644
@@ -305,6 +305,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
 {
 	int hit = 0;
 	int saved_ch = 0;
+	const char *start = bol;
 
 	if ((p->token != GREP_PATTERN) &&
 	    ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
@@ -365,6 +366,10 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
 	}
 	if (p->token == GREP_PATTERN_HEAD && saved_ch)
 		*eol = saved_ch;
+	if (hit) {
+		pmatch[0].rm_so += bol - start;
+		pmatch[0].rm_eo += bol - start;
+	}
 	return hit;
 }
 
Signature · 1 line
1.6.3.1
#2 lore raw

Re: [PATCH] grep: fix word-regexp colouring

Quoted 28 lines · René Scharfe

As noticed by Dmitry Gryazin: When a pattern is found but it doesn't start and end at word boundaries, bol is forwarded to after the match and the pattern is searched again. When a pattern is finally found between word boundaries, the match offsets are off by the number of characters that have been skipped.

This patch corrects the offsets to be relative to the value of bol as passed to match_one_pattern() by its caller.

---
 grep.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
grep.c +1 -0
index 04c777a..a649f06 100644
@@ -305,6 +305,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
 {
 	int hit = 0;
 	int saved_ch = 0;
+	const char *start = bol;
 
 	if ((p->token != GREP_PATTERN) &&
 	    ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))

grep.c static int match_one_pattern(struct grep_pat *p, char *bol, char *eol, · line 375

Quoted 10 lines
grep.c +4 -0
@@ -365,6 +366,10 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
 	}
 	if (p->token == GREP_PATTERN_HEAD && saved_ch)
 		*eol = saved_ch;
+	if (hit) {
+		pmatch[0].rm_so += bol - start;
+		pmatch[0].rm_eo += bol - start;
+	}
 	return hit;
 }
 

Thanks.

ACK