Subject: bug#17420: [PATCH] grep: always convert fgrep to grep

From: Norihiro Tanaka <[email-masked]>
Date: Tue, 06 May 2014 22:36:57 +0900

fgrep uses different matcher from grep, but there is no reason why it
must be so.  Further more, fgrep matcher is slower than grep matcher in
many cases, because it never uses DFA.  This patch converts fgrep to grep
any time.

By the way, if `FGREP_NO_DFA' env. is set, fgrep still uses fgrep matcher.
It may be required in some cases.  fgrep matcher requires less memory
than grep matcher even if a pattern is too long, and/or is faster than
it in some cases to have multiple patterns.
From a326b5fbfcb3ae547001dcc051272ae88cebcc7d Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Tue, 6 May 2014 16:00:34 +0900
Subject: [PATCH] grep: always convert fgrep to grep

src/grep.c (main): Do it.
---
 src/grep.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/src/grep.c b/src/grep.c
index a661fc0..35f4c75 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -2336,18 +2336,22 @@ main (int argc, char **argv)
   /* If fgrep in a multibyte locale, then use grep if either
      (1) case is ignored (where grep is typically faster), or
      (2) the pattern has an encoding error (where fgrep might not work).  */
-  if (compile == Fcompile && MB_CUR_MAX > 1
-      && (match_icase || contains_encoding_error (keys, keycc)))
+  if (compile == Fcompile)
     {
-      size_t new_keycc;
-      char *new_keys;
-      fgrep_to_grep_pattern (keycc, keys, &new_keycc, &new_keys);
-      free (keys);
-      keys = new_keys;
-      keycc = new_keycc;
-      matcher = "grep";
-      compile = Gcompile;
-      execute = EGexecute;
+      if (!getenv ("FGREP_NO_DFA"))
+        {
+          size_t new_keycc;
+          char *new_keys;
+          fgrep_to_grep_pattern (keycc, keys, &new_keycc, &new_keys);
+          free (keys);
+          keys = new_keys;
+          keycc = new_keycc;
+          matcher = "grep";
+          compile = Gcompile;
+          execute = EGexecute;
+        }
+      else if (contains_encoding_error (keys, keycc))
+        error (EXIT_TROUBLE, 0, _("contains encoding error"));
     }
 
   if (MB_CUR_MAX > 1)
-- 
1.9.2