]> Savannah Git Hosting - gnulib.git/commitdiff
c32tob: New module.
authorBruno Haible <bruno@clisp.org>
Wed, 1 Jan 2020 16:01:54 +0000 (17:01 +0100)
committerBruno Haible <bruno@clisp.org>
Wed, 1 Jan 2020 16:01:54 +0000 (17:01 +0100)
* 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.

ChangeLog
doc/posix-functions/wctob.texi
lib/c32tob.c [new file with mode: 0644]
lib/uchar.in.h
m4/uchar.m4
modules/c32tob [new file with mode: 0644]
modules/uchar
modules/uchar-c++-tests
tests/test-uchar-c++.cc
tests/test-uchar.c

index 88e602f6f6fe90162da499675b839e370401869a..9e6db1a0eb48e1109048998958874f65e68a025c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2020-01-01  Bruno Haible  <bruno@clisp.org>
+
+       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  <bruno@clisp.org>
 
        locale C++ tests: Fix link error on AIX (regression from 2019-12-18).
index 34164ffd9d17235f06127264f79b5acd00bdd505..e91ce5de654a1499997a4127d3d6e1c119a5d0c9 100644 (file)
@@ -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 (file)
index 0000000..e9db7a1
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2020.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <uchar.h>
+
+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
+}
index 972ce4f973a4e7dee6a3aadc2157f39b19e7e8ba..9cba39bf80d8406c1041fa84e9bf37ee2576f9e9 100644 (file)
@@ -37,6 +37,9 @@
 /* Get mbstate_t, size_t.  */
 #include <wchar.h>
 
+/* 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 */
index 69a049c75a6c15a7470abb3ce5dde62971ddc318..c5a3594ecd4362d741e020eb2077823b46a11598 100644 (file)
@@ -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 <uchar.h>.
 
 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 (file)
index 0000000..3ef42ba
--- /dev/null
@@ -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:
+<uchar.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
index 48ed448af2b913eaa767edf35367575620ee2fc6..67a88662147d886810093d6f103db05b8a0f6276 100644 (file)
@@ -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 $@
index 8fb7463e60961e3bfc6e86658f507b340eb4206c..69058e3a1fec0c7115040d35563d08aa175169c7 100644 (file)
@@ -1,6 +1,7 @@
 Files:
 tests/test-uchar-c++.cc
 tests/test-uchar-c++2.cc
+tests/signature.h
 
 Status:
 c++-test
index ccdb3461d448dbeafa33897bf23110cabc0e64bc..9a11a13503dcd41e289eebb93d4ec0596dcb5179 100644 (file)
 
 #include <uchar.h>
 
+#include "signature.h"
+
+
+#if GNULIB_TEST_C32TOB
+SIGNATURE_CHECK (GNULIB_NAMESPACE::c32tob, int, (wint_t));
+#endif
+
 
 int
 main ()
index c703906a74785cd82b9ad998e294200c7c64d178..c2de59d4792c63b57043c7e38a46152772aa206d 100644 (file)
@@ -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)
 {