Subject: [PATCH 3/4] Get rid of re_set_whitespace_regexp

From: Michal Nazarewicz <[email-masked]>
Date: Thu, 28 Jul 2016 20:07:16 +0200

* src/regex.h (re_set_whitespace_regexp): Delete.
(re_compile_pattern): Add whitespace_regexp argument #ifdef emacs.

* src/regex.c (re_set_whitespace_regexp, whitespace_regexp): Delete.
(regex_compile): Add whitespace_regexp argument #ifdef emacs and wrap
whitespace_regexp-related code in an #ifdef emacs so it’s compiled out
unless building Emacs.
(re_compile_pattern): Pass whitespace_regexp argument to regex_compile

* src/search.c (compile_pattern_1): Don’t use re_set_whitespace_regexp,
pass the regex as argument to re_compile_pattern instead.
---
 src/regex.c  | 39 ++++++++++++++++++++++++---------------
 src/regex.h  |  3 +--
 src/search.c | 13 +++++--------
 3 files changed, 30 insertions(+), 25 deletions(-)

diff --git a/src/regex.c b/src/regex.c
index 4edc064..c32a62f 100644
--- a/src/regex.c
+++ b/src/regex.c
@@ -1177,16 +1177,6 @@ re_set_syntax (reg_syntax_t syntax)
 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
 
 #endif
-
-/* Regexp to use to replace spaces, or NULL meaning don't.  */
-static const_re_char *whitespace_regexp;
-
-void
-re_set_whitespace_regexp (const char *regexp)
-{
-  whitespace_regexp = (const_re_char *) regexp;
-}
-WEAK_ALIAS (__re_set_syntax, re_set_syntax)
 
 /* This table gives an error message for each of the error codes listed
    in regex.h.  Obviously the order here has to be same as there.
@@ -1569,6 +1559,9 @@ do {									\
 
 static reg_errcode_t regex_compile (re_char *pattern, size_t size,
 				    reg_syntax_t syntax,
+#ifdef emacs
+				    const char *whitespace_regexp,
+#endif
 				    struct re_pattern_buffer *bufp);
 static void store_op1 (re_opcode_t op, unsigned char *loc, int arg);
 static void store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2);
@@ -2398,6 +2391,9 @@ static boolean group_in_compile_stack (compile_stack_type compile_stack,
 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
    Returns one of error codes defined in `regex.h', or zero for success.
 
+   If WHITESPACE_REGEXP is given (only #ifdef emacs), it is used instead of
+   a space character in PATTERN.
+
    Assumes the `allocated' (and perhaps `buffer') and `translate'
    fields are set in BUFP on entry.
 
@@ -2431,6 +2427,9 @@ do {									\
 
 static reg_errcode_t
 regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
+#ifdef emacs
+	       const char *whitespace_regexp,
+#endif
 	       struct re_pattern_buffer *bufp)
 {
   /* We fetch characters from PATTERN here.  */
@@ -2483,6 +2482,7 @@ regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
   /* If the object matched can contain multibyte characters.  */
   const boolean multibyte = RE_MULTIBYTE_P (bufp);
 
+#ifdef emacs
   /* Nonzero if we have pushed down into a subpattern.  */
   int in_subpattern = 0;
 
@@ -2491,6 +2491,7 @@ regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
   re_char *main_p;
   re_char *main_pattern;
   re_char *main_pend;
+#endif
 
 #ifdef DEBUG
   debug++;
@@ -2559,6 +2560,7 @@ regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
     {
       if (p == pend)
 	{
+#ifdef emacs
 	  /* If this is the end of an included regexp,
 	     pop back to the main regexp and try again.  */
 	  if (in_subpattern)
@@ -2569,6 +2571,7 @@ regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
 	      pend = main_pend;
 	      continue;
 	    }
+#endif
 	  /* If this is the end of the main regexp, we are done.  */
 	  break;
 	}
@@ -2577,6 +2580,7 @@ regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
 
       switch (c)
 	{
+#ifdef emacs
 	case ' ':
 	  {
 	    re_char *p1 = p;
@@ -2609,6 +2613,7 @@ regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
 	    pend = p + strlen ((const char *) p);
 	    break;
 	  }
+#endif
 
 	case '^':
 	  {
@@ -6276,13 +6281,10 @@ bcmp_translate (const_re_char *s1, const_re_char *s2, register ssize_t len,
 const char *
 re_compile_pattern (const char *pattern, size_t length,
 #ifdef emacs
-		    reg_syntax_t syntax,
+		    reg_syntax_t syntax, const char *whitespace_regexp,
 #endif
 		    struct re_pattern_buffer *bufp)
 {
-#ifndef emacs
-  const reg_syntax_t syntax = re_syntax_options;
-#endif
   reg_errcode_t ret;
 
   /* GNU code is written to assume at least RE_NREGS registers will be set
@@ -6294,7 +6296,14 @@ re_compile_pattern (const char *pattern, size_t length,
      setting no_sub.  */
   bufp->no_sub = 0;
 
-  ret = regex_compile ((re_char*) pattern, length, syntax, bufp);
+  ret = regex_compile ((re_char*) pattern, length,
+#ifdef emacs
+		       syntax,
+		       whitespace_regexp,
+#else
+		       re_syntax_options,
+#endif
+		       bufp);
 
   if (!ret)
     return NULL;
diff --git a/src/regex.h b/src/regex.h
index 4497333..af9480d 100644
--- a/src/regex.h
+++ b/src/regex.h
@@ -474,6 +474,7 @@ extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);
 extern const char *re_compile_pattern (const char *__pattern, size_t __length,
 #ifdef emacs
 				       reg_syntax_t syntax,
+				       const char *whitespace_regexp,
 #endif
 				       struct re_pattern_buffer *__buffer);
 
@@ -627,8 +628,6 @@ extern re_wctype_t re_wctype_parse (const unsigned char **strp, unsigned limit);
 
 typedef int re_wchar_t;
 
-extern void re_set_whitespace_regexp (const char *regexp);
-
 #endif /* not WIDE_CHAR_SUPPORT */
 
 #endif /* regex.h */
diff --git a/src/search.c b/src/search.c
index f041952..c7556a9 100644
--- a/src/search.c
+++ b/src/search.c
@@ -113,6 +113,7 @@ static void
 compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
 		   Lisp_Object translate, bool posix)
 {
+  const char *whitespace_regexp;
   reg_syntax_t syntax;
   char *val;
 
@@ -132,21 +133,17 @@ compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
      So let's turn it off.  */
   /*  BLOCK_INPUT;  */
 
-  if (STRINGP (Vsearch_spaces_regexp))
-    re_set_whitespace_regexp (SSDATA (Vsearch_spaces_regexp));
-  else
-    re_set_whitespace_regexp (NULL);
-
   syntax = RE_SYNTAX_EMACS | (posix ? 0 : RE_NO_POSIX_BACKTRACKING);
+  whitespace_regexp = STRINGP (Vsearch_spaces_regexp) ?
+    SSDATA (Vsearch_spaces_regexp) : NULL;
+
   val = (char *) re_compile_pattern (SSDATA (pattern), SBYTES (pattern),
-				     syntax, &cp->buf);
+				     syntax, whitespace_regexp, &cp->buf);
 
   /* If the compiled pattern hard codes some of the contents of the
      syntax-table, it can only be reused with *this* syntax table.  */
   cp->syntax_table = cp->buf.used_syntax ? BVAR (current_buffer, syntax_table) : Qt;
 
-  re_set_whitespace_regexp (NULL);
-
   /* unblock_input ();  */
   if (val)
     xsignal1 (Qinvalid_regexp, build_string (val));
-- 
2.8.0.rc3.226.g39d4020