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