c32tob: Make consistent with mbrtoc32.
authorBruno Haible <bruno@clisp.org>
Thu, 9 Jan 2020 01:04:07 +0000 (02:04 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 9 Jan 2020 01:04:07 +0000 (02:04 +0100)
* lib/c32tob.c: Include <stdio.h>, <string.h>, <wchar.h>.
(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
lib/c32tob.c
modules/c32tob

index c303d41edd77ede6dad04f4defe53e96d2790fa2..9c3f6032b50308303e62462451d40dd76abd7a3b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2020-01-08  Bruno Haible  <bruno@clisp.org>
+
+       c32tob: Make consistent with mbrtoc32.
+       * lib/c32tob.c: Include <stdio.h>, <string.h>, <wchar.h>.
+       (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  <bruno@clisp.org>
 
        c32rtomb: Add tests.
index 4da438f01a5911d8fe032401328b1fff12ca4d9c..55f61c7c6cd9db9f14225db0996bfffc5a1ccdcb 100644 (file)
 /* Specification.  */
 #include <uchar.h>
 
+#include <stdio.h>
+#include <string.h>
+#include <wchar.h>
+
 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)
index 3ef42ba613d695941ea0ee3010cb843f79596e88..42e18a985b0e68c3749e35d32d9e0db57b3b2469 100644 (file)
@@ -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: