Subject: bug#18800: dfa: prefer bool at DFA interfaces

From: Paul Eggert <[email-masked]>
Date: Wed, 20 Apr 2016 23:57:46 -0700

In updating Bug#18000's patches to the current grep source, I couldn't build 
with just the first patch installed, so I squashed the first two patches into 
one. Also, I changed a few more 'int's into 'bool's and fixed a comment or two. 
The result is attached. Again, I'm not installing this yet, as we're expecting a 
new release soon.
>From 24cfee9b9923db293e977742f2792eec39b0f0dc Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Wed, 20 Apr 2016 23:38:43 -0700
Subject: [PATCH 1/2] dfa: prefer bool at DFA interfaces

* src/dfa.c (struct dfa, dfasyntax, dfaanalyze, dfaexec_main)
(dfaexec_mb, dfaexec_sb, dfaexec_noop, dfaexec, dfacomp):
* src/dfa.h (dfasyntax, dfacomp, dfaexec, dfaanalyze):
* src/dfasearch.c (EGexecute):
Use bool for boolean.
---
 src/dfa.c       | 25 +++++++++++++------------
 src/dfa.h       | 14 +++++++-------
 src/dfasearch.c |  2 +-
 3 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/src/dfa.c b/src/dfa.c
index e609801..ee5fe88 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -327,7 +327,8 @@ 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 *, bool *);
 
   /* The following are valid only if MB_CUR_MAX > 1.  */
 
@@ -689,12 +690,12 @@ 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)
 {
   int i;
   syntax_bits_set = 1;
   syntax_bits = bits;
-  case_fold = fold != 0;
+  case_fold = fold;
   eolbyte = eol;
 
   for (i = CHAR_MIN; i <= CHAR_MAX; ++i)
@@ -2292,7 +2293,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);
@@ -2328,7 +2329,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);
 
@@ -3210,7 +3211,7 @@ skip_remains_mb (struct dfa *d, unsigned char const *p,
     - word-delimiter-in-MB-locale: \<, \>, \b
     */
 static inline char *
-dfaexec_main (struct dfa *d, char const *begin, char *end, int allow_nl,
+dfaexec_main (struct dfa *d, char const *begin, char *end, bool allow_nl,
              size_t *count, bool multibyte)
 {
   state_num s, s1;              /* Current state.  */
@@ -3402,14 +3403,14 @@ dfaexec_main (struct dfa *d, char const *begin, char *end, int allow_nl,
 
 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, bool *backref)
 {
   return dfaexec_main (d, begin, end, allow_nl, count, 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, bool *backref)
 {
   return dfaexec_main (d, begin, end, allow_nl, count, false);
 }
@@ -3418,9 +3419,9 @@ dfaexec_sb (struct dfa *d, char const *begin, char *end,
    any regexp that uses a construct not supported by this code.  */
 static char *
 dfaexec_noop (struct dfa *d, char const *begin, char *end,
-              int allow_nl, size_t *count, int *backref)
+              bool allow_nl, size_t *count, bool *backref)
 {
-  *backref = 1;
+  *backref = true;
   return (char *) begin;
 }
 
@@ -3429,7 +3430,7 @@ dfaexec_noop (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, bool *backref)
 {
   return d->dfaexec (d, begin, end, allow_nl, count, backref);
 }
@@ -3622,7 +3623,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);
   dfaparse (s, len, d);
diff --git a/src/dfa.h b/src/dfa.h
index fb9ac9a..23b8783 100644
--- a/src/dfa.h
+++ b/src/dfa.h
@@ -53,12 +53,12 @@ extern void dfamustfree (struct dfamust *);
 /* 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
@@ -67,13 +67,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. */
+   encountered a back-reference.  The caller can 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, bool *backref);
 
 /* Return a superset for D.  The superset matches everything that D
    matches, along with some other strings (though the latter should be
@@ -97,7 +97,7 @@ 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);
+extern void dfaanalyze (struct dfa *, bool);
 
 /* Compute, for each possible character, the transitions out of a given
    state, storing them in an array of integers. */
diff --git a/src/dfasearch.c b/src/dfasearch.c
index d348d44..d966e7e 100644
--- a/src/dfasearch.c
+++ b/src/dfasearch.c
@@ -226,7 +226,7 @@ EGexecute (char *buf, size_t size, size_t *match_size,
           char const *next_beg, *dfa_beg = beg;
           size_t count = 0;
           bool exact_kwset_match = false;
-          int backref = 0;
+          bool backref = false;
 
           /* Try matching with KWset, if it's defined.  */
           if (kwset)
-- 
2.5.5

>From e1ae97cb4b6e75b8815a71e0f1c21cb6ad0df208 Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Wed, 20 Apr 2016 23:44:03 -0700
Subject: [PATCH 2/2] dfa: stop exporting internal functions

* src/dfa.c, src/dfa.h (dfaparse, dfaanalyze, dfastate, dfainit):
Now static.
---
 src/dfa.c |  8 ++++----
 src/dfa.h | 16 ----------------
 2 files changed, 4 insertions(+), 20 deletions(-)

diff --git a/src/dfa.c b/src/dfa.c
index ee5fe88..a2aee29 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -1913,7 +1913,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;
@@ -2292,7 +2292,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.  */
@@ -2538,7 +2538,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.  */
@@ -3468,7 +3468,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 23b8783..ce267ec 100644
--- a/src/dfa.h
+++ b/src/dfa.h
@@ -87,22 +87,6 @@ extern bool dfaisfast (struct dfa const *) _GL_ATTRIBUTE_PURE;
 /* Free the storage held by the components of a struct dfa. */
 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 *, bool);
-
-/* 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.5.5