1# iswdigit.m4 serial 1
2dnl Copyright (C) 2020 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7AC_DEFUN([gl_FUNC_ISWDIGIT],
8[
9  AC_REQUIRE([gl_WCTYPE_H_DEFAULTS])
10  AC_REQUIRE([gl_WCTYPE_H])
11  AC_REQUIRE([gt_LOCALE_FR])
12  AC_REQUIRE([gt_LOCALE_JA])
13  AC_REQUIRE([gt_LOCALE_FR_UTF8])
14  AC_REQUIRE([gt_LOCALE_ZH_CN])
15
16  if test $HAVE_ISWCNTRL = 0 || test $REPLACE_ISWCNTRL = 1; then
17    dnl <wctype.h> redefines iswdigit already.
18    REPLACE_ISWDIGIT="$REPLACE_ISWCNTRL"
19  else
20    AC_CACHE_CHECK([whether iswdigit is ISO C compliant],
21      [gl_cv_func_iswdigit_works],
22      [
23       dnl Initial guess, used when cross-compiling or when no suitable locale
24       dnl is present.
25changequote(,)dnl
26       case "$host_os" in
27         # Guess no on FreeBSD, NetBSD, Solaris, native Windows.
28         freebsd* | dragonfly* | netbsd* | solaris* | mingw*)
29           gl_cv_func_iswdigit_works="guessing no" ;;
30         # Guess yes otherwise.
31         *) gl_cv_func_iswdigit_works="guessing yes" ;;
32       esac
33changequote([,])dnl
34       if test $LOCALE_FR != none || test $LOCALE_JA != none || test $LOCALE_FR_UTF8 != none || test $LOCALE_ZH_CN != none; then
35         AC_RUN_IFELSE(
36           [AC_LANG_SOURCE([[
37#include <locale.h>
38#include <stdlib.h>
39#include <string.h>
40/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
41   <wchar.h>.
42   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
43   included before <wchar.h>.  */
44#include <stddef.h>
45#include <stdio.h>
46#include <time.h>
47#include <wchar.h>
48#include <wctype.h>
49
50/* Returns the value of iswdigit for the multibyte character s[0..n-1].  */
51static int
52for_character (const char *s, size_t n)
53{
54  mbstate_t state;
55  wchar_t wc;
56  size_t ret;
57
58  memset (&state, '\0', sizeof (mbstate_t));
59  wc = (wchar_t) 0xBADFACE;
60  ret = mbrtowc (&wc, s, n, &state);
61  if (ret != n)
62    abort ();
63
64  return iswdigit (wc);
65}
66
67int
68main (int argc, char *argv[])
69{
70  int is;
71  int result = 0;
72
73  if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
74    {
75      /* This fails on mingw, MSVC 14.  */
76      /* U+00B2 SUPERSCRIPT TWO */
77      is = for_character ("\262", 1);
78      if (!(is == 0))
79        result |= 1;
80    }
81  if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
82    {
83      /* This fails on NetBSD 8.0.  */
84      /* U+FF11 FULLWIDTH DIGIT ONE */
85      is = for_character ("\243\261", 2);
86      if (!(is == 0))
87        result |= 2;
88    }
89  if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
90    {
91      /* This fails on FreeBSD 12, NetBSD 8.0, MSVC 14.  */
92      /* U+0663 ARABIC-INDIC DIGIT THREE */
93      is = for_character ("\331\243", 2);
94      if (!(is == 0))
95        result |= 4;
96      /* This fails on FreeBSD 12, NetBSD 8.0, MSVC 14.  */
97      /* U+FF11 FULLWIDTH DIGIT ONE */
98      is = for_character ("\357\274\221", 3);
99      if (!(is == 0))
100        result |= 8;
101    }
102  if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
103    {
104      /* This fails on NetBSD 8.0, Solaris 10, Solaris 11.4.  */
105      /* U+FF11 FULLWIDTH DIGIT ONE */
106      is = for_character ("\243\261", 2);
107      if (!(is == 0))
108        result |= 16;
109    }
110  return result;
111}]])],
112           [gl_cv_func_iswdigit_works=yes],
113           [gl_cv_func_iswdigit_works=no],
114           [:])
115       fi
116      ])
117    case "$gl_cv_func_iswdigit_works" in
118      *yes) ;;
119      *) REPLACE_ISWDIGIT=1 ;;
120    esac
121  fi
122])
123