Remove the wcwidth.h file. Move wcwidth's declaration to wchar_.h.
[gnulib.git] / lib / mbswidth.c
1 /* Determine the number of screen columns needed for a string.
2    Copyright (C) 2000-2007 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 #include <config.h>
21
22 /* Specification.  */
23 #include "mbswidth.h"
24
25 /* Get MB_CUR_MAX.  */
26 #include <stdlib.h>
27
28 #include <string.h>
29
30 /* Get isprint().  */
31 #include <ctype.h>
32
33 /* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth().  */
34 #include <wchar.h>
35
36 /* Get iswcntrl().  */
37 #include <wctype.h>
38
39 #ifndef mbsinit
40 # if !HAVE_MBSINIT
41 #  define mbsinit(ps) 1
42 # endif
43 #endif
44
45 /* Returns the number of columns needed to represent the multibyte
46    character string pointed to by STRING.  If a non-printable character
47    occurs, and MBSW_REJECT_UNPRINTABLE is specified, -1 is returned.
48    With flags = MBSW_REJECT_INVALID | MBSW_REJECT_UNPRINTABLE, this is
49    the multibyte analogue of the wcswidth function.
50    If STRING is not of length < INT_MAX / 2, integer overflow can occur.  */
51 int
52 mbswidth (const char *string, int flags)
53 {
54   return mbsnwidth (string, strlen (string), flags);
55 }
56
57 /* Returns the number of columns needed to represent the multibyte
58    character string pointed to by STRING of length NBYTES.  If a
59    non-printable character occurs, and MBSW_REJECT_UNPRINTABLE is
60    specified, -1 is returned.
61    If NBYTES is not < INT_MAX / 2, integer overflow can occur.  */
62 int
63 mbsnwidth (const char *string, size_t nbytes, int flags)
64 {
65   const char *p = string;
66   const char *plimit = p + nbytes;
67   int width;
68
69   width = 0;
70 #if HAVE_MBRTOWC
71   if (MB_CUR_MAX > 1)
72     {
73       while (p < plimit)
74         switch (*p)
75           {
76             case ' ': case '!': case '"': case '#': case '%':
77             case '&': case '\'': case '(': case ')': case '*':
78             case '+': case ',': case '-': case '.': case '/':
79             case '0': case '1': case '2': case '3': case '4':
80             case '5': case '6': case '7': case '8': case '9':
81             case ':': case ';': case '<': case '=': case '>':
82             case '?':
83             case 'A': case 'B': case 'C': case 'D': case 'E':
84             case 'F': case 'G': case 'H': case 'I': case 'J':
85             case 'K': case 'L': case 'M': case 'N': case 'O':
86             case 'P': case 'Q': case 'R': case 'S': case 'T':
87             case 'U': case 'V': case 'W': case 'X': case 'Y':
88             case 'Z':
89             case '[': case '\\': case ']': case '^': case '_':
90             case 'a': case 'b': case 'c': case 'd': case 'e':
91             case 'f': case 'g': case 'h': case 'i': case 'j':
92             case 'k': case 'l': case 'm': case 'n': case 'o':
93             case 'p': case 'q': case 'r': case 's': case 't':
94             case 'u': case 'v': case 'w': case 'x': case 'y':
95             case 'z': case '{': case '|': case '}': case '~':
96               /* These characters are printable ASCII characters.  */
97               p++;
98               width++;
99               break;
100             default:
101               /* If we have a multibyte sequence, scan it up to its end.  */
102               {
103                 mbstate_t mbstate;
104                 memset (&mbstate, 0, sizeof mbstate);
105                 do
106                   {
107                     wchar_t wc;
108                     size_t bytes;
109                     int w;
110
111                     bytes = mbrtowc (&wc, p, plimit - p, &mbstate);
112
113                     if (bytes == (size_t) -1)
114                       /* An invalid multibyte sequence was encountered.  */
115                       {
116                         if (!(flags & MBSW_REJECT_INVALID))
117                           {
118                             p++;
119                             width++;
120                             break;
121                           }
122                         else
123                           return -1;
124                       }
125
126                     if (bytes == (size_t) -2)
127                       /* An incomplete multibyte character at the end.  */
128                       {
129                         if (!(flags & MBSW_REJECT_INVALID))
130                           {
131                             p = plimit;
132                             width++;
133                             break;
134                           }
135                         else
136                           return -1;
137                       }
138
139                     if (bytes == 0)
140                       /* A null wide character was encountered.  */
141                       bytes = 1;
142
143                     w = wcwidth (wc);
144                     if (w >= 0)
145                       /* A printable multibyte character.  */
146                       width += w;
147                     else
148                       /* An unprintable multibyte character.  */
149                       if (!(flags & MBSW_REJECT_UNPRINTABLE))
150                         width += (iswcntrl (wc) ? 0 : 1);
151                       else
152                         return -1;
153
154                     p += bytes;
155                   }
156                 while (! mbsinit (&mbstate));
157               }
158               break;
159           }
160       return width;
161     }
162 #endif
163
164   while (p < plimit)
165     {
166       unsigned char c = (unsigned char) *p++;
167
168       if (isprint (c))
169         width++;
170       else if (!(flags & MBSW_REJECT_UNPRINTABLE))
171         width += (iscntrl (c) ? 0 : 1);
172       else
173         return -1;
174     }
175   return width;
176 }