git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] grep: fix colouring of matches with zero length

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Jun 1, 2009, 21:53 UTC
Message-ID
<4A244DC1.1090801@lsrfire.ath.cx>
In-Reply-To
<7voct7k61r.fsf@alter.siamese.dyndns.org>
Junio C Hamano schrieb:
Show 16 quoted lines
> René Scharfe <rene.scharfe@lsrfire.ath.cx> writes:
> 
>> The first hunk fixes match colouring if the patterns is the empty string.
>> Such a pattern matches the whole line,...
> 
> Hmmm...
> 
> An empty pattern may have to produce a match with any line, but I do not
> think it should match the whole line.  GNU seems to agree with me.
> 
> 	echo foo | grep --color -e ''
>         echo foo | grep --color -F -e ''
>         echo foo | grep --color -e '.*'
>         echo foo | grep --color -F -e 'foo'
> 
> The first two are uncoloured (but still show matches).

You're right, and matching the zero-length string at the beginning of the line actually makes sense. Corrected patch below.

If a zero-length match is encountered, break out of loop and show the rest of the line uncoloured. Otherwise we'd be looping forever, trying to make progress by advancing the pointer by zero characters.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 grep.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
diff --git a/grep.c b/grep.c
index cc6d5b0..7bf4a60 100644
--- a/grep.c
+++ b/grep.c
@@ -500,6 +500,8 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 
 		*eol = '\0';
 		while (next_match(opt, bol, eol, ctx, &match, eflags)) {
+			if (match.rm_so == match.rm_eo)
+				break;
 			printf("%.*s%s%.*s%s",
 			       (int)match.rm_so, bol,
 			       opt->color_match,
-- 
1.6.3.1
Previous: Junio C Hamano
Message 3 of 3 in “grep: fix colouring of matches with zero length”
  1. grep: fix colouring of matches with zero lengthRené Scharfe, Jun 1, 2009
  2. Junio C HamanoJun 1, 2009
  3. René ScharfeJun 1, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.