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