[RFA] wcswidth and wcwidth
J. Johnston
jjohnstn@redhat.com
Tue Apr 8 17:45:00 GMT 2003
Ok to apply.
-- Jeff J.
Corinna Vinschen wrote:
> On Thu, Apr 03, 2003 at 12:27:14PM +0200, Corinna Vinschen wrote:
>
>>Hi,
>>
>>attached is an implementation of wcswidth and wcwidth as described in SUSv3,
>>see http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html
>>and http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
>>
>>Since newlib doesn't have width tables for characters, all printable
>>characters are treated equally as having a width of 1, all control chars
>>have a width of 0. Any other non-printable, non-control character is
>>treated as non-existant in the current locale, so wcwidth returns a
>>width of -1.
>>
>>wcswidth is implemented using wcwidth.
>
>
> Sic. It looks like I need always a second try. This time I scrambled the
> loop condition in wcswidth. Should be &&, not ||. New patch attached.
>
> Corinna
>
> 2003-04-03 Corinna Vinschen <corinna@vinschen.de>
>
> * libc/include/wchar.h: Add definitions for wcswidth and wcwidth.
> * libc/string/Makefile.am: Add wcswidth.c and wcwidth.c
> * libc/string/Makefile.in: Regenerated.
> * libc/string/wcswidth.c: New file.
> * libc/string/wcwidth.c: New file.
> * libc/string/wcstrings.tex: Add wcswidth and wcwidth.
>
> Index: libc/include/wchar.h
> ===================================================================
> RCS file: /cvs/src/src/newlib/libc/include/wchar.h,v
> retrieving revision 1.9
> diff -p -u -r1.9 wchar.h
> --- libc/include/wchar.h 3 Apr 2003 08:26:07 -0000 1.9
> +++ libc/include/wchar.h 3 Apr 2003 13:35:53 -0000
> @@ -62,6 +62,8 @@ wchar_t *_EXFUN(wcspbrk, (const wchar_t
> wchar_t *_EXFUN(wcsrchr, (const wchar_t *, wchar_t));
> size_t _EXFUN(wcsspn, (const wchar_t *, const wchar_t *));
> wchar_t *_EXFUN(wcsstr, (const wchar_t *, const wchar_t *));
> +int _EXFUN(wcswidth, (const wchar_t *, size_t));
> +int _EXFUN(wcwidth, (const wchar_t));
> wchar_t *_EXFUN(wmemchr, (const wchar_t *, wchar_t, size_t));
> int _EXFUN(wmemcmp, (const wchar_t *, const wchar_t *, size_t));
> wchar_t *_EXFUN(wmemcpy, (wchar_t * , const wchar_t * , size_t));
> Index: libc/string/Makefile.am
> ===================================================================
> RCS file: /cvs/src/src/newlib/libc/string/Makefile.am,v
> retrieving revision 1.11
> diff -p -u -r1.11 Makefile.am
> --- libc/string/Makefile.am 3 Apr 2003 08:26:07 -0000 1.11
> +++ libc/string/Makefile.am 3 Apr 2003 13:35:53 -0000
> @@ -61,6 +61,8 @@ GENERAL_SOURCES = \
> wcsrchr.c \
> wcsspn.c \
> wcsstr.c \
> + wcswidth.c \
> + wcwidth.c \
> wmemchr.c \
> wmemcmp.c \
> wmemcpy.c \
> @@ -109,7 +111,8 @@ wcscat.def wcschr.def wcscmp.def wcscoll
> wcscpy.def wcscspn.def \
> wcslcat.def wcslcpy.def wcslen.def wcsncat.def wcsncmp.def \
> wcsncpy.def wcspbrk.def wcsrchr.def wcsspn.def wcsstr.def \
> -wmemchr.def wmemcmp.def wmemcpy.def wmemmove.def wmemset.def
> +wcswidth.def wcwidth.def wmemchr.def wmemcmp.def wmemcpy.def \
> +wmemmove.def wmemset.def
>
> SUFFIXES = .def
>
> Index: libc/string/Makefile.in
> ===================================================================
> RCS file: /cvs/src/src/newlib/libc/string/Makefile.in,v
> retrieving revision 1.15
> diff -p -u -r1.15 Makefile.in
> --- libc/string/Makefile.in 3 Apr 2003 08:26:07 -0000 1.15
> +++ libc/string/Makefile.in 3 Apr 2003 13:35:53 -0000
> @@ -167,6 +167,8 @@ GENERAL_SOURCES = \
> wcsrchr.c \
> wcsspn.c \
> wcsstr.c \
> + wcswidth.c \
> + wcwidth.c \
> wmemchr.c \
> wmemcmp.c \
> wmemcpy.c \
> @@ -207,7 +209,8 @@ wcscat.def wcschr.def wcscmp.def wcscoll
> wcscpy.def wcscspn.def \
> wcslcat.def wcslcpy.def wcslen.def wcsncat.def wcsncmp.def \
> wcsncpy.def wcspbrk.def wcsrchr.def wcsspn.def wcsstr.def \
> -wmemchr.def wmemcmp.def wmemcpy.def wmemmove.def wmemset.def
> +wcswidth.def wcwidth.def wmemchr.def wmemcmp.def wmemcpy.def \
> +wmemmove.def wmemset.def
>
>
> SUFFIXES = .def
> @@ -246,9 +249,9 @@ LIBS = @LIBS@
> @USE_LIBTOOL_FALSE@wcslcpy.$(OBJEXT) wcslen.$(OBJEXT) wcsncat.$(OBJEXT) \
> @USE_LIBTOOL_FALSE@wcsncmp.$(OBJEXT) wcsncpy.$(OBJEXT) \
> @USE_LIBTOOL_FALSE@wcspbrk.$(OBJEXT) wcsrchr.$(OBJEXT) wcsspn.$(OBJEXT) \
> -@USE_LIBTOOL_FALSE@wcsstr.$(OBJEXT) wmemchr.$(OBJEXT) wmemcmp.$(OBJEXT) \
> -@USE_LIBTOOL_FALSE@wmemcpy.$(OBJEXT) wmemmove.$(OBJEXT) \
> -@USE_LIBTOOL_FALSE@wmemset.$(OBJEXT)
> +@USE_LIBTOOL_FALSE@wcsstr.$(OBJEXT) wcswidth.$(OBJEXT) wcwidth.$(OBJEXT) \
> +@USE_LIBTOOL_FALSE@wmemchr.$(OBJEXT) wmemcmp.$(OBJEXT) wmemcpy.$(OBJEXT) \
> +@USE_LIBTOOL_FALSE@wmemmove.$(OBJEXT) wmemset.$(OBJEXT)
> LTLIBRARIES = $(noinst_LTLIBRARIES)
>
> @USE_LIBTOOL_TRUE@libstring_la_OBJECTS = bcopy.lo bzero.lo index.lo \
> @@ -263,8 +266,8 @@ LTLIBRARIES = $(noinst_LTLIBRARIES)
> @USE_LIBTOOL_TRUE@wcscat.lo wcschr.lo wcscmp.lo wcscoll.lo wcscpy.lo \
> @USE_LIBTOOL_TRUE@wcscspn.lo wcslcat.lo wcslcpy.lo wcslen.lo wcsncat.lo \
> @USE_LIBTOOL_TRUE@wcsncmp.lo wcsncpy.lo wcspbrk.lo wcsrchr.lo wcsspn.lo \
> -@USE_LIBTOOL_TRUE@wcsstr.lo wmemchr.lo wmemcmp.lo wmemcpy.lo wmemmove.lo \
> -@USE_LIBTOOL_TRUE@wmemset.lo
> +@USE_LIBTOOL_TRUE@wcsstr.lo wcswidth.lo wcwidth.lo wmemchr.lo wmemcmp.lo \
> +@USE_LIBTOOL_TRUE@wmemcpy.lo wmemmove.lo wmemset.lo
> CFLAGS = @CFLAGS@
> COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
> LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
> Index: libc/string/wcstrings.tex
> ===================================================================
> RCS file: /cvs/src/src/newlib/libc/string/wcstrings.tex,v
> retrieving revision 1.2
> diff -p -u -r1.2 wcstrings.tex
> --- libc/string/wcstrings.tex 3 Apr 2003 08:26:07 -0000 1.2
> +++ libc/string/wcstrings.tex 3 Apr 2003 13:35:53 -0000
> @@ -27,6 +27,8 @@ declarations are in @file{wchar.h}.
> * wcsrchr:: Reverse search for wide-character in string
> * wcsspn:: Find initial match in wide-character string
> * wcsstr:: Find wide-character string segment
> +* wcswidth:: Number of column positions of a wide-character string
> +* wcwidth:: Number of column positions of a wide-character code
> @end menu
>
> @page
> @@ -91,4 +93,10 @@ declarations are in @file{wchar.h}.
>
> @page
> @include string/wcsstr.def
> +
> +@page
> +@include string/wcswidth.def
> +
> +@page
> +@include string/wcwidth.def
>
> Index: libc/string/wcswidth.c
> ===================================================================
> RCS file: libc/string/wcswidth.c
> diff -N libc/string/wcswidth.c
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ libc/string/wcswidth.c 3 Apr 2003 13:35:53 -0000
> @@ -0,0 +1,56 @@
> +/*
> +FUNCTION
> + <<wcswidth>>---number of column positions of a wide-character string
> +
> +INDEX
> + wcswidth
> +
> +ANSI_SYNOPSIS
> + #include <wchar.h>
> + int wcswidth(const wchar_t *<[pwcs]>, size_t <[n]>);
> +
> +TRAD_SYNOPSIS
> + #include <wchar.h>
> + int wcswidth(<[pwcs]>, <[n]>)
> + wchar_t *<[wc]>;
> + size_t <[n]>;
> +
> +DESCRIPTION
> + The <<wcswidth>> function shall determine the number of column
> + positions required for n wide-character codes (or fewer than n
> + wide-character codes if a null wide-character code is encountered
> + before n wide-character codes are exhausted) in the string pointed
> + to by pwcs.
> +
> +RETURNS
> + The <<wcswidth>> function either shall return 0 (if pwcs points to a
> + null wide-character code), or return the number of column positions
> + to be occupied by the wide-character string pointed to by pwcs, or
> + return -1 (if any of the first n wide-character codes in the
> + wide-character string pointed to by pwcs is not a printable
> + wide-character code).
> +
> +PORTABILITY
> +<<wcswidth>> has been introduced in the Single UNIX Specification Volume 2
> +<<wcswidth>> has been marked as extension in Single UNIX Specification Volume 3
> +*/
> +
> +#include <_ansi.h>
> +#include <wchar.h>
> +
> +int
> +_DEFUN (wcswidth, (pwcs, n),
> + _CONST wchar_t *pwcs _AND
> + size_t n)
> +
> +{
> + int w, len = 0;
> + if (!pwcs || n == 0)
> + return 0;
> + do {
> + if ((w = wcwidth (*pwcs)) < 0)
> + return -1;
> + len += w;
> + } while (*pwcs++ && --n > 0);
> + return len;
> +}
> Index: libc/string/wcwidth.c
> ===================================================================
> RCS file: libc/string/wcwidth.c
> diff -N libc/string/wcwidth.c
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ libc/string/wcwidth.c 3 Apr 2003 13:35:53 -0000
> @@ -0,0 +1,53 @@
> +/*
> +FUNCTION
> + <<wcwidth>>---number of column positions of a wide-character code
> +
> +INDEX
> + wcwidth
> +
> +ANSI_SYNOPSIS
> + #include <wchar.h>
> + int wcwidth(const wchar_t <[wc]>);
> +
> +TRAD_SYNOPSIS
> + #include <wchar.h>
> + int wcwidth(<[wc]>)
> + wchar_t *<[wc]>;
> +
> +DESCRIPTION
> + The <<wcwidth>> function shall determine the number of column
> + positions required for the wide character wc. The application
> + shall ensure that the value of wc is a character representable
> + as a wchar_t, and is a wide-character code corresponding to a
> + valid character in the current locale.
> +
> +RETURNS
> + The <<wcwidth>> function shall either return 0 (if wc is a null
> + wide-character code), or return the number of column positions to
> + be occupied by the wide-character code wc, or return -1 (if wc
> + does not correspond to a printable wide-character code).
> +
> + The current implementation of <<wcwidth>> simply sets the width
> + of all printable characters to 1 since newlib has no character
> + tables around.
> +
> +PORTABILITY
> +<<wcwidth>> has been introduced in the Single UNIX Specification Volume 2
> +<<wcwidth>> has been marked as extension in Single UNIX Specification Volume 3
> +*/
> +
> +#include <_ansi.h>
> +#include <wchar.h>
> +#include <wctype.h>
> +
> +int
> +_DEFUN (wcwidth, (wc),
> + _CONST wchar_t wc)
> +
> +{
> + if (iswprint (wc))
> + return 1;
> + if (iswcntrl (wc) || wc == L'\0')
> + return 0;
> + return -1;
> +}
>
More information about the Newlib
mailing list