[PATCH 2/2] findutils/grep.c: Short-circuit -v to bail out on first match

Ari Sundholm ari at tuxera.com
Mon Jan 28 15:11:19 UTC 2019


A small optimization. There is no need to try matching the current
input line against any further patterns if a match was already
found and -v is specified.

Signed-off-by: Ari Sundholm <ari at tuxera.com>
Signed-off-by: Niko Vähäsarja <niko at tuxera.com>
---
 findutils/grep.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/findutils/grep.c b/findutils/grep.c
index 9d9da422c..6abefffdc 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -447,6 +447,9 @@ static int grep_file(FILE *file)
 			 * at first match */
 			if (found && !invert_search)
 				goto do_found;
+			else if (invert_search && found)
+				break;
+
 			pattern_ptr = pattern_ptr->link;
 		} /* while (pattern_ptr) */
 
-- 
2.17.1



More information about the busybox mailing list