Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / string / wcwidth.c
blob8348eefe8a4bb53d126855785d639f3c07774620
1 /*
2 FUNCTION
3 <<wcwidth>>---number of column positions of a wide-character code
5 INDEX
6 wcwidth
8 SYNOPSIS
9 #include <wchar.h>
10 int wcwidth(const wint_t <[wc]>);
12 DESCRIPTION
13 The <<wcwidth>> function shall determine the number of column
14 positions required for the wide character <[wc]>. The application
15 shall ensure that the value of <[wc]> is a character representable
16 as a wint_t (combining Unicode surrogate pairs into single 21-bit
17 Unicode code points), and is a wide-character code corresponding to a
18 valid character in the current locale.
20 RETURNS
21 The <<wcwidth>> function shall either return 0 (if <[wc]> is a null
22 wide-character code), or return the number of column positions to
23 be occupied by the wide-character code <[wc]>, or return -1 (if <[wc]>
24 does not correspond to a printable wide-character code).
26 PORTABILITY
27 <<wcwidth>> has been introduced in the Single UNIX Specification Volume 2.
28 <<wcwidth>> has been marked as an extension in the Single UNIX Specification Volume 3.
32 * This is an implementation of wcwidth() (defined in
33 * IEEE Std 1002.1-2001) for Unicode.
35 * http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
37 * In fixed-width output devices, Latin characters all occupy a single
38 * "cell" position of equal width, whereas ideographic CJK characters
39 * occupy two such cells. Interoperability between terminal-line
40 * applications and (teletype-style) character terminals using the
41 * UTF-8 encoding requires agreement on which character should advance
42 * the cursor by how many cell positions. No established formal
43 * standards exist at present on which Unicode character shall occupy
44 * how many cell positions on character terminals. These routines are
45 * a first attempt of defining such behavior based on simple rules
46 * applied to data provided by the Unicode Consortium.
48 * For some graphical characters, the Unicode standard explicitly
49 * defines a character-cell width via the definition of the East Asian
50 * FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes.
51 * In all these cases, there is no ambiguity about which width a
52 * terminal shall use. For characters in the East Asian Ambiguous (A)
53 * class, the width choice depends purely on a preference of backward
54 * compatibility with either historic CJK or Western practice.
55 * Choosing single-width for these characters is easy to justify as
56 * the appropriate long-term solution, as the CJK practice of
57 * displaying these characters as double-width comes from historic
58 * implementation simplicity (8-bit encoded characters were displayed
59 * single-width and 16-bit ones double-width, even for Greek,
60 * Cyrillic, etc.) and not any typographic considerations.
62 * Much less clear is the choice of width for the Not East Asian
63 * (Neutral) class. Existing practice does not dictate a width for any
64 * of these characters. It would nevertheless make sense
65 * typographically to allocate two character cells to characters such
66 * as for instance EM SPACE or VOLUME INTEGRAL, which cannot be
67 * represented adequately with a single-width glyph. The following
68 * routines at present merely assign a single-cell width to all
69 * neutral characters, in the interest of simplicity. This is not
70 * entirely satisfactory and should be reconsidered before
71 * establishing a formal standard in this area. At the moment, the
72 * decision which Not East Asian (Neutral) characters should be
73 * represented by double-width glyphs cannot yet be answered by
74 * applying a simple rule from the Unicode database content. Setting
75 * up a proper standard for the behavior of UTF-8 character terminals
76 * will require a careful analysis not only of each Unicode character,
77 * but also of each presentation form, something the author of these
78 * routines has avoided to do so far.
80 * http://www.unicode.org/unicode/reports/tr11/
82 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
84 * Permission to use, copy, modify, and distribute this software
85 * for any purpose and without fee is hereby granted. The author
86 * disclaims all warranties with regard to this software.
88 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
91 #include <_ansi.h>
92 #include <wchar.h>
93 #ifndef _MB_CAPABLE
94 #include <wctype.h> /* iswprint, iswcntrl */
95 #endif
96 #include "local.h"
98 #ifdef _MB_CAPABLE
99 struct interval
101 int first;
102 int last;
105 /* auxiliary function for binary search in interval table */
106 static int
107 bisearch(wint_t ucs, const struct interval *table, int max)
109 int min = 0;
110 int mid;
112 if (ucs < table[0].first || ucs > table[max].last)
113 return 0;
114 while (max >= min)
116 mid = (min + max) / 2;
117 if (ucs > table[mid].last)
118 min = mid + 1;
119 else if (ucs < table[mid].first)
120 max = mid - 1;
121 else
122 return 1;
125 return 0;
127 #endif /* _MB_CAPABLE */
129 /* The following function defines the column width of an ISO 10646
130 * character as follows:
132 * - The null character (U+0000) has a column width of 0.
134 * - Other C0/C1 control characters and DEL will lead to a return
135 * value of -1.
137 * - If the current language is recognized as a language usually using
138 * CJK fonts, spacing characters in the East Asian Ambiguous (A)
139 * category as defined in Unicode Technical Report #11 have a column
140 * width of 2.
142 * - Non-spacing and enclosing combining characters (general
143 * category code Mn or Me in the Unicode database) have a
144 * column width of 0.
146 * - SOFT HYPHEN (U+00AD) has a column width of 1.
148 * - Other format characters (general category code Cf in the Unicode
149 * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
151 * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
152 * have a column width of 0.
154 * - Spacing characters in the East Asian Wide (W) or East Asian
155 * Full-width (F) category as defined in Unicode Technical
156 * Report #11 have a column width of 2.
158 * - All remaining characters (including all printable
159 * ISO 8859-1 and WGL4 characters, Unicode control characters,
160 * etc.) have a column width of 1.
162 * This implementation assumes that wint_t characters are encoded
163 * in ISO 10646.
167 __wcwidth (const wint_t ucs)
169 #ifdef _MB_CAPABLE
170 /* sorted list of non-overlapping intervals of East Asian Ambiguous chars */
171 static const struct interval ambiguous[] =
172 #include "ambiguous.t"
174 /* sorted list of non-overlapping intervals of non-spacing characters */
175 static const struct interval combining[] =
176 #include "combining.t"
178 /* sorted list of non-overlapping intervals of wide characters,
179 ranges extended to Blocks where possible
181 static const struct interval wide[] =
182 #include "wide.t"
184 /* Test for NUL character */
185 if (ucs == 0)
186 return 0;
188 /* Test for printable ASCII characters */
189 if (ucs >= 0x20 && ucs < 0x7f)
190 return 1;
192 /* Test for control characters */
193 if (ucs < 0xa0)
194 return -1;
196 /* Test for surrogate pair values. */
197 if (ucs >= 0xd800 && ucs <= 0xdfff)
198 return -1;
200 /* check CJK width mode (1: ambiguous-wide, 0: normal, -1: disabled) */
201 int cjk_lang = __locale_cjk_lang ();
203 /* binary search in table of ambiguous characters */
204 if (cjk_lang > 0
205 && bisearch(ucs, ambiguous,
206 sizeof(ambiguous) / sizeof(struct interval) - 1))
207 return 2;
209 /* binary search in table of non-spacing characters */
210 if (bisearch(ucs, combining,
211 sizeof(combining) / sizeof(struct interval) - 1))
212 return 0;
214 /* if we arrive here, ucs is not a combining or C0/C1 control character */
216 /* binary search in table of wide character codes */
217 if (cjk_lang >= 0
218 && bisearch(ucs, wide,
219 sizeof(wide) / sizeof(struct interval) - 1))
220 return 2;
221 else
222 return 1;
223 #else /* !_MB_CAPABLE */
224 if (iswprint (ucs))
225 return 1;
226 if (iswcntrl (ucs) || ucs == L'\0')
227 return 0;
228 return -1;
229 #endif /* _MB_CAPABLE */
233 wcwidth (const wint_t wc)
235 wint_t wi = wc;
237 #ifdef _MB_CAPABLE
238 wi = _jp2uc (wi);
239 #endif /* _MB_CAPABLE */
240 return __wcwidth (wi);