X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fmbswidth.c;h=69a801ceaed6350681e0fd117b821e06525993da;hb=488cf84c63fbbffa44143d0ebf56e5a6c26d4383;hp=bb6aa1a6d8e83587591ca558483f212d371be947;hpb=1dce3d25ed05cce7a562b231025ff64697b310d1;p=gnulib.git diff --git a/lib/mbswidth.c b/lib/mbswidth.c index bb6aa1a6d..69a801cea 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-2001 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,19 +21,10 @@ # include #endif -/* Get MB_LEN_MAX. */ -#if HAVE_LIMITS_H -# include -#endif - /* Get MB_CUR_MAX. */ -#if HAVE_STDLIB_H -# include -#endif +#include -#if HAVE_STRING_H -# include -#endif +#include /* Get isprint(). */ #include @@ -43,18 +34,21 @@ # 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 -/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */ -#if HAVE_MBRTOWC && defined mbstate_t -# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0) -# define mbsinit(ps) 1 +#ifndef mbsinit +# if !HAVE_MBSINIT +# define mbsinit(ps) 1 +# endif #endif #ifndef HAVE_DECL_WCWIDTH @@ -81,32 +75,40 @@ int wcwidth (); /* Undefine to protect against the definition in wctype.h of solaris2.6. */ #undef ISPRINT #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c)) +#undef ISCNTRL +#define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c)) #include "mbswidth.h" /* 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 analogon of the wcswidth function. */ int -mbswidth (const char *string, int flags) +mbswidth (string, flags) + const char *string; + int flags; { return mbsnwidth (string, strlen (string), 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) +mbsnwidth (string, nbytes, flags) + const char *string; + size_t nbytes; + int flags; { const char *p = string; const char *plimit = p + nbytes; int width; width = 0; -#if HAVE_MBRTOWC && (MB_LEN_MAX > 1) +#if HAVE_MBRTOWC if (MB_CUR_MAX > 1) { while (p < plimit) @@ -152,7 +154,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++; @@ -165,7 +167,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++; @@ -185,8 +187,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; @@ -204,8 +206,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; }