From: Bruno Haible Date: Wed, 1 Jan 2020 16:01:54 +0000 (+0100) Subject: c32tob: New module. X-Git-Tag: v1.0~4404 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=86e05effe83d629bfcb4d798c4951781387a1bfa;p=gnulib.git c32tob: New module. * lib/uchar.in.h (_GL_LARGE_CHAR32_T): New macro. (c32tob): New declaration. * lib/c32tob.c: New file. * m4/uchar.m4 (gl_UCHAR_MODULE_INDICATOR, gl_UCHAR_H_DEFAULTS): New macros. (gl_UCHAR_H): Require gl_UCHAR_H_DEFAULTS. * modules/uchar (Depends-on): Add snippet/c++defs. (Makefile.am): Include c++defs.h and substitute GNULIB_C32TOB in uchar.h. * modules/c32tob: New file. * tests/test-uchar.c: Verify that _GL_LARGE_CHAR32_T is correctly defined. * tests/test-uchar-c++.cc: Include signature.h. Test the signature of c32tob. * modules/uchar-c++-tests (Files): Add tests/signature.h. * doc/posix-functions/wctob.texi: Mention the new module. --- diff --git a/ChangeLog b/ChangeLog index 88e602f6f6..9e6db1a0eb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +2020-01-01 Bruno Haible + + c32tob: New module. + * lib/uchar.in.h (_GL_LARGE_CHAR32_T): New macro. + (c32tob): New declaration. + * lib/c32tob.c: New file. + * m4/uchar.m4 (gl_UCHAR_MODULE_INDICATOR, gl_UCHAR_H_DEFAULTS): New + macros. + (gl_UCHAR_H): Require gl_UCHAR_H_DEFAULTS. + * modules/uchar (Depends-on): Add snippet/c++defs. + (Makefile.am): Include c++defs.h and substitute GNULIB_C32TOB in + uchar.h. + * modules/c32tob: New file. + * tests/test-uchar.c: Verify that _GL_LARGE_CHAR32_T is correctly + defined. + * tests/test-uchar-c++.cc: Include signature.h. Test the signature of + c32tob. + * modules/uchar-c++-tests (Files): Add tests/signature.h. + * doc/posix-functions/wctob.texi: Mention the new module. + 2020-01-01 Bruno Haible locale C++ tests: Fix link error on AIX (regression from 2019-12-18). diff --git a/doc/posix-functions/wctob.texi b/doc/posix-functions/wctob.texi index 34164ffd9d..e91ce5de65 100644 --- a/doc/posix-functions/wctob.texi +++ b/doc/posix-functions/wctob.texi @@ -25,6 +25,9 @@ IRIX 6.5. Portability problems not fixed by Gnulib: @itemize @item -On Windows and 32-bit AIX platforms, @code{wchar_t} is a 16-bit type and therefore cannot -accommodate all Unicode characters. +On Windows and 32-bit AIX platforms, @code{wchar_t} is a 16-bit type and +therefore cannot accommodate all Unicode characters. +However, the Gnulib function @code{c32tob}, provided by Gnulib module +@code{c32tob}, operates on 32-bit wide characters and therefore does not have +this limitation. @end itemize diff --git a/lib/c32tob.c b/lib/c32tob.c new file mode 100644 index 0000000000..e9db7a1a98 --- /dev/null +++ b/lib/c32tob.c @@ -0,0 +1,37 @@ +/* Convert 32-bit wide character to unibyte character. + Copyright (C) 2020 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 . */ + +/* Written by Bruno Haible , 2020. */ + +#include + +/* Specification. */ +#include + +int +c32tob (wint_t wc) +{ +#if _GL_LARGE_CHAR32_T + /* 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); + else + return EOF; +#else + return wctob (wc); +#endif +} diff --git a/lib/uchar.in.h b/lib/uchar.in.h index 972ce4f973..9cba39bf80 100644 --- a/lib/uchar.in.h +++ b/lib/uchar.in.h @@ -37,6 +37,9 @@ /* Get mbstate_t, size_t. */ #include +/* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */ + + #if !@HAVE_UCHAR_H@ /* A 16-bit variant of wchar_t. @@ -51,4 +54,20 @@ typedef uint_least32_t char32_t; #endif +/* Define if a 'char32_t' can hold more characters than a 'wchar_t'. */ +#if (defined _AIX && !defined __64BIT__) || defined _WIN32 || defined __CYGWIN__ +# define _GL_LARGE_CHAR32_T 1 +#endif + + +/* Converts a 32-bit wide character to unibyte character. + Returns the single-byte representation of WC if it exists, + or EOF otherwise. */ +#if @GNULIB_C32TOB@ +_GL_FUNCDECL_SYS (c32tob, int, (wint_t wc)); +_GL_CXXALIAS_SYS (c32tob, int, (wint_t wc)); +_GL_CXXALIASWARN (c32tob); +#endif + + #endif /* _@GUARD_PREFIX@_UCHAR_H */ diff --git a/m4/uchar.m4 b/m4/uchar.m4 index 69a049c75a..c5a3594ecd 100644 --- a/m4/uchar.m4 +++ b/m4/uchar.m4 @@ -1,4 +1,4 @@ -# uchar.m4 serial 1 +# uchar.m4 serial 2 dnl Copyright (C) 2019-2020 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -9,6 +9,8 @@ dnl Prepare the overridden . AC_DEFUN_ONCE([gl_UCHAR_H], [ + AC_REQUIRE([gl_UCHAR_H_DEFAULTS]) + gl_CHECK_NEXT_HEADERS([uchar.h]) if test $ac_cv_header_uchar_h = yes; then HAVE_UCHAR_H=1 @@ -17,3 +19,17 @@ AC_DEFUN_ONCE([gl_UCHAR_H], fi AC_SUBST([HAVE_UCHAR_H]) ]) + +AC_DEFUN([gl_UCHAR_MODULE_INDICATOR], +[ + dnl Use AC_REQUIRE here, so that the default settings are expanded once only. + AC_REQUIRE([gl_UCHAR_H_DEFAULTS]) + gl_MODULE_INDICATOR_SET_VARIABLE([$1]) + dnl Define it also as a C macro, for the benefit of the unit tests. + gl_MODULE_INDICATOR_FOR_TESTS([$1]) +]) + +AC_DEFUN([gl_UCHAR_H_DEFAULTS], +[ + GNULIB_C32TOB=0; AC_SUBST([GNULIB_C32TOB]) +]) diff --git a/modules/c32tob b/modules/c32tob new file mode 100644 index 0000000000..3ef42ba613 --- /dev/null +++ b/modules/c32tob @@ -0,0 +1,24 @@ +Description: +c32tob() function: convert 32-bit wide character to unibyte character. + +Files: +lib/c32tob.c + +Depends-on: +uchar +wctob + +configure.ac: +gl_UCHAR_MODULE_INDICATOR([c32tob]) + +Makefile.am: +lib_SOURCES += c32tob.c + +Include: + + +License: +LGPL + +Maintainer: +Bruno Haible diff --git a/modules/uchar b/modules/uchar index 48ed448af2..67a8866214 100644 --- a/modules/uchar +++ b/modules/uchar @@ -7,6 +7,7 @@ m4/uchar.m4 Depends-on: include_next +snippet/c++defs stdint wchar @@ -16,7 +17,7 @@ gl_UCHAR_H Makefile.am: BUILT_SOURCES += uchar.h -uchar.h: uchar.in.h $(top_builddir)/config.status +uchar.h: uchar.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(AM_V_GEN)rm -f $@-t $@ && \ { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \ sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \ @@ -25,6 +26,8 @@ uchar.h: uchar.in.h $(top_builddir)/config.status -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \ -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \ -e 's|@''NEXT_UCHAR_H''@|$(NEXT_UCHAR_H)|g' \ + -e 's/@''GNULIB_C32TOB''@/$(GNULIB_C32TOB)/g' \ + -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \ < $(srcdir)/uchar.in.h; \ } > $@-t && \ mv $@-t $@ diff --git a/modules/uchar-c++-tests b/modules/uchar-c++-tests index 8fb7463e60..69058e3a1f 100644 --- a/modules/uchar-c++-tests +++ b/modules/uchar-c++-tests @@ -1,6 +1,7 @@ Files: tests/test-uchar-c++.cc tests/test-uchar-c++2.cc +tests/signature.h Status: c++-test diff --git a/tests/test-uchar-c++.cc b/tests/test-uchar-c++.cc index ccdb3461d4..9a11a13503 100644 --- a/tests/test-uchar-c++.cc +++ b/tests/test-uchar-c++.cc @@ -21,6 +21,13 @@ #include +#include "signature.h" + + +#if GNULIB_TEST_C32TOB +SIGNATURE_CHECK (GNULIB_NAMESPACE::c32tob, int, (wint_t)); +#endif + int main () diff --git a/tests/test-uchar.c b/tests/test-uchar.c index c703906a74..c2de59d479 100644 --- a/tests/test-uchar.c +++ b/tests/test-uchar.c @@ -35,6 +35,13 @@ verify ((char32_t)(-1) >= 0); /* Check that char32_t is at least 31 bits wide. */ verify ((char32_t)0x7FFFFFFF != (char32_t)0x3FFFFFFF); +/* Check that _GL_LARGE_CHAR32_T is correctly defined. */ +#if _GL_LARGE_CHAR32_T +verify (sizeof (char32_t) > sizeof (wchar_t)); +#else +verify (sizeof (char32_t) == sizeof (wchar_t)); +#endif + int main (void) {