1 /* Convert 32-bit wide character to multibyte character.
2 Copyright (C) 2020-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2020. */
27 #include "attribute.h" /* FALLTHROUGH */
28 #include "localcharset.h"
31 #if GL_CHAR32_T_IS_UNICODE
32 # include "lc-charset-unicode.h"
36 c32rtomb (char *s
, char32_t wc
, mbstate_t *ps
)
39 #if HAVE_WORKING_MBRTOC32 && HAVE_WORKING_C32RTOMB
41 # if C32RTOMB_RETVAL_BUG
43 /* We know the NUL wide character corresponds to the NUL character. */
47 return c32rtomb (s
, wc
, ps
);
49 #elif _GL_SMALL_WCHAR_T
52 return wcrtomb (NULL
, 0, ps
);
55 /* Special-case all encodings that may produce wide character values
57 const char *encoding
= locale_charset ();
58 if (STREQ_OPT (encoding
, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
60 /* Special-case the UTF-8 encoding. Assume that the wide-character
61 encoding in a UTF-8 locale is UCS-2 or, equivalently, UTF-16. */
64 s
[0] = (unsigned char) wc
;
73 else if (wc
< 0x10000)
75 if (wc
< 0xd800 || wc
>= 0xe000)
83 else if (wc
< 0x110000)
91 switch (count
) /* note: code falls through cases! */
93 case 4: s
[3] = 0x80 | (wc
& 0x3f); wc
= wc
>> 6; wc
|= 0x10000;
95 case 3: s
[2] = 0x80 | (wc
& 0x3f); wc
= wc
>> 6; wc
|= 0x800;
97 case 2: s
[1] = 0x80 | (wc
& 0x3f); wc
= wc
>> 6; wc
|= 0xc0;
98 /*case 1:*/ s
[0] = wc
;
105 if ((wchar_t) wc
== wc
)
106 return wcrtomb (s
, (wchar_t) wc
, ps
);
117 /* char32_t and wchar_t are equivalent. */
118 # if GL_CHAR32_T_IS_UNICODE && GL_CHAR32_T_VS_WCHAR_T_NEEDS_CONVERSION
121 wc
= unicode_to_locale_encoding (wc
);
129 return wcrtomb (s
, (wchar_t) wc
, ps
);