From 2fba79d77532c61ff2a49ae02bea719cd3ab3ccf Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 2 Jul 2023 13:27:57 +0200 Subject: [PATCH] mbswidth: Overcome wchar_t limitations. * lib/mbswidth.c: Include instead of . (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 | 10 ++++++++++ lib/mbswidth.c | 16 +++++++++------- modules/mbswidth | 8 +++++--- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index bcc1bbd933..898ebbb4c5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2023-07-02 Bruno Haible + + mbswidth: Overcome wchar_t limitations. + * lib/mbswidth.c: Include instead of . + (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 Fix long bootstrap lines 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 -/* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth(). */ +/* Get mbstate_t, mbsinit(). */ #include -/* Get iswcntrl(). */ -#include +/* Get char32_t, mbrtoc32(), c32iscntrl(), c32width(). */ +#include /* Get INT_MAX. */ #include @@ -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 -- 2.39.5