>From d6f8671505956401691e3c35d19499470f582a88 Mon Sep 17 00:00:00 2001
From: Bruno Haible
Date: Thu, 9 Jan 2020 02:04:07 +0100
Subject: [PATCH 4/4] c32tob: Make consistent with mbrtoc32.
* lib/c32tob.c: Include , , .
(c32tob): If the char32_t encoding and the wchar_t encoding may differ,
use c32rtomb, not wctob.
* modules/c32tob (Files): Add m4/mbrtoc32.m4.
(Depends-on): Add c32rtomb.
(configure.ac): Require gl_MBRTOC32_SANITYCHECK.
---
ChangeLog | 10 ++++++++++
lib/c32tob.c | 19 ++++++++++++++++++-
modules/c32tob | 3 +++
3 files changed, 31 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index c303d41..9c3f603 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
2020-01-08 Bruno Haible
+ c32tob: Make consistent with mbrtoc32.
+ * lib/c32tob.c: Include , , .
+ (c32tob): If the char32_t encoding and the wchar_t encoding may differ,
+ use c32rtomb, not wctob.
+ * modules/c32tob (Files): Add m4/mbrtoc32.m4.
+ (Depends-on): Add c32rtomb.
+ (configure.ac): Require gl_MBRTOC32_SANITYCHECK.
+
+2020-01-08 Bruno Haible
+
c32rtomb: Add tests.
* tests/test-c32rtomb.c: New file, based on tests/test-wcrtomb.c.
* tests/test-c32rtomb.sh: New file, based on tests/test-wcrtomb.sh.
diff --git a/lib/c32tob.c b/lib/c32tob.c
index 4da438f..55f61c7 100644
--- a/lib/c32tob.c
+++ b/lib/c32tob.c
@@ -21,10 +21,27 @@
/* Specification. */
#include
+#include
+#include
+#include
+
int
c32tob (wint_t wc)
{
-#if _GL_LARGE_CHAR32_T
+#if HAVE_WORKING_MBRTOC32 && !defined __GLIBC__
+ /* The char32_t encoding of a multibyte character may be different than its
+ wchar_t encoding. */
+ if (wc != WEOF)
+ {
+ mbstate_t state;
+ char buf[8];
+
+ memset (&state, '\0', sizeof (mbstate_t));
+ if (c32rtomb (buf, wc, &state) == 1)
+ return (unsigned char) buf[0];
+ }
+ return EOF;
+#elif _GL_LARGE_CHAR32_T
/* In all known encodings, unibyte characters correspond only to
characters in the BMP. */
if (wc != WEOF && (wchar_t) wc == wc)
diff --git a/modules/c32tob b/modules/c32tob
index 3ef42ba..42e18a9 100644
--- a/modules/c32tob
+++ b/modules/c32tob
@@ -3,12 +3,15 @@ c32tob() function: convert 32-bit wide character to unibyte character.
Files:
lib/c32tob.c
+m4/mbrtoc32.m4
Depends-on:
uchar
+c32rtomb
wctob
configure.ac:
+AC_REQUIRE([gl_MBRTOC32_SANITYCHECK])
gl_UCHAR_MODULE_INDICATOR([c32tob])
Makefile.am:
--
2.7.4