summaryrefslogtreecommitdiffstats
path: root/gl/c32is-impl.h
diff options
context:
space:
mode:
Diffstat (limited to 'gl/c32is-impl.h')
-rw-r--r--gl/c32is-impl.h105
1 files changed, 105 insertions, 0 deletions
diff --git a/gl/c32is-impl.h b/gl/c32is-impl.h
new file mode 100644
index 00000000..8366035d
--- /dev/null
+++ b/gl/c32is-impl.h
@@ -0,0 +1,105 @@
1/* Test whether a 32-bit wide character belongs to a specific character class.
2 Copyright (C) 2020-2025 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17/* Written by Bruno Haible <bruno@clisp.org>, 2020. */
18
19#include <wchar.h>
20#include <wctype.h>
21
22#ifdef __CYGWIN__
23# include <cygwin/version.h>
24#endif
25
26#if GNULIB_defined_mbstate_t
27# include "localcharset.h"
28# include "streq.h"
29#endif
30
31#if GL_CHAR32_T_IS_UNICODE
32# include "lc-charset-unicode.h"
33#endif
34
35#include "unictype.h"
36
37#if _GL_WCHAR_T_IS_UCS4 && !GNULIB_defined_mbstate_t
38_GL_EXTERN_INLINE
39#endif
40int
41FUNC (wint_t wc)
42{
43 /* The char32_t encoding of a multibyte character is defined by the way
44 mbrtoc32() is defined. */
45
46#if GNULIB_defined_mbstate_t /* AIX, IRIX */
47 /* mbrtoc32() is defined on top of mbtowc() for the non-UTF-8 locales
48 and directly for the UTF-8 locales. */
49 if (wc != WEOF)
50 {
51 const char *encoding = locale_charset ();
52 if (STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
53 return UCS_FUNC (wc);
54 else
55 return WCHAR_FUNC (wc);
56 }
57 else
58 return 0;
59
60#elif HAVE_WORKING_MBRTOC32 && HAVE_WORKING_C32RTOMB /* glibc, Android */
61 /* mbrtoc32() is essentially defined by the system libc. */
62
63# if _GL_WCHAR_T_IS_UCS4
64 /* The char32_t encoding of a multibyte character is known to be the same as
65 the wchar_t encoding. */
66 return WCHAR_FUNC (wc);
67# else
68 /* The char32_t encoding of a multibyte character is known to be UCS-4,
69 different from the wchar_t encoding. */
70 if (wc != WEOF)
71 return UCS_FUNC (wc);
72 else
73 return 0;
74# endif
75
76#elif _GL_SMALL_WCHAR_T /* Cygwin, mingw, MSVC */
77 /* The wchar_t encoding is UTF-16.
78 The char32_t encoding is UCS-4. */
79
80# if defined __CYGWIN__ && CYGWIN_VERSION_DLL_MAJOR >= 1007
81 /* As an extension to POSIX, the iswalnum() function of Cygwin >= 1.7
82 supports also wc arguments outside the Unicode BMP, that is, outside
83 the 'wchar_t' range. See
84 <https://lists.gnu.org/archive/html/bug-gnulib/2011-02/msg00019.html>
85 = <https://cygwin.com/ml/cygwin/2011-02/msg00044.html>. */
86 return WCHAR_FUNC (wc);
87# else
88 if (wc == WEOF || wc == (wchar_t) wc)
89 /* wc is in the range for the isw* functions. */
90 return WCHAR_FUNC (wc);
91 else
92 return UCS_FUNC (wc);
93# endif
94
95#else /* macOS, FreeBSD, NetBSD, OpenBSD, HP-UX, Solaris, Minix, Android */
96 /* char32_t and wchar_t are equivalent. */
97 static_assert (sizeof (char32_t) == sizeof (wchar_t));
98
99# if GL_CHAR32_T_IS_UNICODE && GL_CHAR32_T_VS_WCHAR_T_NEEDS_CONVERSION
100 return UCS_FUNC (wc);
101# else
102 return WCHAR_FUNC (wc);
103# endif
104#endif
105}