Fixes after wcwidth module creation.
[gnulib.git] / lib / mbswidth.c
1 /* Determine the number of screen columns needed for a string.
2    Copyright (C) 2000-2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <haible@clisp.cons.org>.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 /* Specification.  */
25 #include "mbswidth.h"
26
27 /* Get MB_CUR_MAX.  */
28 #include <stdlib.h>
29
30 #include <string.h>
31
32 /* Get isprint().  */
33 #include <ctype.h>
34
35 /* Get mbstate_t, mbrtowc(), mbsinit().  */
36 #if HAVE_WCHAR_H
37 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
38    <wchar.h>.
39    BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
40    <wchar.h>.  */
41 # include <stdio.h>
42 # include <time.h>
43 # include <wchar.h>
44 #endif
45
46 /* Get wcwidth().  */
47 #include "wcwidth.h"
48
49 /* Get iswcntrl().  */
50 #if HAVE_WCTYPE_H
51 # include <wctype.h>
52 #endif
53 #if !defined iswcntrl && !HAVE_ISWCNTRL
54 # define iswcntrl(wc) 0
55 #endif
56
57 #ifndef mbsinit
58 # if !HAVE_MBSINIT
59 #  define mbsinit(ps) 1
60 # endif
61 #endif
62
63 /* Get ISPRINT.  */
64 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
65 # define IN_CTYPE_DOMAIN(c) 1
66 #else
67 # define IN_CTYPE_DOMAIN(c) isascii(c)
68 #endif
69 /* Undefine to protect against the definition in wctype.h of Solaris 2.6.   */
70 #undef ISPRINT
71 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
72 #undef ISCNTRL
73 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
74
75 /* Returns the number of columns needed to represent the multibyte
76    character string pointed to by STRING.  If a non-printable character
77    occurs, and MBSW_REJECT_UNPRINTABLE is specified, -1 is returned.
78    With flags = MBSW_REJECT_INVALID | MBSW_REJECT_UNPRINTABLE, this is
79    the multibyte analogue of the wcswidth function.
80    If STRING is not of length < INT_MAX / 2, integer overflow can occur.  */
81 int
82 mbswidth (const char *string, int flags)
83 {
84   return mbsnwidth (string, strlen (string), flags);
85 }
86
87 /* Returns the number of columns needed to represent the multibyte
88    character string pointed to by STRING of length NBYTES.  If a
89    non-printable character occurs, and MBSW_REJECT_UNPRINTABLE is
90    specified, -1 is returned.
91    If NBYTES is not < INT_MAX / 2, integer overflow can occur.  */
92 int
93 mbsnwidth (const char *string, size_t nbytes, int flags)
94 {
95   const char *p = string;
96   const char *plimit = p + nbytes;
97   int width;
98
99   width = 0;
100 #if HAVE_MBRTOWC
101   if (MB_CUR_MAX > 1)
102     {
103       while (p < plimit)
104         switch (*p)
105           {
106             case ' ': case '!': case '"': case '#': case '%':
107             case '&': case '\'': case '(': case ')': case '*':
108             case '+': case ',': case '-': case '.': case '/':
109             case '0': case '1': case '2': case '3': case '4':
110             case '5': case '6': case '7': case '8': case '9':
111             case ':': case ';': case '<': case '=': case '>':
112             case '?':
113             case 'A': case 'B': case 'C': case 'D': case 'E':
114             case 'F': case 'G': case 'H': case 'I': case 'J':
115             case 'K': case 'L': case 'M': case 'N': case 'O':
116             case 'P': case 'Q': case 'R': case 'S': case 'T':
117             case 'U': case 'V': case 'W': case 'X': case 'Y':
118             case 'Z':
119             case '[': case '\\': case ']': case '^': case '_':
120             case 'a': case 'b': case 'c': case 'd': case 'e':
121             case 'f': case 'g': case 'h': case 'i': case 'j':
122             case 'k': case 'l': case 'm': case 'n': case 'o':
123             case 'p': case 'q': case 'r': case 's': case 't':
124             case 'u': case 'v': case 'w': case 'x': case 'y':
125             case 'z': case '{': case '|': case '}': case '~':
126               /* These characters are printable ASCII characters.  */
127               p++;
128               width++;
129               break;
130             default:
131               /* If we have a multibyte sequence, scan it up to its end.  */
132               {
133                 mbstate_t mbstate;
134                 memset (&mbstate, 0, sizeof mbstate);
135                 do
136                   {
137                     wchar_t wc;
138                     size_t bytes;
139                     int w;
140
141                     bytes = mbrtowc (&wc, p, plimit - p, &mbstate);
142
143                     if (bytes == (size_t) -1)
144                       /* An invalid multibyte sequence was encountered.  */
145                       {
146                         if (!(flags & MBSW_REJECT_INVALID))
147                           {
148                             p++;
149                             width++;
150                             break;
151                           }
152                         else
153                           return -1;
154                       }
155
156                     if (bytes == (size_t) -2)
157                       /* An incomplete multibyte character at the end.  */
158                       {
159                         if (!(flags & MBSW_REJECT_INVALID))
160                           {
161                             p = plimit;
162                             width++;
163                             break;
164                           }
165                         else
166                           return -1;
167                       }
168
169                     if (bytes == 0)
170                       /* A null wide character was encountered.  */
171                       bytes = 1;
172
173                     w = wcwidth (wc);
174                     if (w >= 0)
175                       /* A printable multibyte character.  */
176                       width += w;
177                     else
178                       /* An unprintable multibyte character.  */
179                       if (!(flags & MBSW_REJECT_UNPRINTABLE))
180                         width += (iswcntrl (wc) ? 0 : 1);
181                       else
182                         return -1;
183
184                     p += bytes;
185                   }
186                 while (! mbsinit (&mbstate));
187               }
188               break;
189           }
190       return width;
191     }
192 #endif
193
194   while (p < plimit)
195     {
196       unsigned char c = (unsigned char) *p++;
197
198       if (ISPRINT (c))
199         width++;
200       else if (!(flags & MBSW_REJECT_UNPRINTABLE))
201         width += (ISCNTRL (c) ? 0 : 1);
202       else
203         return -1;
204     }
205   return width;
206 }