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