Debian Codegraph @

lib/c32to-impl.h

raw
1 /* Case mapping of a 32-bit wide character.
2 Copyright (C) 2020-2024 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 <[email protected]>, 2023. */
18
19 #include <wchar.h>
20 #include <wctype.h>
21
22 #if GNULIB_defined_mbstate_t
23 # include "localcharset.h"
24 # include "streq.h"
25 #endif
26
27 #if GL_CHAR32_T_IS_UNICODE
28 # include "lc-charset-unicode.h"
29 #endif
30
31 #include "unicase.h"
32
33 #if _GL_WCHAR_T_IS_UCS4 && !GNULIB_defined_mbstate_t
34 _GL_EXTERN_INLINE
35 #endif
36 wint_t
37 FUNC (wint_t wc)
38 {
39 /* The char32_t encoding of a multibyte character is defined by the way
40 mbrtoc32() is defined. */
41
42 #if GNULIB_defined_mbstate_t /* AIX, IRIX */
43 /* mbrtoc32() is defined on top of mbtowc() for the non-UTF-8 locales
44 and directly for the UTF-8 locales. */
45 if (wc != WEOF)
46 {
47 const char *encoding = locale_charset ();
48 if (STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
49 return UCS_FUNC (wc);
50 else
51 return WCHAR_FUNC (wc);
52 }
53 else
54 return wc;
55
56 #elif HAVE_WORKING_MBRTOC32 && HAVE_WORKING_C32RTOMB /* glibc, Android */
57 /* mbrtoc32() is essentially defined by the system libc. */
58
59 # if _GL_WCHAR_T_IS_UCS4
60 /* The char32_t encoding of a multibyte character is known to be the same as
61 the wchar_t encoding. */
62 return WCHAR_FUNC (wc);
63 # else
64 /* The char32_t encoding of a multibyte character is known to be UCS-4,
65 different from the wchar_t encoding. */
66 if (wc != WEOF)
67 return UCS_FUNC (wc);
68 else
69 return wc;
70 # endif
71
72 #elif _GL_SMALL_WCHAR_T /* Cygwin, mingw, MSVC */
73 /* The wchar_t encoding is UTF-16.
74 The char32_t encoding is UCS-4. */
75
76 # if defined _WIN32 && !defined __CYGWIN__
77 /* On native Windows, in the UTF-8 locale, towlower and towupper are
78 lacking (at least) the mappings for ISO-8859-1 characters, such as
79 0x00C9 <-> 0x00E9. Since it is expensive to test whether the locale
80 encoding is UTF-8, ignore the system's WCHAR_FUNC altogether. */
81 if (wc != WEOF)
82 return UCS_FUNC (wc);
83 else
84 return wc;
85 # else
86 if (wc == WEOF || wc == (wchar_t) wc)
87 /* wc is in the range for the tow* functions. */
88 return WCHAR_FUNC (wc);
89 else
90 return UCS_FUNC (wc);
91 # endif
92
93 #else /* macOS, FreeBSD, NetBSD, OpenBSD, HP-UX, Solaris, Minix, Android */
94 /* char32_t and wchar_t are equivalent. */
95 static_assert (sizeof (char32_t) == sizeof (wchar_t));
96
97 # if GL_CHAR32_T_IS_UNICODE && GL_CHAR32_T_VS_WCHAR_T_NEEDS_CONVERSION
98 return UCS_FUNC (wc);
99 # else
100 return WCHAR_FUNC (wc);
101 # endif
102 #endif
103 }