(mbsnwidth): Don't loop endlessly when called with an
[gnulib.git] / lib / mbswidth.c
1 /* Determine the number of screen columns needed for a string.
2    Copyright (C) 2000 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 /* Get MB_LEN_MAX.  */
25 #if HAVE_LIMITS_H
26 # include <limits.h>
27 #endif
28
29 /* Get MB_CUR_MAX.  */
30 #if HAVE_STDLIB_H
31 # include <stdlib.h>
32 #endif
33
34 #if HAVE_STRING_H
35 # include <string.h>
36 #endif
37
38 /* Get isprint().  */
39 #include <ctype.h>
40
41 /* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth().  */
42 #if HAVE_WCHAR_H
43 # include <wchar.h>
44 #endif
45
46 /* Get iswprint().  */
47 #if HAVE_WCTYPE_H
48 # include <wctype.h>
49 #endif
50 #if !defined iswprint && !HAVE_ISWPRINT
51 # define iswprint(wc) 1
52 #endif
53
54 /* Some systems, like BeOS, have multibyte encodings but lack mbstate_t.  */
55 #if HAVE_MBRTOWC && defined mbstate_t
56 # define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
57 # define mbsinit(ps) 1
58 #endif
59
60 #ifndef HAVE_DECL_WCWIDTH
61 "this configure-time declaration test was not run"
62 #endif
63 #if !HAVE_DECL_WCWIDTH
64 int wcwidth ();
65 #endif
66
67 #ifndef wcwidth
68 # if !HAVE_WCWIDTH
69 /* wcwidth doesn't exist, so assume all printable characters have
70    width 1.  */
71 #  define wcwidth(wc) ((wc) == 0 ? 0 : iswprint (wc) ? 1 : -1)
72 # endif
73 #endif
74
75 /* Get ISPRINT.  */
76 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
77 # define IN_CTYPE_DOMAIN(c) 1
78 #else
79 # define IN_CTYPE_DOMAIN(c) isascii(c)
80 #endif
81 /* Undefine to protect against the definition in wctype.h of solaris2.6.   */
82 #undef ISPRINT
83 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
84
85 #include "mbswidth.h"
86
87 /* Returns the number of columns needed to represent the multibyte
88    character string pointed to by STRING.  If a non-printable character
89    occurs, -1 is returned, unless MBSW_ACCEPT_UNPRINTABLE is specified.
90    With flags = 0, this is the multibyte analogon of the wcswidth function.  */
91 int
92 mbswidth (const char *string, int flags)
93 {
94   return mbsnwidth (string, strlen (string), flags);
95 }
96
97 /* Returns the number of columns needed to represent the multibyte
98    character string pointed to by STRING of length NBYTES.  If a
99    non-printable character occurs, -1 is returned, unless
100    MBSW_ACCEPT_UNPRINTABLE is specified.  */
101 int
102 mbsnwidth (const char *string, size_t nbytes, int flags)
103 {
104   const char *p = string;
105   const char *plimit = p + nbytes;
106   int width;
107
108   width = 0;
109 #if HAVE_MBRTOWC && (MB_LEN_MAX > 1)
110   if (MB_CUR_MAX > 1)
111     {
112       while (p < plimit)
113         switch (*p)
114           {
115             case ' ': case '!': case '"': case '#': case '%':
116             case '&': case '\'': case '(': case ')': case '*':
117             case '+': case ',': case '-': case '.': case '/':
118             case '0': case '1': case '2': case '3': case '4':
119             case '5': case '6': case '7': case '8': case '9':
120             case ':': case ';': case '<': case '=': case '>':
121             case '?':
122             case 'A': case 'B': case 'C': case 'D': case 'E':
123             case 'F': case 'G': case 'H': case 'I': case 'J':
124             case 'K': case 'L': case 'M': case 'N': case 'O':
125             case 'P': case 'Q': case 'R': case 'S': case 'T':
126             case 'U': case 'V': case 'W': case 'X': case 'Y':
127             case 'Z':
128             case '[': case '\\': case ']': case '^': case '_':
129             case 'a': case 'b': case 'c': case 'd': case 'e':
130             case 'f': case 'g': case 'h': case 'i': case 'j':
131             case 'k': case 'l': case 'm': case 'n': case 'o':
132             case 'p': case 'q': case 'r': case 's': case 't':
133             case 'u': case 'v': case 'w': case 'x': case 'y':
134             case 'z': case '{': case '|': case '}': case '~':
135               /* These characters are printable ASCII characters.  */
136               p++;
137               width++;
138               break;
139             default:
140               /* If we have a multibyte sequence, scan it up to its end.  */
141               {
142                 mbstate_t mbstate;
143                 memset (&mbstate, 0, sizeof mbstate);
144                 do
145                   {
146                     wchar_t wc;
147                     size_t bytes;
148                     int w;
149
150                     bytes = mbrtowc (&wc, p, plimit - p, &mbstate);
151
152                     if (bytes == (size_t) -1)
153                       /* An invalid multibyte sequence was encountered.  */
154                       {
155                         if (flags & MBSW_ACCEPT_INVALID)
156                           {
157                             p++;
158                             width++;
159                             break;
160                           }
161                         else
162                           return -1;
163                       }
164
165                     if (bytes == (size_t) -2)
166                       /* An incomplete multibyte character at the end.  */
167                       {
168                         if (flags & MBSW_ACCEPT_INVALID)
169                           {
170                             p = plimit;
171                             width++;
172                             break;
173                           }
174                         else
175                           return -1;
176                       }
177
178                     if (bytes == 0)
179                       /* A null wide character was encountered.  */
180                       bytes = 1;
181
182                     w = wcwidth (wc);
183                     if (w >= 0)
184                       /* A printable multibyte character.  */
185                       width += w;
186                     else
187                       /* An unprintable multibyte character.  */
188                       if (flags & MBSW_ACCEPT_UNPRINTABLE)
189                         width += 1;
190                       else
191                         return -1;
192
193                     p += bytes;
194                   }
195                 while (! mbsinit (&mbstate));
196               }
197               break;
198           }
199       return width;
200     }
201 #endif
202
203   while (p < plimit)
204     {
205       unsigned char c = (unsigned char) *p++;
206
207       if ((flags & MBSW_ACCEPT_UNPRINTABLE) || ISPRINT (c))
208         width++;
209       else
210         return -1;
211     }
212   return width;
213 }