[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
new module 'c32tob'
From: |
Bruno Haible |
Subject: |
new module 'c32tob' |
Date: |
Wed, 01 Jan 2020 17:04:39 +0100 |
User-agent: |
KMail/5.1.3 (Linux/4.4.0-170-generic; KDE/5.18.0; x86_64; ; ) |
c32tob is like wctob, just for 32-bit wide characters.
It's not a standardized function; just a convenience function provided by
gnulib.
2020-01-01 Bruno Haible <address@hidden>
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/doc/posix-functions/wctob.texi b/doc/posix-functions/wctob.texi
index 34164ff..e91ce5d 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 0000000..e9db7a1
--- /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 <http://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible <address@hidden>, 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
+}
diff --git a/lib/uchar.in.h b/lib/uchar.in.h
index 972ce4f..9cba39b 100644
--- a/lib/uchar.in.h
+++ b/lib/uchar.in.h
@@ -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 */
diff --git a/m4/uchar.m4 b/m4/uchar.m4
index 69a049c..c5a3594 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 <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
index 0000000..3ef42ba
--- /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:
+<uchar.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
diff --git a/modules/uchar b/modules/uchar
index 48ed448..67a8866 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 8fb7463..69058e3 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 ccdb346..9a11a13 100644
--- a/tests/test-uchar-c++.cc
+++ b/tests/test-uchar-c++.cc
@@ -21,6 +21,13 @@
#include <uchar.h>
+#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 c703906..c2de59d 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)
{
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- new module 'c32tob',
Bruno Haible <=