From 1a602613384e6dcec05eb693bc63726600520cd7 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Wed, 26 Jul 2023 14:13:48 +0200
Subject: [PATCH] wctype-h: Work around iswprint bug on mingw.

* lib/wctype.in.h (rpl_iswprint): On mingw, don't use the system's
iswprint function.
* tests/test-wctype-h.c (main): Verify that this character class
contains the ASCII space but not tab and newline.
* tests/test-c32isprint.c (main): For tab, \v, \f, expect the same value
on native Windows as on other platforms.
* doc/posix-functions/iswprint.texi: Mention the mingw bug.
---
 ChangeLog                         | 11 +++++++++++
 doc/posix-functions/iswprint.texi |  4 ++++
 lib/wctype.in.h                   |  7 ++++++-
 tests/test-c32isprint.c           |  2 --
 tests/test-wctype-h.c             |  5 +++++
 5 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3d602638de..930191fd98 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2023-07-26  Bruno Haible  <bruno@clisp.org>
+
+	wctype-h: Work around iswprint bug on mingw.
+	* lib/wctype.in.h (rpl_iswprint): On mingw, don't use the system's
+	iswprint function.
+	* tests/test-wctype-h.c (main): Verify that this character class
+	contains the ASCII space but not tab and newline.
+	* tests/test-c32isprint.c (main): For tab, \v, \f, expect the same value
+	on native Windows as on other platforms.
+	* doc/posix-functions/iswprint.texi: Mention the mingw bug.
+
 2023-07-26  Bruno Haible  <bruno@clisp.org>
 
 	iswblank tests: Add more tests.
diff --git a/doc/posix-functions/iswprint.texi b/doc/posix-functions/iswprint.texi
index 90ce3057e0..728baa0b7b 100644
--- a/doc/posix-functions/iswprint.texi
+++ b/doc/posix-functions/iswprint.texi
@@ -15,6 +15,10 @@ Minix 3.1.8.
 This function cannot be called from plain inline or extern inline functions
 on some platforms:
 OS X 10.8.
+@item
+This function returns true for the tab (@code{'\t'}) character
+on some platforms:
+mingw.
 @end itemize
 
 Portability problems not fixed by Gnulib:
diff --git a/lib/wctype.in.h b/lib/wctype.in.h
index 926a78d621..a8649b83a1 100644
--- a/lib/wctype.in.h
+++ b/lib/wctype.in.h
@@ -132,7 +132,8 @@ typedef unsigned int rpl_wint_t;
 /* FreeBSD 4.4 to 4.11 has <wctype.h> but lacks the functions.
    Linux libc5 has <wctype.h> and the functions but they are broken.
    mingw and MSVC have <wctype.h> and the functions but they take a wchar_t
-   as argument, not an rpl_wint_t.
+   as argument, not an rpl_wint_t.  Additionally, the mingw iswprint function
+   is broken.
    Assume all 11 functions (all isw* except iswblank) are implemented the
    same way, or not at all.  */
 # if ! @HAVE_ISWCNTRL@ || @REPLACE_ISWCNTRL@
@@ -184,7 +185,11 @@ rpl_iswlower (wint_t wc)
 _GL_WCTYPE_INLINE int
 rpl_iswprint (wint_t wc)
 {
+#   ifdef __MINGW32__
+  return ((wchar_t) wc == wc ? wc == ' ' || iswgraph ((wchar_t) wc) : 0);
+#   else
   return ((wchar_t) wc == wc ? iswprint ((wchar_t) wc) : 0);
+#   endif
 }
 
 _GL_WCTYPE_INLINE int
diff --git a/tests/test-c32isprint.c b/tests/test-c32isprint.c
index b7617ee105..18ccf55677 100644
--- a/tests/test-c32isprint.c
+++ b/tests/test-c32isprint.c
@@ -68,9 +68,7 @@ main (int argc, char *argv[])
     for (c = 0; c < 0x100; c++)
       switch (c)
         {
-        #if !(defined _WIN32 && !defined __CYGWIN__)
         case '\t': case '\v': case '\f':
-        #endif
         case ' ': case '!': case '"': case '#': case '%':
         case '&': case '\'': case '(': case ')': case '*':
         case '+': case ',': case '-': case '.': case '/':
diff --git a/tests/test-wctype-h.c b/tests/test-wctype-h.c
index ceee02bc7f..af02fe7eb6 100644
--- a/tests/test-wctype-h.c
+++ b/tests/test-wctype-h.c
@@ -62,6 +62,11 @@ main (void)
   ASSERT (!iswupper (e));
   ASSERT (!iswxdigit (e));
 
+  /* Sanity check for the iswprint function.  */
+  ASSERT (iswprint (L' '));
+  ASSERT (!iswprint (L'\t'));
+  ASSERT (!iswprint (L'\n'));
+
   /* Check that the tow* functions exist as functions or as macros.  */
   (void) towlower (0);
   (void) towupper (0);
-- 
2.39.5