This patch migrates the 'mbswidth' module from wchar_t to char32_t.
2023-07-02 Bruno Haible <br...@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). diff --git a/lib/mbswidth.c b/lib/mbswidth.c index ca0a47d16f..da2d8030f3 100644 --- a/lib/mbswidth.c +++ b/lib/mbswidth.c @@ -29,11 +29,11 @@ /* 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; diff --git a/modules/mbswidth b/modules/mbswidth index f3fe2ed21b..4dd8c55ee4 100644 --- a/modules/mbswidth +++ b/modules/mbswidth @@ -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