X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fmbswidth.c;h=a51176026da466c6b47a17a67ac924836c562cb8;hb=bae84310eb40472530384f6d5c7738f5dc03a62d;hp=208a629d34f1fe93c0ee55c0211466f3ea5eebad;hpb=db5283c74954c09ce38522191517e8fef4c57a3c;p=gnulib.git diff --git a/lib/mbswidth.c b/lib/mbswidth.c index 208a629d3..a51176026 100644 --- a/lib/mbswidth.c +++ b/lib/mbswidth.c @@ -1,5 +1,5 @@ /* Determine the number of screen columns needed for a string. - Copyright (C) 2000 Free Software Foundation, Inc. + Copyright (C) 2000-2004 Free Software Foundation, Inc. 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 @@ -21,35 +21,44 @@ # include #endif -/* Get MB_LEN_MAX. */ -#if HAVE_LIMITS_H -# include -#endif +/* Specification. */ +#include "mbswidth.h" /* Get MB_CUR_MAX. */ -#if HAVE_STDLIB_H -# include -#endif +#include -#if HAVE_STRING_H -# include -#endif +#include /* Get isprint(). */ #include /* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth(). */ #if HAVE_WCHAR_H +/* Tru64 with Desktop Toolkit C has a bug: must be included before + . + BSD/OS 4.1 has a bug: and must be included before + . */ +# include +# include # include #endif -/* Get iswprint(). */ +/* Get iswprint(), iswcntrl(). */ #if HAVE_WCTYPE_H # include #endif #if !defined iswprint && !HAVE_ISWPRINT # define iswprint(wc) 1 #endif +#if !defined iswcntrl && !HAVE_ISWCNTRL +# define iswcntrl(wc) 0 +#endif + +#ifndef mbsinit +# if !HAVE_MBSINIT +# define mbsinit(ps) 1 +# endif +#endif #ifndef HAVE_DECL_WCWIDTH "this configure-time declaration test was not run" @@ -72,16 +81,17 @@ int wcwidth (); #else # define IN_CTYPE_DOMAIN(c) isascii(c) #endif -/* Undefine to protect against the definition in wctype.h of solaris2.6. */ +/* Undefine to protect against the definition in wctype.h of Solaris 2.6. */ #undef ISPRINT #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c)) - -#include "mbswidth.h" +#undef ISCNTRL +#define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c)) /* Returns the number of columns needed to represent the multibyte character string pointed to by STRING. If a non-printable character - occurs, -1 is returned, unless MBSW_ACCEPT_UNPRINTABLE is specified. - With flags = 0, this is the multibyte analogon of the wcswidth function. */ + occurs, and MBSW_REJECT_UNPRINTABLE is specified, -1 is returned. + With flags = MBSW_REJECT_INVALID | MBSW_REJECT_UNPRINTABLE, this is + the multibyte analogue of the wcswidth function. */ int mbswidth (const char *string, int flags) { @@ -90,8 +100,8 @@ mbswidth (const char *string, int flags) /* Returns the number of columns needed to represent the multibyte character string pointed to by STRING of length NBYTES. If a - non-printable character occurs, -1 is returned, unless - MBSW_ACCEPT_UNPRINTABLE is specified. */ + non-printable character occurs, and MBSW_REJECT_UNPRINTABLE is + specified, -1 is returned. */ int mbsnwidth (const char *string, size_t nbytes, int flags) { @@ -100,7 +110,7 @@ mbsnwidth (const char *string, size_t nbytes, int flags) int width; width = 0; -#if HAVE_MBRTOWC && (MB_LEN_MAX > 1) +#if HAVE_MBRTOWC if (MB_CUR_MAX > 1) { while (p < plimit) @@ -146,7 +156,7 @@ mbsnwidth (const char *string, size_t nbytes, int flags) if (bytes == (size_t) -1) /* An invalid multibyte sequence was encountered. */ { - if (flags & MBSW_ACCEPT_INVALID) + if (!(flags & MBSW_REJECT_INVALID)) { p++; width++; @@ -159,7 +169,7 @@ mbsnwidth (const char *string, size_t nbytes, int flags) if (bytes == (size_t) -2) /* An incomplete multibyte character at the end. */ { - if (flags & MBSW_ACCEPT_INVALID) + if (!(flags & MBSW_REJECT_INVALID)) { p = plimit; width++; @@ -179,8 +189,8 @@ mbsnwidth (const char *string, size_t nbytes, int flags) width += w; else /* An unprintable multibyte character. */ - if (flags & MBSW_ACCEPT_UNPRINTABLE) - width += 1; + if (!(flags & MBSW_REJECT_UNPRINTABLE)) + width += (iswcntrl (wc) ? 0 : 1); else return -1; @@ -198,8 +208,10 @@ mbsnwidth (const char *string, size_t nbytes, int flags) { unsigned char c = (unsigned char) *p++; - if ((flags & MBSW_ACCEPT_UNPRINTABLE) || ISPRINT (c)) + if (ISPRINT (c)) width++; + else if (!(flags & MBSW_REJECT_UNPRINTABLE)) + width += (ISCNTRL (c) ? 0 : 1); else return -1; }