]> Savannah Git Hosting - gnulib.git/commitdiff
mbswidth: Overcome wchar_t limitations.
authorBruno Haible <bruno@clisp.org>
Sun, 2 Jul 2023 11:27:57 +0000 (13:27 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 2 Jul 2023 11:27:57 +0000 (13:27 +0200)
* lib/mbswidth.c: Include <uchar.h> instead of <wctype.h>.
(mbsnwidth): Use mbrtoc32 instead of mbrtowc. Use c32width instead of
wcwidth. Use c32iscntrl instead of iswcntrl.
* modules/mbswidth (Depends-on): Remove wctype-h, mbrtowc, wcwidth. Add
uchar, mbrtoc32, c32width, c32iscntrl.
(Link): Add $(LIBC32CONV).

ChangeLog
lib/mbswidth.c
modules/mbswidth

index bcc1bbd933696e62a7b01cdda0cbce7a4b71ee33..898ebbb4c5cd2007d0880df9e1670549c205be56 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2023-07-02  Bruno Haible  <bruno@clisp.org>
+
+       mbswidth: Overcome wchar_t limitations.
+       * lib/mbswidth.c: Include <uchar.h> instead of <wctype.h>.
+       (mbsnwidth): Use mbrtoc32 instead of mbrtowc. Use c32width instead of
+       wcwidth. Use c32iscntrl instead of iswcntrl.
+       * modules/mbswidth (Depends-on): Remove wctype-h, mbrtowc, wcwidth. Add
+       uchar, mbrtoc32, c32width, c32iscntrl.
+       (Link): Add $(LIBC32CONV).
+
 2023-07-01  Paul Eggert  <eggert@cs.ucla.edu>
 
        Fix long bootstrap lines
index ca0a47d16f1ecb082636b10cd366fd03329118f9..da2d8030f3220f7e1ef75bdcf701339aaf8d82fe 100644 (file)
 /* Get isprint().  */
 #include <ctype.h>
 
-/* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth().  */
+/* Get mbstate_t, mbsinit().  */
 #include <wchar.h>
 
-/* Get iswcntrl().  */
-#include <wctype.h>
+/* Get char32_t, mbrtoc32(), c32iscntrl(), c32width().  */
+#include <uchar.h>
 
 /* Get INT_MAX.  */
 #include <limits.h>
@@ -97,11 +97,11 @@ mbsnwidth (const char *string, size_t nbytes, int flags)
                 memset (&mbstate, 0, sizeof mbstate);
                 do
                   {
-                    wchar_t wc;
+                    char32_t wc;
                     size_t bytes;
                     int w;
 
-                    bytes = mbrtowc (&wc, p, plimit - p, &mbstate);
+                    bytes = mbrtoc32 (&wc, p, plimit - p, &mbstate);
 
                     if (bytes == (size_t) -1)
                       /* An invalid multibyte sequence was encountered.  */
@@ -132,8 +132,10 @@ mbsnwidth (const char *string, size_t nbytes, int flags)
                     if (bytes == 0)
                       /* A null wide character was encountered.  */
                       bytes = 1;
+                    else if (bytes == (size_t) -3)
+                      bytes = 0;
 
-                    w = wcwidth (wc);
+                    w = c32width (wc);
                     if (w >= 0)
                       /* A printable multibyte character.  */
                       {
@@ -145,7 +147,7 @@ mbsnwidth (const char *string, size_t nbytes, int flags)
                       /* An unprintable multibyte character.  */
                       if (!(flags & MBSW_REJECT_UNPRINTABLE))
                         {
-                          if (!iswcntrl (wc))
+                          if (!c32iscntrl (wc))
                             {
                               if (width == INT_MAX)
                                 goto overflow;
index f3fe2ed21bbd7402bae7486993b67e8b5ebd7ec3..4dd8c55ee4fc891c72d9655b3b278ac453a19ef4 100644 (file)
@@ -10,10 +10,11 @@ m4/mbswidth.m4
 
 Depends-on:
 wchar
-wctype-h
-mbrtowc
+uchar
+mbrtoc32
 mbsinit
-wcwidth
+c32width
+c32iscntrl
 extensions
 
 configure.ac:
@@ -28,6 +29,7 @@ Include:
 Link:
 $(LTLIBUNISTRING) when linking with libtool, $(LIBUNISTRING) otherwise
 $(MBRTOWC_LIB)
+$(LTLIBC32CONV) when linking with libtool, $(LIBC32CONV) otherwise
 
 License:
 GPL