]> Savannah Git Hosting - gnulib.git/commitdiff
mbsrtoc32s: New module.
authorBruno Haible <bruno@clisp.org>
Sat, 4 Jan 2020 15:02:31 +0000 (16:02 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 4 Jan 2020 15:09:35 +0000 (16:09 +0100)
* lib/uchar.in.h (mbsrtoc32s): New declaration.
* lib/mbsrtowcs-impl.h: Parameterize: Use macros FUNC, DCHAR_T,
INTERNAL_STATE, MBRTOWC.
* lib/mbsrtowcs.c (FUNC, DCHAR_T, INTERNAL_STATE, MBRTOWC): New macros.
* lib/mbsrtoc32s.c: New file.
* lib/mbsrtoc32s-state.c: New file, based on lib/mbsrtowcs-state.c.
* m4/uchar.m4 (gl_UCHAR_H_DEFAULTS): Initialize GNULIB_MBSRTOC32S.
* modules/uchar (Makefile.am): Substitute GNULIB_MBSRTOC32S.
* modules/mbsrtoc32s: New file.
* tests/test-uchar-c++.cc: Test the signature of mbsrtoc32s.
* doc/posix-functions/mbsrtowcs.texi: Mention the new module.

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

index 91bcb3eedc94b37d850f37bc05a55995ac2b2dd4..08cceb3337d4127387bc38b686d2b6fc840a3351 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2020-01-04  Bruno Haible  <bruno@clisp.org>
+
+       mbsrtoc32s: New module.
+       * lib/uchar.in.h (mbsrtoc32s): New declaration.
+       * lib/mbsrtowcs-impl.h: Parameterize: Use macros FUNC, DCHAR_T,
+       INTERNAL_STATE, MBRTOWC.
+       * lib/mbsrtowcs.c (FUNC, DCHAR_T, INTERNAL_STATE, MBRTOWC): New macros.
+       * lib/mbsrtoc32s.c: New file.
+       * lib/mbsrtoc32s-state.c: New file, based on lib/mbsrtowcs-state.c.
+       * m4/uchar.m4 (gl_UCHAR_H_DEFAULTS): Initialize GNULIB_MBSRTOC32S.
+       * modules/uchar (Makefile.am): Substitute GNULIB_MBSRTOC32S.
+       * modules/mbsrtoc32s: New file.
+       * tests/test-uchar-c++.cc: Test the signature of mbsrtoc32s.
+       * doc/posix-functions/mbsrtowcs.texi: Mention the new module.
+
 2020-01-04  Bruno Haible  <bruno@clisp.org>
 
        mbrtowc, mbrtoc32: Tighten dependendies.
index e6e4e5afc7302910d8250db97afb60a040ed44d4..03c9983ef9902bebf96ec8f4655e722a30fb162d 100644 (file)
@@ -22,8 +22,11 @@ mingw.
 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{mbsrtoc32s}, provided by Gnulib module
+@code{mbsrtoc32s}, operates on 32-bit wide characters and therefore does not
+have this limitation.
 @item
 The specification is not clear about whether this function should update the
 conversion state when the first argument (the destination pointer) is NULL.
diff --git a/lib/mbsrtoc32s-state.c b/lib/mbsrtoc32s-state.c
new file mode 100644 (file)
index 0000000..6e041ef
--- /dev/null
@@ -0,0 +1,37 @@
+/* Convert string to 32-bit wide string.
+   Copyright (C) 2008-2020 Free Software Foundation, Inc.
+   Written by Bruno Haible <bruno@clisp.org>, 2020.
+
+   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/>.  */
+
+#include <config.h>
+
+#include <wchar.h>
+
+/* Internal state used by the functions mbsrtoc32s() and mbsnrtoc32s().  */
+mbstate_t _gl_mbsrtoc32s_state
+/* The state must initially be in the "initial state"; so, zero-initialize it.
+   On most systems, putting it into BSS is sufficient.  Not so on Mac OS X 10.3,
+   see <https://lists.gnu.org/r/bug-gnulib/2009-01/msg00329.html>.
+   When it needs an initializer, use 0 or {0} as initializer? 0 only works
+   when mbstate_t is a scalar type (such as when gnulib defines it, or on
+   AIX, IRIX, mingw). {0} works as an initializer in all cases: for a struct
+   or union type, but also for a scalar type (ISO C 99, 6.7.8.(11)).  */
+#if defined __ELF__
+  /* On ELF systems, variables in BSS behave well.  */
+#else
+  /* Use braces, to be on the safe side.  */
+  = { 0 }
+#endif
+  ;
diff --git a/lib/mbsrtoc32s.c b/lib/mbsrtoc32s.c
new file mode 100644 (file)
index 0000000..432ffaf
--- /dev/null
@@ -0,0 +1,54 @@
+/* Convert string to 32-bit wide string.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+   Written by Bruno Haible <bruno@clisp.org>, 2020.
+
+   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/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <uchar.h>
+
+#include <wchar.h>
+
+#if _GL_LARGE_CHAR32_T
+
+# include <errno.h>
+# include <limits.h>
+# include <stdlib.h>
+
+# include "strnlen1.h"
+
+extern mbstate_t _gl_mbsrtoc32s_state;
+
+# define FUNC mbsrtoc32s
+# define DCHAR_T char32_t
+# define INTERNAL_STATE _gl_mbsrtoc32s_state
+# define MBRTOWC mbrtoc32
+# include "mbsrtowcs-impl.h"
+
+#else
+/* char32_t and wchar_t are equivalent.  */
+
+# include "verify.h"
+
+verify (sizeof (char32_t) == sizeof (wchar_t));
+
+size_t
+mbsrtoc32s (char32_t *dest, const char **srcp, size_t len, mbstate_t *ps)
+{
+  return mbsrtowcs ((wchar_t *) dest, srcp, len, ps);
+}
+
+#endif
index 053417b8aed4ec42d8bb2cd179f1e18890181fe6..06ecec2dcad91c5a5715605efb0249c2ed31303e 100644 (file)
    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
 
 size_t
-mbsrtowcs (wchar_t *dest, const char **srcp, size_t len, mbstate_t *ps)
+FUNC (DCHAR_T *dest, const char **srcp, size_t len, mbstate_t *ps)
 {
   if (ps == NULL)
-    ps = &_gl_mbsrtowcs_state;
+    ps = &INTERNAL_STATE;
   {
     const char *src = *srcp;
 
     if (dest != NULL)
       {
-        wchar_t *destptr = dest;
+        DCHAR_T *destptr = dest;
 
         for (; len > 0; destptr++, len--)
           {
@@ -46,7 +46,7 @@ mbsrtowcs (wchar_t *dest, const char **srcp, size_t len, mbstate_t *ps)
               src_avail = 4 + strnlen1 (src + 4, MB_LEN_MAX - 4);
 
             /* Parse the next multibyte character.  */
-            ret = mbrtowc (destptr, src, src_avail, ps);
+            ret = MBRTOWC (destptr, src, src_avail, ps);
 
             if (ret == (size_t)(-2))
               /* Encountered a multibyte character that extends past a '\0' byte
@@ -93,7 +93,7 @@ mbsrtowcs (wchar_t *dest, const char **srcp, size_t len, mbstate_t *ps)
               src_avail = 4 + strnlen1 (src + 4, MB_LEN_MAX - 4);
 
             /* Parse the next multibyte character.  */
-            ret = mbrtowc (NULL, src, src_avail, &state);
+            ret = MBRTOWC (NULL, src, src_avail, &state);
 
             if (ret == (size_t)(-2))
               /* Encountered a multibyte character that extends past a '\0' byte
index 0c164e76c8b59974bd4ec6e1fe2d97611ac20604..6d1c6855f0e4fb9f918fa3f9e458ff71bfd64bd4 100644 (file)
@@ -29,4 +29,8 @@
 
 extern mbstate_t _gl_mbsrtowcs_state;
 
+#define FUNC mbsrtowcs
+#define DCHAR_T wchar_t
+#define INTERNAL_STATE _gl_mbsrtowcs_state
+#define MBRTOWC mbrtowc
 #include "mbsrtowcs-impl.h"
index 5241b2e4ff86371c893561bc9144e6f46d4709ed..318cf8ef64a0ef53bc2b4b16a7f73c45b0c0eed3 100644 (file)
@@ -107,4 +107,16 @@ _GL_WARN_ON_USE (mbrtoc32, "mbrtoc32 is not portable - "
 #endif
 
 
+/* Convert a string to a 32-bit wide string.  */
+#if @GNULIB_MBSRTOC32S@
+_GL_FUNCDECL_SYS (mbsrtoc32s, size_t,
+                  (char32_t *dest, const char **srcp, size_t len, mbstate_t *ps)
+                  _GL_ARG_NONNULL ((2)));
+_GL_CXXALIAS_SYS (mbsrtoc32s, size_t,
+                  (char32_t *dest, const char **srcp, size_t len,
+                   mbstate_t *ps));
+_GL_CXXALIASWARN (mbsrtoc32s);
+#endif
+
+
 #endif /* _@GUARD_PREFIX@_UCHAR_H */
index 4aebf5c0769f464e8f77f15b1036fa34471da0eb..e92f5d6da635f82513c04c3eee4b3035cf252463 100644 (file)
@@ -1,4 +1,4 @@
-# uchar.m4 serial 5
+# uchar.m4 serial 6
 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,
@@ -50,6 +50,7 @@ AC_DEFUN([gl_UCHAR_H_DEFAULTS],
   GNULIB_BTOC32=0;           AC_SUBST([GNULIB_BTOC32])
   GNULIB_C32TOB=0;           AC_SUBST([GNULIB_C32TOB])
   GNULIB_MBRTOC32=0;         AC_SUBST([GNULIB_MBRTOC32])
+  GNULIB_MBSRTOC32S=0;       AC_SUBST([GNULIB_MBSRTOC32S])
   dnl Assume proper GNU behavior unless another module says otherwise.
   HAVE_MBRTOC32=1;           AC_SUBST([HAVE_MBRTOC32])
   REPLACE_MBRTOC32=0;        AC_SUBST([REPLACE_MBRTOC32])
diff --git a/modules/mbsrtoc32s b/modules/mbsrtoc32s
new file mode 100644 (file)
index 0000000..66b578e
--- /dev/null
@@ -0,0 +1,36 @@
+Description:
+mbsrtoc32s() function: convert string to 32-bit wide string.
+
+Files:
+lib/mbsrtoc32s.c
+lib/mbsrtowcs-impl.h
+lib/mbsrtoc32s-state.c
+
+Depends-on:
+uchar
+wchar
+verify
+mbrtoc32        [test $SMALL_WCHAR_T = 1]
+strnlen1        [test $SMALL_WCHAR_T = 1]
+mbsrtowcs       [test $SMALL_WCHAR_T = 0]
+
+configure.ac:
+if test $SMALL_WCHAR_T = 1; then
+  AC_LIBOBJ([mbsrtoc32s-state])
+fi
+gl_UCHAR_MODULE_INDICATOR([mbsrtoc32s])
+
+Makefile.am:
+lib_SOURCES += mbsrtoc32s.c
+
+Include:
+<uchar.h>
+
+Link:
+$(LIB_MBRTOWC)
+
+License:
+LGPLv2+
+
+Maintainer:
+Bruno Haible
index bd1b4e0473bcfea07a2bb890f45196db8bbf5d0b..03101c1f073e3832080aac2902a3435b58eea119 100644 (file)
@@ -31,6 +31,7 @@ uchar.h: uchar.in.h $(top_builddir)/config.status $(CXXDEFS_H)
              -e 's/@''GNULIB_BTOC32''@/$(GNULIB_BTOC32)/g' \
              -e 's/@''GNULIB_C32TOB''@/$(GNULIB_C32TOB)/g' \
              -e 's/@''GNULIB_MBRTOC32''@/$(GNULIB_MBRTOC32)/g' \
+             -e 's/@''GNULIB_MBSRTOC32S''@/$(GNULIB_MBSRTOC32S)/g' \
              -e 's|@''HAVE_MBRTOC32''@|$(HAVE_MBRTOC32)|g' \
              -e 's|@''REPLACE_MBRTOC32''@|$(REPLACE_MBRTOC32)|g' \
              -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
index 56a3a25a53da7529d477a6dcbb97630285ed06d5..a7132a13b2547988627c8ba06d4deef3ba2e847f 100644 (file)
@@ -37,6 +37,11 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::mbrtoc32, size_t,
                  (char32_t *, const char *, size_t, mbstate_t *));
 #endif
 
+#if GNULIB_TEST_MBSRTOC32S
+SIGNATURE_CHECK (GNULIB_NAMESPACE::mbsrtoc32s, size_t,
+                 (char32_t *, const char **, size_t, mbstate_t *));
+#endif
+
 
 int
 main ()