Subject: bug#43863: closed (Re: bug#43863: [PATCH] grep: remove unusable code)

From: [email-masked] (GNU bug Tracking System)
Date: Sun, 11 Oct 2020 16:57:01 +0000

Your bug report

#43863: [PATCH] grep: remove unusable code

which was filed against the grep package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
43863: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=43863
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#43863: [PATCH] grep: remove unusable code
From: Paul Eggert <[email-masked]>
To: Norihiro Tanaka <[email-masked]>, Shlomi Fish <[email-masked]>
Cc: [email-masked]
Date: Sun, 11 Oct 2020 09:56:36 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

Thanks for the patch. I installed it, and also installed the attached minor 
cleanups of related code that I discovered while reviewing it.
>From f31abf786f61f4bdd7134559a5f155fc9c8c2513 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sun, 11 Oct 2020 09:53:22 -0700
Subject: [PATCH] grep: minor kwset cleanups

* src/kwsearch.c (Fexecute):
Assume C99 to put declarations nearer uses.
* src/kwset.c (bmexec): Omit unnecessary test.
* src/kwset.h (struct kwsmatch): Make OFFSET and SIZE individual
elements, not arrays of size 1 (a revenant of an earlier API).
All uses changed.
---
 src/kwsearch.c | 36 ++++++++++++------------------------
 src/kwset.c    | 14 +++++---------
 src/kwset.h    |  6 +++---
 3 files changed, 20 insertions(+), 36 deletions(-)

diff --git a/src/kwsearch.c b/src/kwsearch.c
index 1b31917..685502d 100644
--- a/src/kwsearch.c
+++ b/src/kwsearch.c
@@ -87,8 +87,8 @@ Fcompile (char *pattern, size_t size, reg_syntax_t ignored, bool exact)
   while (p <= pattern + size);
 
   free (buf);
-  ptrdiff_t words = kwswords (kwset);
 
+  ptrdiff_t words = kwswords (kwset);
   kwsprep (kwset);
 
   struct kwsearch *kwsearch = xmalloc (sizeof *kwsearch);
@@ -111,32 +111,22 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
   char const *beg, *end, *mb_start;
   ptrdiff_t len;
   char eol = eolbyte;
-  struct kwsmatch kwsmatch;
-  size_t ret_val;
-  bool mb_check;
-  bool longest;
   struct kwsearch *kwsearch = vcp;
   kwset_t kwset = kwsearch->kwset;
-  size_t mbclen;
-
-  if (match_lines)
-    mb_check = longest = false;
-  else
-    {
-      mb_check = localeinfo.multibyte & !localeinfo.using_utf8;
-      longest = mb_check | !!start_ptr | match_words;
-    }
+  bool mb_check = localeinfo.multibyte & !localeinfo.using_utf8 & !match_lines;
+  bool longest = (mb_check | !!start_ptr | match_words) & !match_lines;
 
   for (mb_start = beg = start_ptr ? start_ptr : buf; beg <= buf + size; beg++)
     {
+      struct kwsmatch kwsmatch;
       ptrdiff_t offset = kwsexec (kwset, beg - match_lines,
                                   buf + size - beg + match_lines, &kwsmatch,
                                   longest);
       if (offset < 0)
         break;
-      len = kwsmatch.size[0] - 2 * match_lines;
+      len = kwsmatch.size - 2 * match_lines;
 
-      mbclen = 0;
+      size_t mbclen = 0;
       if (mb_check
           && mb_goback (&mb_start, &mbclen, beg + offset, buf + size) != 0)
         {
@@ -216,10 +206,11 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
               }
             if (!len)
               break;
-            offset = kwsexec (kwset, beg, --len, &kwsmatch, true);
-            if (offset != 0)
+
+            struct kwsmatch shorter_match;
+            if (kwsexec (kwset, beg, --len, &shorter_match, true) != 0)
               break;
-            len = kwsmatch.size[0];
+            len = shorter_match.size;
           }
 
       /* No word match was found at BEG.  Skip past word constituents,
@@ -227,7 +218,7 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
          them could make things much slower.  */
       beg += wordchars_size (beg, buf + size);
       mb_start = beg;
-    } /* for (beg in buf) */
+    }
 
   return -1;
 
@@ -244,9 +235,6 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
   beg = beg ? beg + 1 : buf;
   len = end - beg;
  success_in_beg_and_len:;
-  size_t off = beg - buf;
-
   *match_size = len;
-  ret_val = off;
-  return ret_val;
+  return beg - buf;
 }
diff --git a/src/kwset.c b/src/kwset.c
index 403af7e..b271e39 100644
--- a/src/kwset.c
+++ b/src/kwset.c
@@ -760,13 +760,9 @@ bmexec (kwset_t kwset, char const *text, ptrdiff_t size,
                    (kwset->trans
                     ? bmexec_trans (kwset, text, size)
                     : bmexec_trans (kwset, text, size)));
-  if (0 <= ret)
-    {
-       kwsmatch->index = 0;
-       kwsmatch->offset[0] = ret;
-       kwsmatch->size[0] = kwset->mind;
-    }
-
+  kwsmatch->index = 0;
+  kwsmatch->offset = ret;
+  kwsmatch->size = kwset->mind;
   return ret;
 }
 
@@ -891,8 +887,8 @@ acexec_trans (kwset_t kwset, char const *text, ptrdiff_t len,
     }
 
   kwsmatch->index = accept->accepting / 2;
-  kwsmatch->offset[0] = left - text;
-  kwsmatch->size[0] = accept->depth;
+  kwsmatch->offset = left - text;
+  kwsmatch->size = accept->depth;
 
   return left - text;
 }
diff --git a/src/kwset.h b/src/kwset.h
index 793e210..eee3d73 100644
--- a/src/kwset.h
+++ b/src/kwset.h
@@ -24,9 +24,9 @@
 
 struct kwsmatch
 {
-  ptrdiff_t index;			/* Index number of matching keyword.  */
-  ptrdiff_t offset[1];		/* Offset of match.  */
-  ptrdiff_t size[1];		/* Length of match.  */
+  ptrdiff_t index;	/* Index number of matching keyword.  */
+  ptrdiff_t offset;	/* Offset of match.  */
+  ptrdiff_t size;	/* Length of match.  */
 };
 
 #include "arg-nonnull.h"
-- 
2.25.1

Subject: [PATCH] grep: remove unusable code
From: Norihiro Tanaka <[email-masked]>
To: <[email-masked]>
Date: Thu, 08 Oct 2020 18:55:50 +0900
List-Id: <debbugs-submit.debbugs.gnu.org>

Codes described in attached patch in this mail is dead.

These codes make sense when a maltibyte character is returned in case
conversion for single byte character, but I believe that their cases are
removed in commit 016e590a8198009bce0e1078f6d4c7e037e2df3c.
From e62e727ac271b1cecf5542eebd07eb8501053d08 Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Thu, 8 Oct 2020 18:46:32 +0900
Subject: [PATCH] grep: remove unusable code

* src/kwsearch.c (Fcompile, Fexecute): Remove unusable code.  No longer these
are used after commit 016e590a8198009bce0e1078f6d4c7e037e2df3c.
---
 src/kwsearch.c |   47 -----------------------------------------------
 1 files changed, 0 insertions(+), 47 deletions(-)

diff --git a/src/kwsearch.c b/src/kwsearch.c
index 1174dbc..1b31917 100644
--- a/src/kwsearch.c
+++ b/src/kwsearch.c
@@ -89,38 +89,6 @@ Fcompile (char *pattern, size_t size, reg_syntax_t ignored, bool exact)
   free (buf);
   ptrdiff_t words = kwswords (kwset);
 
-  if (match_icase)
-    {
-      /* For each pattern character C that has a case folded
-         counterpart F that is multibyte and so cannot easily be
-         implemented via translating a single byte, append a pattern
-         containing just F.  That way, if the data contains F, the
-         matcher can fall back on DFA.  For example, if C is 'i' and
-         the locale is en_US.utf8, append a pattern containing just
-         the character U+0131 (LATIN SMALL LETTER DOTLESS I), so that
-         Fexecute will use a DFA if the data contain U+0131.  */
-      mbstate_t mbs = { 0 };
-      char checked[NCHAR] = {0,};
-      for (p = pattern; p < pattern + size; p++)
-        {
-          unsigned char c = *p;
-          if (checked[c])
-            continue;
-          checked[c] = true;
-
-          wint_t wc = localeinfo.sbctowc[c];
-          wchar_t folded[CASE_FOLDED_BUFSIZE];
-
-          for (int i = case_folded_counterparts (wc, folded); 0 <= --i; )
-            {
-              char s[MB_LEN_MAX];
-              int nbytes = wcrtomb (s, folded[i], &mbs);
-              if (1 < nbytes)
-                kwsincr (kwset, s, nbytes);
-            }
-        }
-    }
-
   kwsprep (kwset);
 
   struct kwsearch *kwsearch = xmalloc (sizeof *kwsearch);
@@ -168,21 +136,6 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
         break;
       len = kwsmatch.size[0] - 2 * match_lines;
 
-      if (kwsearch->words <= kwsmatch.index)
-        {
-          /* The data contain a multibyte character that matches
-             some pattern character that is a case folded counterpart.
-             Since the kwset code cannot handle this case, fall back
-             on the DFA code, which can.  */
-          if (! kwsearch->re)
-            {
-              fgrep_to_grep_pattern (&kwsearch->pattern, &kwsearch->size);
-              kwsearch->re = GEAcompile (kwsearch->pattern, kwsearch->size,
-                                         RE_SYNTAX_GREP, !!start_ptr);
-            }
-          return EGexecute (kwsearch->re, buf, size, match_size, start_ptr);
-        }
-
       mbclen = 0;
       if (mb_check
           && mb_goback (&mb_start, &mbclen, beg + offset, buf + size) != 0)
-- 
1.7.1