projects
/
gnulib.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Include <stdlib.h>.
[gnulib.git]
/
lib
/
mbswidth.c
diff --git
a/lib/mbswidth.c
b/lib/mbswidth.c
index
d187bc0
..
c6f91db
100644
(file)
--- a/
lib/mbswidth.c
+++ b/
lib/mbswidth.c
@@
-1,5
+1,5
@@
/* Determine the number of screen columns needed for a string.
/* Determine the number of screen columns needed for a string.
- Copyright (C) 2000 Free Software Foundation, Inc.
+ Copyright (C) 2000
-2002
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
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,13
@@
# include <config.h>
#endif
# include <config.h>
#endif
-/* Get MB_LEN_MAX. */
-#if HAVE_LIMITS_H
-# include <limits.h>
-#endif
+/* Specification. */
+#include "mbswidth.h"
/* Get MB_CUR_MAX. */
/* Get MB_CUR_MAX. */
-#if HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
+#include <stdlib.h>
-#if HAVE_STRING_H
-# include <string.h>
-#endif
+#include <string.h>
/* Get isprint(). */
#include <ctype.h>
/* Get isprint(). */
#include <ctype.h>
@@
-43,18
+37,21
@@
# include <wchar.h>
#endif
# include <wchar.h>
#endif
-/* Get iswprint(). */
+/* Get iswprint()
, iswcntrl()
. */
#if HAVE_WCTYPE_H
# include <wctype.h>
#endif
#if !defined iswprint && !HAVE_ISWPRINT
# define iswprint(wc) 1
#endif
#if HAVE_WCTYPE_H
# include <wctype.h>
#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
#endif
#ifndef HAVE_DECL_WCWIDTH
@@
-74,22
+71,21
@@
int wcwidth ();
/* Get ISPRINT. */
#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
/* Get ISPRINT. */
#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
-/* Undefine to protect against the definition in wctype.h of solaris2.6. */
-# undef ISASCII
-# define ISASCII(c) 1
+# define IN_CTYPE_DOMAIN(c) 1
#else
#else
-# define I
SASCII(c) isascii
(c)
+# define I
N_CTYPE_DOMAIN(c) isascii
(c)
#endif
#endif
-/* Undefine to protect against the definition in wctype.h of
solaris
2.6. */
+/* Undefine to protect against the definition in wctype.h of
Solaris
2.6. */
#undef ISPRINT
#undef ISPRINT
-#define ISPRINT(c) (I
SASCII
(c) && isprint (c))
-
-#
include "mbswidth.h"
+#define ISPRINT(c) (I
N_CTYPE_DOMAIN
(c) && isprint (c))
+#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
/* 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)
{
int
mbswidth (const char *string, int flags)
{
@@
-98,8
+94,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
/* 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, unles
s
-
MBSW_ACCEPT_UNPRINTABLE is specifi
ed. */
+ non-printable character occurs,
and MBSW_REJECT_UNPRINTABLE i
s
+
specified, -1 is return
ed. */
int
mbsnwidth (const char *string, size_t nbytes, int flags)
{
int
mbsnwidth (const char *string, size_t nbytes, int flags)
{
@@
-108,7
+104,7
@@
mbsnwidth (const char *string, size_t nbytes, int flags)
int width;
width = 0;
int width;
width = 0;
-#if HAVE_MBRTOWC
&& (MB_LEN_MAX > 1)
+#if HAVE_MBRTOWC
if (MB_CUR_MAX > 1)
{
while (p < plimit)
if (MB_CUR_MAX > 1)
{
while (p < plimit)
@@
-154,8
+150,12
@@
mbsnwidth (const char *string, size_t nbytes, int flags)
if (bytes == (size_t) -1)
/* An invalid multibyte sequence was encountered. */
{
if (bytes == (size_t) -1)
/* An invalid multibyte sequence was encountered. */
{
- if (flags & MBSW_ACCEPT_INVALID)
- break;
+ if (!(flags & MBSW_REJECT_INVALID))
+ {
+ p++;
+ width++;
+ break;
+ }
else
return -1;
}
else
return -1;
}
@@
-163,8
+163,12
@@
mbsnwidth (const char *string, size_t nbytes, int flags)
if (bytes == (size_t) -2)
/* An incomplete multibyte character at the end. */
{
if (bytes == (size_t) -2)
/* An incomplete multibyte character at the end. */
{
- if (flags & MBSW_ACCEPT_INVALID)
- break;
+ if (!(flags & MBSW_REJECT_INVALID))
+ {
+ p = plimit;
+ width++;
+ break;
+ }
else
return -1;
}
else
return -1;
}
@@
-179,8
+183,8
@@
mbsnwidth (const char *string, size_t nbytes, int flags)
width += w;
else
/* An unprintable multibyte character. */
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;
else
return -1;
@@
-198,8
+202,10
@@
mbsnwidth (const char *string, size_t nbytes, int flags)
{
unsigned char c = (unsigned char) *p++;
{
unsigned char c = (unsigned char) *p++;
- if (
(flags & MBSW_ACCEPT_UNPRINTABLE) ||
ISPRINT (c))
+ if (ISPRINT (c))
width++;
width++;
+ else if (!(flags & MBSW_REJECT_UNPRINTABLE))
+ width += (ISCNTRL (c) ? 0 : 1);
else
return -1;
}
else
return -1;
}