POSIX provides a way to the libc implementor to add non-ASCII characters to the
"digit" character class. However, ISO C 99 section 7.25.2.1.5 takes that
freedom away. The iswdigit() function is not ISO C compliant on a number of
platforms. This series of patches provides the workaround.


2020-01-25  Bruno Haible  <br...@clisp.org>

        mbchar, wctype: Use the corrected iswdigit function.
        * modules/mbchar (Depends-on): Add iswdigit.
        * modules/wctype (Depends-on): Likewise.

        iswdigit: Add tests.
        * tests/test-iswdigit.c: New file.
        * tests/test-iswdigit.sh: New file.
        * modules/iswdigit-tests: New file.

        iswdigit: New module.
        * m4/iswdigit.m4: New file.
        * lib/wctype.in.h (iswdigit): Potentially override.
        (iswdigit, rpl_iswdigit): Test REPLACE_ISWDIGIT, not REPLACE_ISWCNTRL.
        * lib/iswdigit.c: New file.
        * m4/wctype_h.m4 (gl_WCTYPE_H_DEFAULTS): Initialize GNULIB_ISWDIGIT,
        REPLACE_ISWDIGIT.
        * modules/wctype-h (Makefile.am): Substitute GNULIB_ISWDIGIT,
        REPLACE_ISWDIGIT.
        * modules/iswdigit: New file.
        * doc/posix-functions/iswdigit.texi: Mention the portability problem.

>From 36a9ca3f924853f22af5ac5228c35ca81bddf6cf Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sat, 25 Jan 2020 16:03:15 +0100
Subject: [PATCH 1/3] iswdigit: New module.

* m4/iswdigit.m4: New file.
* lib/wctype.in.h (iswdigit): Potentially override.
(iswdigit, rpl_iswdigit): Test REPLACE_ISWDIGIT, not REPLACE_ISWCNTRL.
* lib/iswdigit.c: New file.
* m4/wctype_h.m4 (gl_WCTYPE_H_DEFAULTS): Initialize GNULIB_ISWDIGIT,
REPLACE_ISWDIGIT.
* modules/wctype-h (Makefile.am): Substitute GNULIB_ISWDIGIT,
REPLACE_ISWDIGIT.
* modules/iswdigit: New file.
* doc/posix-functions/iswdigit.texi: Mention the portability problem.
---
 ChangeLog                         |  14 +++++
 doc/posix-functions/iswdigit.texi |   3 +
 lib/iswdigit.c                    |  26 ++++++++
 lib/wctype.in.h                   |  79 ++++++++++++++++++------
 m4/iswdigit.m4                    | 122 ++++++++++++++++++++++++++++++++++++++
 m4/wctype_h.m4                    |   4 +-
 modules/iswdigit                  |  31 ++++++++++
 modules/wctype-h                  |   2 +
 8 files changed, 262 insertions(+), 19 deletions(-)
 create mode 100644 lib/iswdigit.c
 create mode 100644 m4/iswdigit.m4
 create mode 100644 modules/iswdigit

diff --git a/ChangeLog b/ChangeLog
index 88285ee..75bf56e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,19 @@
 2020-01-25  Bruno Haible  <br...@clisp.org>
 
+	iswdigit: New module.
+	* m4/iswdigit.m4: New file.
+	* lib/wctype.in.h (iswdigit): Potentially override.
+	(iswdigit, rpl_iswdigit): Test REPLACE_ISWDIGIT, not REPLACE_ISWCNTRL.
+	* lib/iswdigit.c: New file.
+	* m4/wctype_h.m4 (gl_WCTYPE_H_DEFAULTS): Initialize GNULIB_ISWDIGIT,
+	REPLACE_ISWDIGIT.
+	* modules/wctype-h (Makefile.am): Substitute GNULIB_ISWDIGIT,
+	REPLACE_ISWDIGIT.
+	* modules/iswdigit: New file.
+	* doc/posix-functions/iswdigit.texi: Mention the portability problem.
+
+2020-01-25  Bruno Haible  <br...@clisp.org>
+
 	hard-locale tests: Make it easy to reuse the musl test.
 	* m4/musl.m4: New file, extracted from modules/hard-locale-tests.
 	* modules/hard-locale-tests (Files): Add it.
diff --git a/doc/posix-functions/iswdigit.texi b/doc/posix-functions/iswdigit.texi
index c4b80c2..3bf89e2 100644
--- a/doc/posix-functions/iswdigit.texi
+++ b/doc/posix-functions/iswdigit.texi
@@ -15,6 +15,9 @@ 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 is not ISO C 99 compliant on some platforms:
+FreeBSD 12, NetBSD 8.0, Solaris 11.4, mingw, MSVC 14.
 @end itemize
 
 Portability problems not fixed by Gnulib:
diff --git a/lib/iswdigit.c b/lib/iswdigit.c
new file mode 100644
index 0000000..4fd27e3
--- /dev/null
+++ b/lib/iswdigit.c
@@ -0,0 +1,26 @@
+/* Test wide character for being a digit.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License along
+   with this program; if not, see <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <wctype.h>
+
+int
+iswdigit (wint_t wc)
+{
+  return wc >= '0' && wc <= '9';
+}
diff --git a/lib/wctype.in.h b/lib/wctype.in.h
index 7e7726f..d9a4379 100644
--- a/lib/wctype.in.h
+++ b/lib/wctype.in.h
@@ -348,7 +348,7 @@ iswcntrl
 }
 
 _GL_WCTYPE_INLINE int
-#   if @REPLACE_ISWCNTRL@
+#   if @REPLACE_ISWDIGIT@
 rpl_iswdigit
 #   else
 iswdigit
@@ -463,16 +463,29 @@ towupper
 
 #  endif
 
-# elif @GNULIB_ISWBLANK@ && (! @HAVE_ISWBLANK@ || @REPLACE_ISWBLANK@)
-/* Only the iswblank function is missing.  */
+# else
+/* Only some of the functions are missing or broken.  */
 
-#  if @REPLACE_ISWBLANK@
-#   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
-#    define iswblank rpl_iswblank
-#   endif
+#  if @GNULIB_ISWBLANK@ && (! @HAVE_ISWBLANK@ || @REPLACE_ISWBLANK@)
+/* Only the iswblank function is missing.  */
+#   if @REPLACE_ISWBLANK@
+#    if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#     define iswblank rpl_iswblank
+#    endif
 _GL_FUNCDECL_RPL (iswblank, int, (wint_t wc));
-#  else
+#   else
 _GL_FUNCDECL_SYS (iswblank, int, (wint_t wc));
+#   endif
+#  endif
+
+#  if @GNULIB_ISWDIGIT@
+#   if @REPLACE_ISWDIGIT@
+#    if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#     undef iswdigit
+#     define iswdigit rpl_iswdigit
+#    endif
+_GL_FUNCDECL_RPL (iswdigit, int, (wint_t wc));
+#   endif
 #  endif
 
 # endif
@@ -517,27 +530,57 @@ rpl_towupper (wint_t wc)
 
 #if @REPLACE_ISWCNTRL@
 _GL_CXXALIAS_RPL (iswalnum, int, (wint_t wc));
-_GL_CXXALIAS_RPL (iswalpha, int, (wint_t wc));
-_GL_CXXALIAS_RPL (iswcntrl, int, (wint_t wc));
-_GL_CXXALIAS_RPL (iswdigit, int, (wint_t wc));
-_GL_CXXALIAS_RPL (iswgraph, int, (wint_t wc));
-_GL_CXXALIAS_RPL (iswlower, int, (wint_t wc));
-_GL_CXXALIAS_RPL (iswprint, int, (wint_t wc));
-_GL_CXXALIAS_RPL (iswpunct, int, (wint_t wc));
-_GL_CXXALIAS_RPL (iswspace, int, (wint_t wc));
-_GL_CXXALIAS_RPL (iswupper, int, (wint_t wc));
-_GL_CXXALIAS_RPL (iswxdigit, int, (wint_t wc));
 #else
 _GL_CXXALIAS_SYS (iswalnum, int, (wint_t wc));
+#endif
+#if @REPLACE_ISWCNTRL@
+_GL_CXXALIAS_RPL (iswalpha, int, (wint_t wc));
+#else
 _GL_CXXALIAS_SYS (iswalpha, int, (wint_t wc));
+#endif
+#if @REPLACE_ISWCNTRL@
+_GL_CXXALIAS_RPL (iswcntrl, int, (wint_t wc));
+#else
 _GL_CXXALIAS_SYS (iswcntrl, int, (wint_t wc));
+#endif
+#if @REPLACE_ISWDIGIT@
+_GL_CXXALIAS_RPL (iswdigit, int, (wint_t wc));
+#else
 _GL_CXXALIAS_SYS (iswdigit, int, (wint_t wc));
+#endif
+#if @REPLACE_ISWCNTRL@
+_GL_CXXALIAS_RPL (iswgraph, int, (wint_t wc));
+#else
 _GL_CXXALIAS_SYS (iswgraph, int, (wint_t wc));
+#endif
+#if @REPLACE_ISWCNTRL@
+_GL_CXXALIAS_RPL (iswlower, int, (wint_t wc));
+#else
 _GL_CXXALIAS_SYS (iswlower, int, (wint_t wc));
+#endif
+#if @REPLACE_ISWCNTRL@
+_GL_CXXALIAS_RPL (iswprint, int, (wint_t wc));
+#else
 _GL_CXXALIAS_SYS (iswprint, int, (wint_t wc));
+#endif
+#if @REPLACE_ISWCNTRL@
+_GL_CXXALIAS_RPL (iswpunct, int, (wint_t wc));
+#else
 _GL_CXXALIAS_SYS (iswpunct, int, (wint_t wc));
+#endif
+#if @REPLACE_ISWCNTRL@
+_GL_CXXALIAS_RPL (iswspace, int, (wint_t wc));
+#else
 _GL_CXXALIAS_SYS (iswspace, int, (wint_t wc));
+#endif
+#if @REPLACE_ISWCNTRL@
+_GL_CXXALIAS_RPL (iswupper, int, (wint_t wc));
+#else
 _GL_CXXALIAS_SYS (iswupper, int, (wint_t wc));
+#endif
+#if @REPLACE_ISWCNTRL@
+_GL_CXXALIAS_RPL (iswxdigit, int, (wint_t wc));
+#else
 _GL_CXXALIAS_SYS (iswxdigit, int, (wint_t wc));
 #endif
 #if __GLIBC__ >= 2
diff --git a/m4/iswdigit.m4 b/m4/iswdigit.m4
new file mode 100644
index 0000000..5f15c39
--- /dev/null
+++ b/m4/iswdigit.m4
@@ -0,0 +1,122 @@
+# iswdigit.m4 serial 1
+dnl Copyright (C) 2020 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_ISWDIGIT],
+[
+  AC_REQUIRE([gl_WCTYPE_H_DEFAULTS])
+  AC_REQUIRE([gl_WCTYPE_H])
+  AC_REQUIRE([gt_LOCALE_FR])
+  AC_REQUIRE([gt_LOCALE_JA])
+  AC_REQUIRE([gt_LOCALE_FR_UTF8])
+  AC_REQUIRE([gt_LOCALE_ZH_CN])
+
+  if test $HAVE_ISWCNTRL = 0 || test $REPLACE_ISWCNTRL = 1; then
+    dnl <wctype.h> redefines iswdigit already.
+    REPLACE_ISWDIGIT="$REPLACE_ISWCNTRL"
+  else
+    AC_CACHE_CHECK([whether iswdigit is ISO C compliant],
+      [gl_cv_func_iswdigit_works],
+      [
+       dnl Initial guess, used when cross-compiling or when no suitable locale
+       dnl is present.
+changequote(,)dnl
+       case "$host_os" in
+         # Guess no on FreeBSD, NetBSD, Solaris, native Windows.
+         freebsd* | dragonfly* | netbsd* | solaris* | mingw*)
+           gl_cv_func_iswdigit_works="guessing no" ;;
+         # Guess yes otherwise.
+         *) gl_cv_func_iswdigit_works="guessing yes" ;;
+       esac
+changequote([,])dnl
+       if test $LOCALE_FR != none || test $LOCALE_JA != none || test $LOCALE_FR_UTF8 != none || test $LOCALE_ZH_CN != none; then
+         AC_RUN_IFELSE(
+           [AC_LANG_SOURCE([[
+#include <locale.h>
+#include <stdlib.h>
+#include <string.h>
+/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
+   <wchar.h>.
+   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
+   included before <wchar.h>.  */
+#include <stddef.h>
+#include <stdio.h>
+#include <time.h>
+#include <wchar.h>
+#include <wctype.h>
+
+/* Returns the value of iswdigit for the multibyte character s[0..n-1].  */
+static int
+for_character (const char *s, size_t n)
+{
+  mbstate_t state;
+  wchar_t wc;
+  size_t ret;
+
+  memset (&state, '\0', sizeof (mbstate_t));
+  wc = (wchar_t) 0xBADFACE;
+  ret = mbrtowc (&wc, s, n, &state);
+  if (ret != n)
+    abort ();
+
+  return iswdigit (wc);
+}
+
+int
+main (int argc, char *argv[])
+{
+  int is;
+  int result = 0;
+
+  if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
+    {
+      /* This fails on mingw, MSVC 14.  */
+      /* U+00B2 SUPERSCRIPT TWO */
+      is = for_character ("\262", 1);
+      if (!(is == 0))
+        result |= 1;
+    }
+  if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
+    {
+      /* This fails on NetBSD 8.0.  */
+      /* U+FF11 FULLWIDTH DIGIT ONE */
+      is = for_character ("\243\261", 2);
+      if (!(is == 0))
+        result |= 2;
+    }
+  if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
+    {
+      /* This fails on FreeBSD 12, NetBSD 8.0, MSVC 14.  */
+      /* U+0663 ARABIC-INDIC DIGIT THREE */
+      is = for_character ("\331\243", 2);
+      if (!(is == 0))
+        result |= 4;
+      /* This fails on FreeBSD 12, NetBSD 8.0, MSVC 14.  */
+      /* U+FF11 FULLWIDTH DIGIT ONE */
+      is = for_character ("\357\274\221", 3);
+      if (!(is == 0))
+        result |= 8;
+    }
+  if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
+    {
+      /* This fails on NetBSD 8.0, Solaris 10, Solaris 11.4.  */
+      /* U+FF11 FULLWIDTH DIGIT ONE */
+      is = for_character ("\243\261", 2);
+      if (!(is == 0))
+        result |= 16;
+    }
+  return result;
+}]])],
+           [gl_cv_func_iswdigit_works=yes],
+           [gl_cv_func_iswdigit_works=no],
+           [:])
+       fi
+      ])
+    case "$gl_cv_func_iswdigit_works" in
+      *yes) ;;
+      *) REPLACE_ISWDIGIT=1 ;;
+    esac
+  fi
+])
diff --git a/m4/wctype_h.m4 b/m4/wctype_h.m4
index dc854e6..24bad38 100644
--- a/m4/wctype_h.m4
+++ b/m4/wctype_h.m4
@@ -1,4 +1,4 @@
-# wctype_h.m4 serial 22
+# wctype_h.m4 serial 23
 
 dnl A placeholder for ISO C99 <wctype.h>, for platforms that lack it.
 
@@ -204,6 +204,7 @@ AC_DEFUN([gl_WCTYPE_MODULE_INDICATOR],
 AC_DEFUN([gl_WCTYPE_H_DEFAULTS],
 [
   GNULIB_ISWBLANK=0;    AC_SUBST([GNULIB_ISWBLANK])
+  GNULIB_ISWDIGIT=0;    AC_SUBST([GNULIB_ISWDIGIT])
   GNULIB_WCTYPE=0;      AC_SUBST([GNULIB_WCTYPE])
   GNULIB_ISWCTYPE=0;    AC_SUBST([GNULIB_ISWCTYPE])
   GNULIB_WCTRANS=0;     AC_SUBST([GNULIB_WCTRANS])
@@ -213,4 +214,5 @@ AC_DEFUN([gl_WCTYPE_H_DEFAULTS],
   HAVE_WCTYPE_T=1;      AC_SUBST([HAVE_WCTYPE_T])
   HAVE_WCTRANS_T=1;     AC_SUBST([HAVE_WCTRANS_T])
   REPLACE_ISWBLANK=0;   AC_SUBST([REPLACE_ISWBLANK])
+  REPLACE_ISWDIGIT=0;   AC_SUBST([REPLACE_ISWDIGIT])
 ])
diff --git a/modules/iswdigit b/modules/iswdigit
new file mode 100644
index 0000000..2098870
--- /dev/null
+++ b/modules/iswdigit
@@ -0,0 +1,31 @@
+Description:
+iswdigit() function: test wide character for being a digit.
+
+Files:
+lib/iswdigit.c
+m4/iswdigit.m4
+
+Depends-on:
+wctype-h
+
+configure.ac:
+gl_FUNC_ISWDIGIT
+if test $HAVE_ISWCNTRL = 0 || test $REPLACE_ISWCNTRL = 1; then
+  :
+else
+  if test $REPLACE_ISWDIGIT = 1; then
+    AC_LIBOBJ([iswdigit])
+  fi
+fi
+gl_WCTYPE_MODULE_INDICATOR([iswdigit])
+
+Makefile.am:
+
+Include:
+<wctype.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+Bruno Haible
diff --git a/modules/wctype-h b/modules/wctype-h
index 37bccce..58f4e51 100644
--- a/modules/wctype-h
+++ b/modules/wctype-h
@@ -34,6 +34,7 @@ wctype.h: wctype.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H
 	      -e 's/@''HAVE_CRTDEFS_H''@/$(HAVE_CRTDEFS_H)/g' \
 	      -e 's/@''GNULIB_OVERRIDES_WINT_T''@/$(GNULIB_OVERRIDES_WINT_T)/g' \
 	      -e 's/@''GNULIB_ISWBLANK''@/$(GNULIB_ISWBLANK)/g' \
+	      -e 's/@''GNULIB_ISWDIGIT''@/$(GNULIB_ISWDIGIT)/g' \
 	      -e 's/@''GNULIB_WCTYPE''@/$(GNULIB_WCTYPE)/g' \
 	      -e 's/@''GNULIB_ISWCTYPE''@/$(GNULIB_ISWCTYPE)/g' \
 	      -e 's/@''GNULIB_WCTRANS''@/$(GNULIB_WCTRANS)/g' \
@@ -44,6 +45,7 @@ wctype.h: wctype.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H
 	      -e 's/@''HAVE_WCTRANS_T''@/$(HAVE_WCTRANS_T)/g' \
 	      -e 's/@''HAVE_WINT_T''@/$(HAVE_WINT_T)/g' \
 	      -e 's/@''REPLACE_ISWBLANK''@/$(REPLACE_ISWBLANK)/g' \
+	      -e 's/@''REPLACE_ISWDIGIT''@/$(REPLACE_ISWDIGIT)/g' \
 	      -e 's/@''REPLACE_ISWCNTRL''@/$(REPLACE_ISWCNTRL)/g' \
 	      -e 's/@''REPLACE_TOWLOWER''@/$(REPLACE_TOWLOWER)/g' \
 	      -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
-- 
2.7.4

>From 081b919b9633518e855fc55a40672a287855f64f Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sat, 25 Jan 2020 16:05:55 +0100
Subject: [PATCH 2/3] iswdigit: Add tests.

* tests/test-iswdigit.c: New file.
* tests/test-iswdigit.sh: New file.
* modules/iswdigit-tests: New file.
---
 ChangeLog              |   5 ++
 modules/iswdigit-tests |  29 +++++++
 tests/test-iswdigit.c  | 232 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test-iswdigit.sh |  39 +++++++++
 4 files changed, 305 insertions(+)
 create mode 100644 modules/iswdigit-tests
 create mode 100644 tests/test-iswdigit.c
 create mode 100755 tests/test-iswdigit.sh

diff --git a/ChangeLog b/ChangeLog
index 75bf56e..6fa977d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2020-01-25  Bruno Haible  <br...@clisp.org>
 
+	iswdigit: Add tests.
+	* tests/test-iswdigit.c: New file.
+	* tests/test-iswdigit.sh: New file.
+	* modules/iswdigit-tests: New file.
+
 	iswdigit: New module.
 	* m4/iswdigit.m4: New file.
 	* lib/wctype.in.h (iswdigit): Potentially override.
diff --git a/modules/iswdigit-tests b/modules/iswdigit-tests
new file mode 100644
index 0000000..ca167eb
--- /dev/null
+++ b/modules/iswdigit-tests
@@ -0,0 +1,29 @@
+Files:
+tests/test-iswdigit.sh
+tests/test-iswdigit.c
+tests/signature.h
+tests/macros.h
+m4/locale-fr.m4
+m4/locale-ja.m4
+m4/locale-zh.m4
+m4/codeset.m4
+
+Depends-on:
+mbrtowc
+setlocale
+
+configure.ac:
+gt_LOCALE_FR
+gt_LOCALE_FR_UTF8
+gt_LOCALE_JA
+gt_LOCALE_ZH_CN
+
+Makefile.am:
+TESTS += test-iswdigit.sh
+TESTS_ENVIRONMENT += \
+  LOCALE_FR='@LOCALE_FR@' \
+  LOCALE_FR_UTF8='@LOCALE_FR_UTF8@' \
+  LOCALE_JA='@LOCALE_JA@' \
+  LOCALE_ZH_CN='@LOCALE_ZH_CN@'
+check_PROGRAMS += test-iswdigit
+test_iswdigit_LDADD = $(LDADD) $(LIB_SETLOCALE) $(LIB_MBRTOWC)
diff --git a/tests/test-iswdigit.c b/tests/test-iswdigit.c
new file mode 100644
index 0000000..5bfe2a4
--- /dev/null
+++ b/tests/test-iswdigit.c
@@ -0,0 +1,232 @@
+/* Test of iswdigit() function.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include <wctype.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (iswdigit, int, (wint_t));
+
+#include <locale.h>
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+
+#include "macros.h"
+
+/* Returns the value of iswdigit for the multibyte character s[0..n-1].  */
+static int
+for_character (const char *s, size_t n)
+{
+  mbstate_t state;
+  wchar_t wc;
+  size_t ret;
+
+  memset (&state, '\0', sizeof (mbstate_t));
+  wc = (wchar_t) 0xBADFACE;
+  ret = mbrtowc (&wc, s, n, &state);
+  ASSERT (ret == n);
+
+  return iswdigit (wc);
+}
+
+int
+main (int argc, char *argv[])
+{
+  int is;
+  char buf[4];
+
+  /* configure should already have checked that the locale is supported.  */
+  if (setlocale (LC_ALL, "") == NULL)
+    return 1;
+
+  /* Test WEOF.  */
+  is = iswdigit (WEOF);
+  ASSERT (is == 0);
+
+  /* Test single-byte characters.
+     ISO C 99 sections 7.25.2.1.5 and 5.2.1 specify that the decimal digits
+     include only the ASCII 0 ... 9 characters.  */
+  {
+    int c;
+
+    for (c = 0; c < 0x100; c++)
+      switch (c)
+        {
+        case '\t': case '\v': case '\f':
+        case ' ': case '!': case '"': case '#': case '%':
+        case '&': case '\'': case '(': case ')': case '*':
+        case '+': case ',': case '-': case '.': case '/':
+        case '0': case '1': case '2': case '3': case '4':
+        case '5': case '6': case '7': case '8': case '9':
+        case ':': case ';': case '<': case '=': case '>':
+        case '?':
+        case 'A': case 'B': case 'C': case 'D': case 'E':
+        case 'F': case 'G': case 'H': case 'I': case 'J':
+        case 'K': case 'L': case 'M': case 'N': case 'O':
+        case 'P': case 'Q': case 'R': case 'S': case 'T':
+        case 'U': case 'V': case 'W': case 'X': case 'Y':
+        case 'Z':
+        case '[': case '\\': case ']': case '^': case '_':
+        case 'a': case 'b': case 'c': case 'd': case 'e':
+        case 'f': case 'g': case 'h': case 'i': case 'j':
+        case 'k': case 'l': case 'm': case 'n': case 'o':
+        case 'p': case 'q': case 'r': case 's': case 't':
+        case 'u': case 'v': case 'w': case 'x': case 'y':
+        case 'z': case '{': case '|': case '}': case '~':
+          /* c is in the ISO C "basic character set".  */
+          buf[0] = (unsigned char) c;
+          is = for_character (buf, 1);
+          switch (c)
+            {
+            case '0': case '1': case '2': case '3': case '4':
+            case '5': case '6': case '7': case '8': case '9':
+              ASSERT (is != 0);
+              break;
+            default:
+              ASSERT (is == 0);
+              break;
+            }
+          break;
+        }
+  }
+
+  if (argc > 1)
+    switch (argv[1][0])
+      {
+      case '0':
+        /* C locale; tested above.  */
+        return 0;
+
+      case '1':
+        /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
+        {
+          /* U+00B2 SUPERSCRIPT TWO */
+          is = for_character ("\262", 1);
+          ASSERT (is == 0);
+          /* U+00B3 SUPERSCRIPT THREE */
+          is = for_character ("\263", 1);
+          ASSERT (is == 0);
+          /* U+00B9 SUPERSCRIPT ONE */
+          is = for_character ("\271", 1);
+          ASSERT (is == 0);
+        }
+        return 0;
+
+      case '2':
+        /* Locale encoding is EUC-JP.  */
+        {
+          /* U+FF11 FULLWIDTH DIGIT ONE */
+          is = for_character ("\243\261", 2);
+          ASSERT (is == 0);
+        }
+        return 0;
+
+      case '3':
+        /* Locale encoding is UTF-8.  */
+        {
+          /* U+00B2 SUPERSCRIPT TWO */
+          is = for_character ("\302\262", 2);
+          ASSERT (is == 0);
+          /* U+00B3 SUPERSCRIPT THREE */
+          is = for_character ("\302\263", 2);
+          ASSERT (is == 0);
+          /* U+00B9 SUPERSCRIPT ONE */
+          is = for_character ("\302\271", 2);
+          ASSERT (is == 0);
+          /* U+0663 ARABIC-INDIC DIGIT THREE */
+          is = for_character ("\331\243", 2);
+          ASSERT (is == 0);
+          /* U+2070 SUPERSCRIPT ZERO */
+          is = for_character ("\342\201\260", 3);
+          ASSERT (is == 0);
+          /* U+2079 SUPERSCRIPT NINE */
+          is = for_character ("\342\201\271", 3);
+          ASSERT (is == 0);
+          /* U+FF11 FULLWIDTH DIGIT ONE */
+          is = for_character ("\357\274\221", 3);
+          ASSERT (is == 0);
+          /* U+1D7D1 MATHEMATICAL BOLD DIGIT THREE */
+          is = for_character ("\360\235\237\221", 4);
+          ASSERT (is == 0);
+          /* U+1D7DB MATHEMATICAL DOUBLE-STRUCK DIGIT THREE */
+          is = for_character ("\360\235\237\233", 4);
+          ASSERT (is == 0);
+          /* U+1D7E5 MATHEMATICAL SANS-SERIF DIGIT THREE */
+          is = for_character ("\360\235\237\245", 4);
+          ASSERT (is == 0);
+          /* U+1D7EF MATHEMATICAL SANS-SERIF BOLD DIGIT THREE */
+          is = for_character ("\360\235\237\257", 4);
+          ASSERT (is == 0);
+          /* U+1D7F9 MATHEMATICAL MONOSPACE DIGIT THREE */
+          is = for_character ("\360\235\237\271", 4);
+          ASSERT (is == 0);
+          /* U+E0033 TAG DIGIT THREE */
+          is = for_character ("\363\240\200\263", 4);
+          ASSERT (is == 0);
+        }
+        return 0;
+
+      case '4':
+        /* Locale encoding is GB18030.  */
+        {
+          /* U+00B2 SUPERSCRIPT TWO */
+          is = for_character ("\201\060\205\065", 4);
+          ASSERT (is == 0);
+          /* U+00B3 SUPERSCRIPT THREE */
+          is = for_character ("\201\060\205\066", 4);
+          ASSERT (is == 0);
+          /* U+00B9 SUPERSCRIPT ONE */
+          is = for_character ("\201\060\206\061", 4);
+          ASSERT (is == 0);
+          /* U+0663 ARABIC-INDIC DIGIT THREE */
+          is = for_character ("\201\061\211\071", 4);
+          ASSERT (is == 0);
+          /* U+2070 SUPERSCRIPT ZERO */
+          is = for_character ("\201\066\255\062", 4);
+          ASSERT (is == 0);
+          /* U+2079 SUPERSCRIPT NINE */
+          is = for_character ("\201\066\256\061", 4);
+          ASSERT (is == 0);
+          /* U+FF11 FULLWIDTH DIGIT ONE */
+          is = for_character ("\243\261", 2);
+          ASSERT (is == 0);
+          /* U+1D7D1 MATHEMATICAL BOLD DIGIT THREE */
+          is = for_character ("\224\063\353\071", 4);
+          ASSERT (is == 0);
+          /* U+1D7DB MATHEMATICAL DOUBLE-STRUCK DIGIT THREE */
+          is = for_character ("\224\063\354\071", 4);
+          ASSERT (is == 0);
+          /* U+1D7E5 MATHEMATICAL SANS-SERIF DIGIT THREE */
+          is = for_character ("\224\063\355\071", 4);
+          ASSERT (is == 0);
+          /* U+1D7EF MATHEMATICAL SANS-SERIF BOLD DIGIT THREE */
+          is = for_character ("\224\063\356\071", 4);
+          ASSERT (is == 0);
+          /* U+1D7F9 MATHEMATICAL MONOSPACE DIGIT THREE */
+          is = for_character ("\224\063\357\071", 4);
+          ASSERT (is == 0);
+          /* U+E0033 TAG DIGIT THREE */
+          is = for_character ("\323\066\232\071", 4);
+          ASSERT (is == 0);
+        }
+        return 0;
+
+      }
+
+  return 1;
+}
diff --git a/tests/test-iswdigit.sh b/tests/test-iswdigit.sh
new file mode 100755
index 0000000..1bde602
--- /dev/null
+++ b/tests/test-iswdigit.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+# Test in the POSIX locale.
+LC_ALL=C     ${CHECKER} ./test-iswdigit${EXEEXT} 0 || exit 1
+LC_ALL=POSIX ${CHECKER} ./test-iswdigit${EXEEXT} 0 || exit 1
+
+# Test in an ISO-8859-1 or ISO-8859-15 locale.
+: ${LOCALE_FR=fr_FR}
+if test $LOCALE_FR != none; then
+  LC_ALL=$LOCALE_FR \
+  ${CHECKER} ./test-iswdigit${EXEEXT} 1 \
+  || exit 1
+fi
+
+# Test whether a specific EUC-JP locale is installed.
+: ${LOCALE_JA=ja_JP}
+if test $LOCALE_JA != none; then
+  LC_ALL=$LOCALE_JA \
+  ${CHECKER} ./test-iswdigit${EXEEXT} 2 \
+  || exit 1
+fi
+
+# Test whether a specific UTF-8 locale is installed.
+: ${LOCALE_FR_UTF8=fr_FR.UTF-8}
+if test $LOCALE_FR_UTF8 != none; then
+  LC_ALL=$LOCALE_FR_UTF8 \
+  ${CHECKER} ./test-iswdigit${EXEEXT} 3 \
+  || exit 1
+fi
+
+# Test whether a specific GB18030 locale is installed.
+: ${LOCALE_ZH_CN=zh_CN.GB18030}
+if test $LOCALE_ZH_CN != none; then
+  LC_ALL=$LOCALE_ZH_CN \
+  ${CHECKER} ./test-iswdigit${EXEEXT} 4 \
+  || exit 1
+fi
+
+exit 0
-- 
2.7.4

>From aa1486ff90d0c01c8086990f163e3ed0a4f78e13 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sat, 25 Jan 2020 16:11:39 +0100
Subject: [PATCH 3/3] mbchar, wctype: Use the corrected iswdigit function.

* modules/mbchar (Depends-on): Add iswdigit.
* modules/wctype (Depends-on): Likewise.
---
 ChangeLog      | 4 ++++
 modules/mbchar | 1 +
 modules/wctype | 1 +
 3 files changed, 6 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 6fa977d..731e0ce 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2020-01-25  Bruno Haible  <br...@clisp.org>
 
+	mbchar, wctype: Use the corrected iswdigit function.
+	* modules/mbchar (Depends-on): Add iswdigit.
+	* modules/wctype (Depends-on): Likewise.
+
 	iswdigit: Add tests.
 	* tests/test-iswdigit.c: New file.
 	* tests/test-iswdigit.sh: New file.
diff --git a/modules/mbchar b/modules/mbchar
index 2f7e11e..b11e365 100644
--- a/modules/mbchar
+++ b/modules/mbchar
@@ -13,6 +13,7 @@ stdbool
 wchar
 wctype-h
 iswblank
+iswdigit
 wcwidth
 memcmp
 
diff --git a/modules/wctype b/modules/wctype
index 9ef78ea..2447c26 100644
--- a/modules/wctype
+++ b/modules/wctype
@@ -15,6 +15,7 @@ m4/wctype.m4
 Depends-on:
 wctype-h
 iswblank        [test $HAVE_WCTYPE = 0]
+iswdigit        [test $HAVE_WCTYPE = 0]
 
 configure.ac:
 gl_FUNC_WCTYPE
-- 
2.7.4

Reply via email to