[PATCH 1/2] regex: new _REGEX_AVOID_UCHAR_H option

Paul Eggert <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs
Message-ID <[email protected]>
This is for GNU Emacs, where the regex code is only auxiliary,
probably not enough to justify the hassle of incorporating
Gnulib’s uchar-h module and its dependencies.
The following changes are in effect only if _REGEX_AVOID_UCHAR_H,
and cause the code to avoid the need for Gnulib’s uchar-h module.
* lib/regcomp.c: Don’t include localeinfo.h.
(re_set_fastmap_icase): Don’t use localeinfo.h functions,
as they rely on char32_t.
* lib/regex_internal.h: Just use of wchar_t, wctype_t and related
functions, instead of using char32_t and related functions.
---
 ChangeLog            | 14 ++++++++++++++
 lib/regcomp.c        |  4 ++--
 lib/regex_internal.h | 38 ++++++++++++++++++++++++++------------
 3 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 8892a3d09b..169ee053dd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2026-05-24  Paul Eggert  <[email protected]>
+
+	regex: new _REGEX_AVOID_UCHAR_H option
+	This is for GNU Emacs, where the regex code is only auxiliary,
+	probably not enough to justify the hassle of incorporating
+	Gnulib’s uchar-h module and its dependencies.
+	The following changes are in effect only if _REGEX_AVOID_UCHAR_H,
+	and cause the code to avoid the need for Gnulib’s uchar-h module.
+	* lib/regcomp.c: Don’t include localeinfo.h.
+	(re_set_fastmap_icase): Don’t use localeinfo.h functions,
+	as they rely on char32_t.
+	* lib/regex_internal.h: Just use of wchar_t, wctype_t and related
+	functions, instead of using char32_t and related functions.
+
 2026-05-22  Paul Eggert  <[email protected]>
 
 	regex-tests: omit unused parts
diff --git a/lib/regcomp.c b/lib/regcomp.c
index ab8716991c..ab3783f94c 100644
--- a/lib/regcomp.c
+++ b/lib/regcomp.c
@@ -23,7 +23,7 @@
 
 /* The localeinfo-related code fixes glibc bug 20381.
    Someday this fix should be merged into glibc.  */
-#ifndef _LIBC
+#if !defined _LIBC && !defined _REGEX_AVOID_UCHAR_H
 # include "localeinfo.h"
 #endif
 
@@ -285,7 +285,7 @@ re_set_fastmap (char *fastmap, unsigned char ch)
 static void
 re_set_fastmap_icase (char *fastmap, wchar_t wc, mbstate_t *mbs)
 {
-#ifdef _LIBC
+#if defined _LIBC || defined _REGEX_AVOID_UCHAR_H
   wchar_t folded[1] = {__towlower (wc)};
   int nfolded = folded[0] != wc;
 #else
diff --git a/lib/regex_internal.h b/lib/regex_internal.h
index 2409170b0a..bf6c9ba3b8 100644
--- a/lib/regex_internal.h
+++ b/lib/regex_internal.h
@@ -119,13 +119,18 @@
 #define NEWLINE_CHAR '\n'
 #define WIDE_NEWLINE_CHAR L'\n'
 
-/* Use Gnulib <uchar.h> when outside of glibc.  */
-#ifdef _LIBC
+/* Use Gnulib <uchar.h> if outside glibc and not avoided by the app.  */
+#if defined _LIBC || defined _REGEX_AVOID_UCHAR_H
 # include <wchar.h>
 # include <wctype.h>
 #else
 # include <uchar.h>
 # undef wctype_t
+# define wchar_t char32_t
+# define wctype_t c32_type_test_t
+#endif
+
+#ifndef _LIBC
 # undef __wctype
 # undef __iswalnum
 # undef __iswctype
@@ -135,17 +140,26 @@
 # undef __mbrtowc
 # undef __wcrtomb
 # undef __regfree
-# define wchar_t char32_t
-# define wctype_t c32_type_test_t
-# define __wctype c32_get_type_test
-# define __iswalnum c32isalnum
-# define __iswctype c32_apply_type_test
-# define __towlower c32tolower
-# define __towupper c32toupper
-# define __btowc btoc32
-# define __mbrtowc mbrtoc32
-# define __wcrtomb c32rtomb
 # define __regfree regfree
+# ifdef _REGEX_AVOID_UCHAR_H
+#  define __wctype wctype
+#  define __iswalnum iswalnum
+#  define __iswctype iswctype
+#  define __towlower towlower
+#  define __towupper towupper
+#  define __btowc btowc
+#  define __mbrtowc mbrtowc
+#  define __wcrtomb wcrtomb
+# else
+#  define __wctype c32_get_type_test
+#  define __iswalnum c32isalnum
+#  define __iswctype c32_apply_type_test
+#  define __towlower c32tolower
+#  define __towupper c32toupper
+#  define __btowc btoc32
+#  define __mbrtowc mbrtoc32
+#  define __wcrtomb c32rtomb
+# endif
 #endif /* not _LIBC */
 
 /* Types related to integers.  Unless protected by #ifdef _LIBC, the
-- 
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.