annotate lib/mbswidth.c @ 17363:5a51fb7777a9

sys_select, sys_time: port 2013-01-30 Solaris 2.6 fix to Cygwin Problem reported by Marco Atzeri in <http://lists.gnu.org/archive/html/bug-gnulib/2013-03/msg00000.html>. * lib/sys_select.in.h [HAVE_SYS_SELECT_H && _CYGWIN_SYS_TIME_H]: Simply delegate to the system <sys/select.h> in this case too. Also, pay attention to _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TIME_H only if OSF/1, since otherwise Cygwin breaks, and it doesn't seem to be needed on Solaris either. * lib/sys_time.in.h [_CYGWIN_SYS_TIME_H]: Simply delgate to the system <sys/time.h> in this case.
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 19 Mar 2013 09:08:47 -0700
parents e542fd46ad6f
children 344018b6e5d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
1 /* Determine the number of screen columns needed for a string.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16201
diff changeset
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9073
diff changeset
4 This program is free software: you can redistribute it and/or modify
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9073
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9073
diff changeset
7 (at your option) any later version.
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
8
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
12 GNU General Public License for more details.
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
13
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9073
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
16
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
17 /* Written by Bruno Haible <haible@clisp.cons.org>. */
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
18
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 7058
diff changeset
19 #include <config.h>
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
20
3979
7627246c7a83 Standard specification idiom. Make mbswidth.h self contained.
Bruno Haible <bruno@clisp.org>
parents: 3978
diff changeset
21 /* Specification. */
7627246c7a83 Standard specification idiom. Make mbswidth.h self contained.
Bruno Haible <bruno@clisp.org>
parents: 3978
diff changeset
22 #include "mbswidth.h"
7627246c7a83 Standard specification idiom. Make mbswidth.h self contained.
Bruno Haible <bruno@clisp.org>
parents: 3978
diff changeset
23
3326
591c68289197 Don't include <limits.h>.
Jim Meyering <jim@meyering.net>
parents: 3174
diff changeset
24 /* Get MB_CUR_MAX. */
591c68289197 Don't include <limits.h>.
Jim Meyering <jim@meyering.net>
parents: 3174
diff changeset
25 #include <stdlib.h>
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
26
3326
591c68289197 Don't include <limits.h>.
Jim Meyering <jim@meyering.net>
parents: 3174
diff changeset
27 #include <string.h>
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
28
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
29 /* Get isprint(). */
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
30 #include <ctype.h>
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
31
9073
eff03f4ce262 Remove the wcwidth.h file. Move wcwidth's declaration to wchar_.h.
Bruno Haible <bruno@clisp.org>
parents: 7888
diff changeset
32 /* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth(). */
7888
b6376840b47b * modules/fnmatch (Depends-on): Depend on wchar.
Eric Blake <ebb9@byu.net>
parents: 7758
diff changeset
33 #include <wchar.h>
6882
6f91a91ac710 Fixes after wcwidth module creation.
Bruno Haible <bruno@clisp.org>
parents: 6877
diff changeset
34
6877
8a1cdc7197a8 * mbchar.h (wcwidth): Include wcwidth.h.
Eric Blake <ebb9@byu.net>
parents: 5848
diff changeset
35 /* Get iswcntrl(). */
7758
ea1da95d3579 * lib/mbchar.h: Just include <wctype.h>; the wctype module
Paul Eggert <eggert@cs.ucla.edu>
parents: 7304
diff changeset
36 #include <wctype.h>
3326
591c68289197 Don't include <limits.h>.
Jim Meyering <jim@meyering.net>
parents: 3174
diff changeset
37
14418
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
38 /* Get INT_MAX. */
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
39 #include <limits.h>
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
40
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
41 /* Returns the number of columns needed to represent the multibyte
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
42 character string pointed to by STRING. If a non-printable character
3326
591c68289197 Don't include <limits.h>.
Jim Meyering <jim@meyering.net>
parents: 3174
diff changeset
43 occurs, and MBSW_REJECT_UNPRINTABLE is specified, -1 is returned.
591c68289197 Don't include <limits.h>.
Jim Meyering <jim@meyering.net>
parents: 3174
diff changeset
44 With flags = MBSW_REJECT_INVALID | MBSW_REJECT_UNPRINTABLE, this is
14418
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
45 the multibyte analogue of the wcswidth function. */
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
46 int
3978
55eb3920b607 Move to ANSI C.
Bruno Haible <bruno@clisp.org>
parents: 3326
diff changeset
47 mbswidth (const char *string, int flags)
2775
afd68d0b9fc1 (mbswidth): Add a flags argument.
Jim Meyering <jim@meyering.net>
parents: 2772
diff changeset
48 {
afd68d0b9fc1 (mbswidth): Add a flags argument.
Jim Meyering <jim@meyering.net>
parents: 2772
diff changeset
49 return mbsnwidth (string, strlen (string), flags);
afd68d0b9fc1 (mbswidth): Add a flags argument.
Jim Meyering <jim@meyering.net>
parents: 2772
diff changeset
50 }
afd68d0b9fc1 (mbswidth): Add a flags argument.
Jim Meyering <jim@meyering.net>
parents: 2772
diff changeset
51
afd68d0b9fc1 (mbswidth): Add a flags argument.
Jim Meyering <jim@meyering.net>
parents: 2772
diff changeset
52 /* Returns the number of columns needed to represent the multibyte
afd68d0b9fc1 (mbswidth): Add a flags argument.
Jim Meyering <jim@meyering.net>
parents: 2772
diff changeset
53 character string pointed to by STRING of length NBYTES. If a
3326
591c68289197 Don't include <limits.h>.
Jim Meyering <jim@meyering.net>
parents: 3174
diff changeset
54 non-printable character occurs, and MBSW_REJECT_UNPRINTABLE is
14418
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
55 specified, -1 is returned. */
2775
afd68d0b9fc1 (mbswidth): Add a flags argument.
Jim Meyering <jim@meyering.net>
parents: 2772
diff changeset
56 int
3978
55eb3920b607 Move to ANSI C.
Bruno Haible <bruno@clisp.org>
parents: 3326
diff changeset
57 mbsnwidth (const char *string, size_t nbytes, int flags)
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
58 {
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
59 const char *p = string;
2775
afd68d0b9fc1 (mbswidth): Add a flags argument.
Jim Meyering <jim@meyering.net>
parents: 2772
diff changeset
60 const char *plimit = p + nbytes;
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
61 int width;
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
62
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
63 width = 0;
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
64 if (MB_CUR_MAX > 1)
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
65 {
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
66 while (p < plimit)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
67 switch (*p)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
68 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
69 case ' ': case '!': case '"': case '#': case '%':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
70 case '&': case '\'': case '(': case ')': case '*':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
71 case '+': case ',': case '-': case '.': case '/':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
72 case '0': case '1': case '2': case '3': case '4':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
73 case '5': case '6': case '7': case '8': case '9':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
74 case ':': case ';': case '<': case '=': case '>':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
75 case '?':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
76 case 'A': case 'B': case 'C': case 'D': case 'E':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
77 case 'F': case 'G': case 'H': case 'I': case 'J':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
78 case 'K': case 'L': case 'M': case 'N': case 'O':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
79 case 'P': case 'Q': case 'R': case 'S': case 'T':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
80 case 'U': case 'V': case 'W': case 'X': case 'Y':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
81 case 'Z':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
82 case '[': case '\\': case ']': case '^': case '_':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
83 case 'a': case 'b': case 'c': case 'd': case 'e':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
84 case 'f': case 'g': case 'h': case 'i': case 'j':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
85 case 'k': case 'l': case 'm': case 'n': case 'o':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
86 case 'p': case 'q': case 'r': case 's': case 't':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
87 case 'u': case 'v': case 'w': case 'x': case 'y':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
88 case 'z': case '{': case '|': case '}': case '~':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
89 /* These characters are printable ASCII characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
90 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
91 width++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
92 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
93 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
94 /* If we have a multibyte sequence, scan it up to its end. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
95 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
96 mbstate_t mbstate;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
97 memset (&mbstate, 0, sizeof mbstate);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
98 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
99 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
100 wchar_t wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
101 size_t bytes;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
102 int w;
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
103
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
104 bytes = mbrtowc (&wc, p, plimit - p, &mbstate);
2775
afd68d0b9fc1 (mbswidth): Add a flags argument.
Jim Meyering <jim@meyering.net>
parents: 2772
diff changeset
105
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
106 if (bytes == (size_t) -1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
107 /* An invalid multibyte sequence was encountered. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
108 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
109 if (!(flags & MBSW_REJECT_INVALID))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
110 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
111 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
112 width++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
113 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
114 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
115 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
116 return -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
117 }
2775
afd68d0b9fc1 (mbswidth): Add a flags argument.
Jim Meyering <jim@meyering.net>
parents: 2772
diff changeset
118
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
119 if (bytes == (size_t) -2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
120 /* An incomplete multibyte character at the end. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
121 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
122 if (!(flags & MBSW_REJECT_INVALID))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
123 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
124 p = plimit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
125 width++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
126 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
127 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
128 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
129 return -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
130 }
2775
afd68d0b9fc1 (mbswidth): Add a flags argument.
Jim Meyering <jim@meyering.net>
parents: 2772
diff changeset
131
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
132 if (bytes == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
133 /* A null wide character was encountered. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
134 bytes = 1;
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
135
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
136 w = wcwidth (wc);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
137 if (w >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
138 /* A printable multibyte character. */
14418
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
139 {
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
140 if (w > INT_MAX - width)
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
141 goto overflow;
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
142 width += w;
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
143 }
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
144 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
145 /* An unprintable multibyte character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
146 if (!(flags & MBSW_REJECT_UNPRINTABLE))
14418
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
147 {
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
148 if (!iswcntrl (wc))
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
149 {
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
150 if (width == INT_MAX)
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
151 goto overflow;
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
152 width++;
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
153 }
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
154 }
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
155 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
156 return -1;
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
157
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
158 p += bytes;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
159 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
160 while (! mbsinit (&mbstate));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
161 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
162 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
163 }
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
164 return width;
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
165 }
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
166
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
167 while (p < plimit)
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
168 {
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
169 unsigned char c = (unsigned char) *p++;
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
170
6921
16e059328532 Assume ANSI C.
Bruno Haible <bruno@clisp.org>
parents: 6882
diff changeset
171 if (isprint (c))
14418
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
172 {
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
173 if (width == INT_MAX)
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
174 goto overflow;
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
175 width++;
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
176 }
3326
591c68289197 Don't include <limits.h>.
Jim Meyering <jim@meyering.net>
parents: 3174
diff changeset
177 else if (!(flags & MBSW_REJECT_UNPRINTABLE))
14418
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
178 {
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
179 if (!iscntrl (c))
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
180 {
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
181 if (width == INT_MAX)
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
182 goto overflow;
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
183 width++;
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
184 }
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
185 }
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
186 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10954
diff changeset
187 return -1;
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
188 }
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
189 return width;
14418
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
190
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
191 overflow:
07a5551df5ac wcswidth, mbswidth: Avoid integer overflow.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
192 return INT_MAX;
2757
0d95df8533b0 from Bruno Haible.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
193 }