Subject: bug#18800: [PATCH 1/2] dfa: prefer bool at DFA interfaces

From: Norihiro Tanaka <[email-masked]>
Date: Thu, 23 Oct 2014 08:25:32 +0900

Norihiro Tanaka <[email-masked]> wrote:
> This change is use bool for booleans at DFA interfaces.  Callers in Gawk
> have already used bool instead of int for them.  Second patch changes
> caller with change in first patch.

Sorry, Previous change was imcomplete.  I fixed it, and added a third
patch.
From e4e13f8ed43b70fc874e0b8d84afb3c6382d7722 Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Sat, 18 Oct 2014 23:57:33 +0900
Subject: [PATCH 1/3] dfa: prefer bool at DFA interfaces

* (src/dfa.c) dfasyntax: Use bool for `fold'.
dfaanalyze, dfacomp: Use bool for `searchflag'.
dfaexec_main, dfaexec_mb, dfaexec_sb, dfaexec: IUse bool for `allow_nl'.
* (src/dfa.h): Update prototype.
---
 src/dfa.c | 20 ++++++++++----------
 src/dfa.h |  8 ++++----
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/src/dfa.c b/src/dfa.c
index 80510a8..b0b0c47 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -343,7 +343,7 @@ struct dfa
   mbstate_t mbs;		/* Multibyte conversion state.  */
 
   /* dfaexec implementation.  */
-  char *(*dfaexec) (struct dfa *, char const *, char *, int, size_t *, int *);
+  char *(*dfaexec) (struct dfa *, char const *, char *, bool, size_t *, int *);
 
   /* The following are valid only if MB_CUR_MAX > 1.  */
 
@@ -720,13 +720,13 @@ wchar_context (wint_t wc)
 
 /* Entry point to set syntax options.  */
 void
-dfasyntax (reg_syntax_t bits, int fold, unsigned char eol)
+dfasyntax (reg_syntax_t bits, bool fold, unsigned char eol)
 {
   unsigned int i;
 
   syntax_bits_set = 1;
   syntax_bits = bits;
-  case_fold = fold != 0;
+  case_fold = fold;
   eolbyte = eol;
 
   for (i = 0; i < NOTCHAR; ++i)
@@ -2320,7 +2320,7 @@ state_separate_contexts (position_set const *s)
    scheme; the number of elements in each set deeper in the stack can be
    used to determine the address of a particular set's array.  */
 void
-dfaanalyze (struct dfa *d, int searchflag)
+dfaanalyze (struct dfa *d, bool searchflag)
 {
   /* Array allocated to hold position sets.  */
   position *posalloc = xnmalloc (d->nleaves, 2 * sizeof *posalloc);
@@ -2356,7 +2356,7 @@ dfaanalyze (struct dfa *d, int searchflag)
   putc ('\n', stderr);
 #endif
 
-  d->searchflag = searchflag != 0;
+  d->searchflag = searchflag;
   alloc_position_set (&merged, d->nleaves);
   d->follows = xcalloc (d->tindex, sizeof *d->follows);
 
@@ -3266,7 +3266,7 @@ skip_remains_mb (struct dfa *d, unsigned char const *p,
    characters.  */
 static inline char *
 dfaexec_main (struct dfa *d, char const *begin, char *end,
-             int allow_nl, size_t *count, int *backref, bool multibyte)
+             bool allow_nl, size_t *count, int *backref, bool multibyte)
 {
   state_num s, s1;              /* Current state.  */
   unsigned char const *p, *mbp; /* Current input character.  */
@@ -3456,14 +3456,14 @@ dfaexec_main (struct dfa *d, char const *begin, char *end,
 
 static char *
 dfaexec_mb (struct dfa *d, char const *begin, char *end,
-            int allow_nl, size_t *count, int *backref)
+            bool allow_nl, size_t *count, int *backref)
 {
   return dfaexec_main (d, begin, end, allow_nl, count, backref, true);
 }
 
 static char *
 dfaexec_sb (struct dfa *d, char const *begin, char *end,
-            int allow_nl, size_t *count, int *backref)
+            bool allow_nl, size_t *count, int *backref)
 {
   return dfaexec_main (d, begin, end, allow_nl, count, backref, false);
 }
@@ -3473,7 +3473,7 @@ dfaexec_sb (struct dfa *d, char const *begin, char *end,
 
 char *
 dfaexec (struct dfa *d, char const *begin, char *end,
-         int allow_nl, size_t *count, int *backref)
+         bool allow_nl, size_t *count, int *backref)
 {
   return d->dfaexec (d, begin, end, allow_nl, count, backref);
 }
@@ -3653,7 +3653,7 @@ dfassbuild (struct dfa *d)
 
 /* Parse and analyze a single string of the given length.  */
 void
-dfacomp (char const *s, size_t len, struct dfa *d, int searchflag)
+dfacomp (char const *s, size_t len, struct dfa *d, bool searchflag)
 {
   dfainit (d);
   dfambcache (d);
diff --git a/src/dfa.h b/src/dfa.h
index f30c3cb..ab2e8ea 100644
--- a/src/dfa.h
+++ b/src/dfa.h
@@ -49,12 +49,12 @@ extern struct dfamust *dfamusts (struct dfa const *);
 /* dfasyntax() takes three arguments; the first sets the syntax bits described
    earlier in this file, the second sets the case-folding flag, and the
    third specifies the line terminator. */
-extern void dfasyntax (reg_syntax_t, int, unsigned char);
+extern void dfasyntax (reg_syntax_t, bool, unsigned char);
 
 /* Compile the given string of the given length into the given struct dfa.
    Final argument is a flag specifying whether to build a searching or an
    exact matcher. */
-extern void dfacomp (char const *, size_t, struct dfa *, int);
+extern void dfacomp (char const *, size_t, struct dfa *, bool);
 
 /* Search through a buffer looking for a match to the given struct dfa.
    Find the first occurrence of a string matching the regexp in the
@@ -63,13 +63,13 @@ extern void dfacomp (char const *, size_t, struct dfa *, int);
    points to the beginning of the buffer, and END points to the first byte
    after its end.  Note however that we store a sentinel byte (usually
    newline) in *END, so the actual buffer must be one byte longer.
-   When NEWLINE is nonzero, newlines may appear in the matching string.
+   When ALLOW_NL is true, newlines may appear in the matching string.
    If COUNT is non-NULL, increment *COUNT once for each newline processed.
    Finally, if BACKREF is non-NULL set *BACKREF to indicate whether we
    encountered a back-reference (1) or not (0).  The caller may use this
    to decide whether to fall back on a backtracking matcher. */
 extern char *dfaexec (struct dfa *d, char const *begin, char *end,
-                      int newline, size_t *count, int *backref);
+                      bool allow_nl, size_t *count, int *backref);
 
 /* Return a superset for D.  The superset matches everything that D
    matches, along with some other strings (though the latter should be
-- 
2.1.1

From 8a998a5afbd8d3b0d09db64939957ccce7b7de4b Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Thu, 23 Oct 2014 07:50:15 +0900
Subject: [PATCH 2/3] grep: change caller for previous change

* (src/dfasearch.c) GEAcompile: Use bool for boolean.
---
 src/dfasearch.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/dfasearch.c b/src/dfasearch.c
index 77b4e3e..d667470 100644
--- a/src/dfasearch.c
+++ b/src/dfasearch.c
@@ -199,7 +199,7 @@ GEAcompile (char const *pattern, size_t size, reg_syntax_t syntax_bits)
     motif = NULL;
 
   dfa = dfaalloc ();
-  dfacomp (pattern, size, dfa, 1);
+  dfacomp (pattern, size, dfa, true);
   kwsmusts ();
 
   free(motif);
@@ -287,8 +287,8 @@ EGexecute (char const *buf, size_t size, size_t *match_size,
               /* Keep using the superset while it reports multiline
                  potential matches; this is more likely to be fast
                  than falling back to KWset would be.  */
-              while ((next_beg = dfaexec (superset, dfa_beg, (char *) end, 1,
-                                          &count, NULL))
+              while ((next_beg = dfaexec (superset, dfa_beg, (char *) end,
+                                          true, &count, NULL))
                      && next_beg != end
                      && count != 0)
                 {
@@ -307,7 +307,8 @@ EGexecute (char const *buf, size_t size, size_t *match_size,
             }
 
           /* Try matching with DFA.  */
-          next_beg = dfaexec (dfa, dfa_beg, (char *) end, 0, &count, &backref);
+          next_beg = dfaexec (dfa, dfa_beg, (char *) end, false, &count,
+                              &backref);
 
           /* If there's no match, or if we've matched the sentinel,
              we're done.  */
-- 
2.1.1

From a077e3d282137bb04267d4897e165b0577be0daf Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Thu, 23 Oct 2014 08:08:45 +0900
Subject: [PATCH 3/3] dfa: change internal functions into static

* src/dfa.c (dfaparse, dfaanalyze dfastate, dfainit) change them into
static.
* src/dfa.h: Remove prototype for changed into static.
---
 src/dfa.c |  8 ++++----
 src/dfa.h | 14 --------------
 2 files changed, 4 insertions(+), 18 deletions(-)

diff --git a/src/dfa.c b/src/dfa.c
index b0b0c47..5723597 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -1957,7 +1957,7 @@ regexp (void)
 /* Main entry point for the parser.  S is a string to be parsed, len is the
    length of the string, so s can include NUL characters.  D is a pointer to
    the struct dfa to parse into.  */
-void
+static void
 dfaparse (char const *s, size_t len, struct dfa *d)
 {
   dfa = d;
@@ -2319,7 +2319,7 @@ state_separate_contexts (position_set const *s)
    Sets are stored as arrays of the elements, obeying a stack-like allocation
    scheme; the number of elements in each set deeper in the stack can be
    used to determine the address of a particular set's array.  */
-void
+static void
 dfaanalyze (struct dfa *d, bool searchflag)
 {
   /* Array allocated to hold position sets.  */
@@ -2558,7 +2558,7 @@ dfaanalyze (struct dfa *d, bool searchflag)
    If after comparing with every group there are characters remaining in C,
    create a new group labeled with the characters of C and insert this
    position in that group.  */
-void
+static void
 dfastate (state_num s, struct dfa *d, state_num trans[])
 {
   leaf_set grps[NOTCHAR];       /* As many as will ever be needed.  */
@@ -3522,7 +3522,7 @@ free_mbdata (struct dfa *d)
 
 /* Initialize the components of a dfa that the other routines don't
    initialize for themselves.  */
-void
+static void
 dfainit (struct dfa *d)
 {
   memset (d, 0, sizeof *d);
diff --git a/src/dfa.h b/src/dfa.h
index ab2e8ea..5983f64 100644
--- a/src/dfa.h
+++ b/src/dfa.h
@@ -85,20 +85,6 @@ extern void dfafree (struct dfa *);
 
 /* Entry points for people who know what they're doing. */
 
-/* Initialize the components of a struct dfa. */
-extern void dfainit (struct dfa *);
-
-/* Incrementally parse a string of given length into a struct dfa. */
-extern void dfaparse (char const *, size_t, struct dfa *);
-
-/* Analyze a parsed regexp; second argument tells whether to build a searching
-   or an exact matcher. */
-extern void dfaanalyze (struct dfa *, int);
-
-/* Compute, for each possible character, the transitions out of a given
-   state, storing them in an array of integers. */
-extern void dfastate (ptrdiff_t, struct dfa *, ptrdiff_t []);
-
 /* Error handling. */
 
 /* dfawarn() is called by the regexp routines whenever a regex is compiled
-- 
2.1.1