Paul Eggert wrote:
> Yes, the idea is that if mbrtoc32 succeeds in the C locale, it should yield a
> char32_t that can be fed back into c32rtomb and get the original byte.
Now that this is done, and the *wc* functions are all consistent with
each other, it's time to make c32tob consistent with mbrtoc32 and c32rtomb
as well.
Adding a unit test, I see that it fails
- mingw, MSVC,
- when module uchar-h-c23 is included, also on macOS, FreeBSD, NetBSD 10,
Solaris 11.
This is most likely due to the changes that I made since 2026-08-27.
This patch set fixes it.
2026-09-20 Bruno Haible <[email protected]>
c32tob: Add tests.
* tests/test-c32tob.c: New file.
* tests/test-c32tob.sh: New file.
* modules/c32tob-tests: New file.
c32tob: Make consistent with btoc32.
* lib/c32tob.c: Include hard-locale.h, <locale.h>.
(c32tob): On Cygwin and native Windows and when uchar-h-c23 is in use,
map the code points U+DF80..U+DFFF back to 0x80..0xFF.
* modules/c32tob (Depends-on): Add hard-locale.
(configure.ac): Require gl_MBRTOC32_C_LOCALE,
gl_MBRTOC32_C_LOCALE_LIKE_ISO_8859.
>From f8e2e1ce277cc2faa1e7fa0974aec2b631b1ea41 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Sun, 20 Sep 2026 21:54:23 +0200
Subject: [PATCH 1/2] c32tob: Make consistent with btoc32.
* lib/c32tob.c: Include hard-locale.h, <locale.h>.
(c32tob): On Cygwin and native Windows and when uchar-h-c23 is in use,
map the code points U+DF80..U+DFFF back to 0x80..0xFF.
* modules/c32tob (Depends-on): Add hard-locale.
(configure.ac): Require gl_MBRTOC32_C_LOCALE,
gl_MBRTOC32_C_LOCALE_LIKE_ISO_8859.
---
ChangeLog | 10 ++++++++++
lib/c32tob.c | 30 ++++++++++++++++++++++++++++--
modules/c32tob | 3 +++
3 files changed, 41 insertions(+), 2 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index a966fc7323..8afb186c2c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2026-09-20 Bruno Haible <[email protected]>
+
+ c32tob: Make consistent with btoc32.
+ * lib/c32tob.c: Include hard-locale.h, <locale.h>.
+ (c32tob): On Cygwin and native Windows and when uchar-h-c23 is in use,
+ map the code points U+DF80..U+DFFF back to 0x80..0xFF.
+ * modules/c32tob (Depends-on): Add hard-locale.
+ (configure.ac): Require gl_MBRTOC32_C_LOCALE,
+ gl_MBRTOC32_C_LOCALE_LIKE_ISO_8859.
+
2026-09-20 Bruno Haible <[email protected]>
mbrtoc32, c32rtomb: Make consistent with mbrtowc on glibc (regr. today).
diff --git a/lib/c32tob.c b/lib/c32tob.c
index 2424e1ee9c..e57e15479a 100644
--- a/lib/c32tob.c
+++ b/lib/c32tob.c
@@ -30,6 +30,12 @@
# include "lc-charset-unicode.h"
#endif
+#if _GL_SMALL_WCHAR_T \
+ || (GL_CHAR32_T_IS_UNICODE && GL_CHAR32_T_VS_WCHAR_T_NEEDS_CONVERSION)
+# include "hard-locale.h"
+# include <locale.h>
+#endif
+
#if _GL_WCHAR_T_IS_UCS4
_GL_EXTERN_INLINE
#endif
@@ -49,15 +55,35 @@ c32tob (wint_t wc)
return (unsigned char) buf[0];
}
return EOF;
-#elif _GL_SMALL_WCHAR_T
+#elif !GNULIB_defined_mbstate_t && _GL_SMALL_WCHAR_T /* Cygwin, mingw, MSVC */
/* In all known encodings, unibyte characters correspond only to
characters in the BMP. */
if (wc != WEOF && (wchar_t) wc == wc)
- return wctob ((wchar_t) wc);
+ {
+ if (!hard_locale (LC_CTYPE))
+ {
+ /* In the "C" locale, map the code points U+DF80..U+DFFF back
+ to the bytes 0x80..0xFF, for consistency with the mbrtoc32 and
+ btoc32 functions. */
+ if (wc >= 0x00 && wc <= 0x7F)
+ return (unsigned char) wc;
+ else if (wc >= 0xDF80 && wc <= 0xDFFF)
+ return (unsigned char) (wc - 0xDF00);
+ else
+ return EOF;
+ }
+ return wctob ((wchar_t) wc);
+ }
else
return EOF;
#else
# if GL_CHAR32_T_IS_UNICODE && GL_CHAR32_T_VS_WCHAR_T_NEEDS_CONVERSION
+ if ((wc >= 0xDF80 && wc <= 0xDFFF) && !hard_locale (LC_CTYPE))
+ {
+ /* In the "C" locale, map the code points U+DF80..U+DFFF back to the bytes
+ 0x80..0xFF, for consistency with the mbrtoc32 and btoc32 functions. */
+ return (unsigned char) (wc - 0xDF00);
+ }
if (wc != 0)
{
wc = unicode_to_locale_encoding (wc);
diff --git a/modules/c32tob b/modules/c32tob
index bbb3eedd19..8e646273e0 100644
--- a/modules/c32tob
+++ b/modules/c32tob
@@ -14,10 +14,13 @@ Depends-on:
uchar-h
mbszero
c32rtomb
+hard-locale
wctob
configure.ac:
AC_REQUIRE([gl_MBRTOC32_SANITYCHECK])
+AC_REQUIRE([gl_MBRTOC32_C_LOCALE])
+AC_REQUIRE([gl_MBRTOC32_C_LOCALE_LIKE_ISO_8859])
AC_REQUIRE([gl_C32RTOMB_SANITYCHECK])
gl_UCHAR_MODULE_INDICATOR([c32tob])
--
2.53.0
From eb72eb6f75f5621c5d648acd11467fd124584617 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Sun, 20 Sep 2026 21:55:23 +0200
Subject: [PATCH 2/2] c32tob: Add tests.
* tests/test-c32tob.c: New file.
* tests/test-c32tob.sh: New file.
* modules/c32tob-tests: New file.
---
ChangeLog | 5 ++
modules/c32tob-tests | 33 ++++++++++
tests/test-c32tob.c | 140 +++++++++++++++++++++++++++++++++++++++++++
tests/test-c32tob.sh | 49 +++++++++++++++
4 files changed, 227 insertions(+)
create mode 100644 modules/c32tob-tests
create mode 100644 tests/test-c32tob.c
create mode 100755 tests/test-c32tob.sh
diff --git a/ChangeLog b/ChangeLog
index 8afb186c2c..6600515ed4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
2026-09-20 Bruno Haible <[email protected]>
+ c32tob: Add tests.
+ * tests/test-c32tob.c: New file.
+ * tests/test-c32tob.sh: New file.
+ * modules/c32tob-tests: New file.
+
c32tob: Make consistent with btoc32.
* lib/c32tob.c: Include hard-locale.h, <locale.h>.
(c32tob): On Cygwin and native Windows and when uchar-h-c23 is in use,
diff --git a/modules/c32tob-tests b/modules/c32tob-tests
new file mode 100644
index 0000000000..bd1e4ef2a1
--- /dev/null
+++ b/modules/c32tob-tests
@@ -0,0 +1,33 @@
+Files:
+tests/test-c32tob.sh
+tests/test-c32tob.c
+tests/signature.h
+tests/macros.h
+m4/locale-en.m4
+m4/locale-fr.m4
+m4/locale-ja.m4
+m4/locale-zh.m4
+m4/codeset.m4
+
+Depends-on:
+btoc32
+setlocale
+
+configure.ac:
+gt_LOCALE_EN_UTF8
+gt_LOCALE_FR
+gt_LOCALE_FR_UTF8
+gt_LOCALE_JA
+gt_LOCALE_ZH_CN
+
+Makefile.am:
+TESTS += \
+ test-c32tob.sh
+TESTS_ENVIRONMENT += \
+ LOCALE_EN_UTF8='@LOCALE_EN_UTF8@' \
+ LOCALE_FR='@LOCALE_FR@' \
+ LOCALE_FR_UTF8='@LOCALE_FR_UTF8@' \
+ LOCALE_JA='@LOCALE_JA@' \
+ LOCALE_ZH_CN='@LOCALE_ZH_CN@'
+check_PROGRAMS += test-c32tob
+test_c32tob_LDADD = $(LDADD) $(LIBUNISTRING) $(SETLOCALE_LIB) $(LIBC32CONV)
diff --git a/tests/test-c32tob.c b/tests/test-c32tob.c
new file mode 100644
index 0000000000..281a7f96d9
--- /dev/null
+++ b/tests/test-c32tob.c
@@ -0,0 +1,140 @@
+/* Test of conversion of wide character to unibyte character.
+ Copyright (C) 2008-2026 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible <[email protected]>, 2026. */
+
+#include <config.h>
+
+#include <uchar.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (c32tob, int, (wint_t));
+
+#include <locale.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "macros.h"
+
+/* Check the unibyte character c. */
+static void
+check_character (unsigned char c)
+{
+ char32_t wc = btoc32 (c);
+ ASSERT (wc != WEOF);
+ int cc = c32tob (wc);
+ ASSERT (cc == c);
+}
+
+int
+main (int argc, char *argv[])
+{
+ /* configure should already have checked that the locale is supported. */
+ if (setlocale (LC_ALL, "") == NULL)
+ return 1;
+
+ /* Test NUL character. */
+ ASSERT (c32tob (0) == 0);
+
+ /* Test single bytes. */
+ for (int c = 0; c < 0x100; c++)
+ switch (c)
+ {
+ case '\t': case '\v': case '\f':
+ case ' ': case '!': case '"': case '#': case '%':
+ case '&': case '\'': case '(': case ')': case '*':
+ case '+': case ',': case '-': case '.': case '/':
+ case '0': case '1': case '2': case '3': case '4':
+ case '5': case '6': case '7': case '8': case '9':
+ case ':': case ';': case '<': case '=': case '>':
+ case '?':
+ case 'A': case 'B': case 'C': case 'D': case 'E':
+ case 'F': case 'G': case 'H': case 'I': case 'J':
+ case 'K': case 'L': case 'M': case 'N': case 'O':
+ case 'P': case 'Q': case 'R': case 'S': case 'T':
+ case 'U': case 'V': case 'W': case 'X': case 'Y':
+ case 'Z':
+ case '[': case '\\': case ']': case '^': case '_':
+ case 'a': case 'b': case 'c': case 'd': case 'e':
+ case 'f': case 'g': case 'h': case 'i': case 'j':
+ case 'k': case 'l': case 'm': case 'n': case 'o':
+ case 'p': case 'q': case 'r': case 's': case 't':
+ case 'u': case 'v': case 'w': case 'x': case 'y':
+ case 'z': case '{': case '|': case '}': case '~':
+ /* c is in the ISO C "basic character set". */
+ check_character (c);
+ break;
+ }
+
+ if (argc > 1)
+ switch (argv[1][0])
+ {
+ case '1':
+ /* C locale; tested above. */
+#if !defined __ANDROID__
+ /* On Android ≥ 5.0, the default locale is the "C.UTF-8" locale, not the
+ "C" locale. Furthermore, when you attempt to set the "C" or "POSIX"
+ locale via setlocale(), what you get is a "C" locale with UTF-8
+ encoding, that is, effectively the "C.UTF-8" locale. */
+ /* Check that c32tob does the inverse of btoc32, in the C locale.
+ Above we have only tested the ISO C "basic character set". */
+ for (int c = 0; c < 0x100; c++)
+ check_character (c);
+#endif
+ return test_exit_status;
+
+ case '2':
+ /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
+#if GL_CHAR32_T_IS_UNICODE
+ ASSERT (c32tob (0x00DF) == (unsigned char) '\337');
+ ASSERT (c32tob (0x00FC) == (unsigned char) '\374');
+#endif
+ return test_exit_status;
+
+ case '3':
+ /* Locale encoding is UTF-8. */
+ ASSERT (c32tob (0x0091) == EOF);
+ ASSERT (c32tob (0x00DF) == EOF);
+ ASSERT (c32tob (0x00FC) == EOF);
+ ASSERT (c32tob (0x1F60B) == EOF);
+ return test_exit_status;
+
+ case '4':
+ /* Locale encoding is EUC-JP. */
+#if GL_CHAR32_T_IS_UNICODE
+ ASSERT (c32tob (0x65E5) == EOF);
+ ASSERT (c32tob (0x672C) == EOF);
+ ASSERT (c32tob (0x8A9E) == EOF);
+#endif
+ return test_exit_status;
+
+ case '5':
+ /* Locale encoding is GB18030. */
+ #if (defined __GLIBC__ && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 13 && __GLIBC_MINOR__ <= 15) || (GL_CHAR32_T_IS_UNICODE && (defined __FreeBSD__ || defined __NetBSD__ || defined __sun))
+ if (test_exit_status != EXIT_SUCCESS)
+ return test_exit_status;
+ fputs ("Skipping test: The GB18030 converter in this system's iconv is broken.\n", stderr);
+ return 77;
+ #endif
+ ASSERT (c32tob (0x0091) == EOF);
+ ASSERT (c32tob (0x00DF) == EOF);
+ ASSERT (c32tob (0x00FC) == EOF);
+ ASSERT (c32tob (0x1F60B) == EOF);
+ return test_exit_status;
+ }
+
+ return 1;
+}
diff --git a/tests/test-c32tob.sh b/tests/test-c32tob.sh
new file mode 100755
index 0000000000..abe9fb51b2
--- /dev/null
+++ b/tests/test-c32tob.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+
+# Test in the POSIX locale.
+LC_ALL=C ${CHECKER} ./test-c32tob${EXEEXT} 1 || exit 1
+LC_ALL=POSIX ${CHECKER} ./test-c32tob${EXEEXT} 1 || exit 1
+
+# Test in an ISO-8859-1 or ISO-8859-15 locale.
+: "${LOCALE_FR=fr_FR}"
+if test $LOCALE_FR != none; then
+ LC_ALL=$LOCALE_FR \
+ ${CHECKER} ./test-c32tob${EXEEXT} 2 \
+ || exit 1
+fi
+
+# Test whether a specific UTF-8 locale is installed.
+: "${LOCALE_EN_UTF8=en_US.UTF-8}"
+: "${LOCALE_FR_UTF8=fr_FR.UTF-8}"
+if test "$LOCALE_EN_UTF8" != none || test $LOCALE_FR_UTF8 != none; then
+ # It's sufficient to test in one of the two locales.
+ if test $LOCALE_FR_UTF8 != none; then
+ testlocale=$LOCALE_FR_UTF8
+ else
+ testlocale="$LOCALE_EN_UTF8"
+ fi
+ LC_ALL="$testlocale" \
+ ${CHECKER} ./test-c32tob${EXEEXT} 3 \
+ || exit 1
+fi
+
+# Test whether a specific EUC-JP locale is installed.
+: "${LOCALE_JA=ja_JP}"
+if test $LOCALE_JA != none; then
+ LC_ALL=$LOCALE_JA \
+ ${CHECKER} ./test-c32tob${EXEEXT} 4 \
+ || exit 1
+fi
+
+# Test whether a specific GB18030 locale is installed.
+: "${LOCALE_ZH_CN=zh_CN.GB18030}"
+if test $LOCALE_ZH_CN != none; then
+ LC_ALL=$LOCALE_ZH_CN \
+ ${CHECKER} ./test-c32tob${EXEEXT} 5
+ case $? in
+ 0 | 77) ;;
+ *) exit 1 ;;
+ esac
+fi
+
+exit 0
--
2.53.0