summaryrefslogtreecommitdiff
path: root/lib/btoc32.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2020-01-09 01:47:17 +0100
committerBruno Haible <bruno@clisp.org>2020-01-09 01:47:17 +0100
commit9be236d67f3d78235c5cbe4381c5dd7b3cddb179 (patch)
tree09bcc0427c1bd58585080336ebed1fe1bfde6898 /lib/btoc32.c
parent877b0c46019d34e722c94248edbfaf5bfbaa17ec (diff)
downloadgnulib-9be236d67f3d78235c5cbe4381c5dd7b3cddb179.tar.gz
mbrtoc32: Use the system's mbrtoc32 if it exists and basically works.
* m4/mbrtoc32.m4 (gl_MBRTOC32_SANITYCHECK): New macro. (gl_FUNC_MBRTOC32): Require it. Set REPLACE_MBRTOC32 if mbrtoc32 exists but is not working. * lib/mbrtoc32.c: Include hard-locale.h, <locale.h>. (mbrtoc32): If the char32_t encoding and the wchar_t encoding may differ, use the system's mbrtoc32, adding workarounds. * modules/mbrtoc32 (Depends-on): Add hard-locale. * doc/posix-functions/mbrtoc32.texi: Mention the Solaris and native Windows problem. * lib/btoc32.c: Include <stdio.h>, <string.h>. (btoc32): If the char32_t encoding and the wchar_t encoding may differ, use mbrtoc32, not btowc. * modules/btoc32 (Depends-on): Add mbrtoc32. * lib/mbsrtoc32s.c (mbsrtoc32s): If the char32_t encoding and the wchar_t encoding may differ, use mbrtoc32, not mbsrtowcs. * modules/mbsrtoc32s (Depends-on): Update conditions. (configure.ac): Compile mbsrtoc32s-state.c unconditionally. * lib/mbsnrtoc32s.c (mbsnrtoc32s): If the char32_t encoding and the wchar_t encoding may differ, use mbrtoc32, not mbsnrtowcs. * modules/mbsnrtoc32s (Depends-on): Update conditions. (configure.ac): Compile mbsrtoc32s-state.c unconditionally.
Diffstat (limited to 'lib/btoc32.c')
-rw-r--r--lib/btoc32.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/btoc32.c b/lib/btoc32.c
index 8b27875813..d8ce087ec6 100644
--- a/lib/btoc32.c
+++ b/lib/btoc32.c
@@ -21,10 +21,30 @@
/* Specification. */
#include <uchar.h>
+#include <stdio.h>
+#include <string.h>
+
wint_t
btoc32 (int c)
{
+#if HAVE_WORKING_MBRTOC32 && !defined __GLIBC__
+ /* The char32_t encoding of a multibyte character may be different than its
+ wchar_t encoding. */
+ if (c != EOF)
+ {
+ mbstate_t state;
+ char s[1];
+ char32_t wc;
+
+ memset (&state, '\0', sizeof (mbstate_t));
+ s[0] = (unsigned char) c;
+ if (mbrtoc32 (&wc, s, 1, &state) <= 1)
+ return wc;
+ }
+ return WEOF;
+#else
/* In all known locale encodings, unibyte characters correspond only to
characters in the BMP. */
return btowc (c);
+#endif
}