summaryrefslogtreecommitdiff
path: root/libbb/unicode.c (plain)
blob: 6bdb666c9cc98dae24479fb03673c1baa5282843
1/* vi: set sw=4 ts=4: */
2/*
3 * Unicode support routines.
4 *
5 * Copyright (C) 2009 Denys Vlasenko
6 *
7 * Licensed under GPLv2, see file LICENSE in this source tree.
8 */
9#include "libbb.h"
10#include "unicode.h"
11
12/* If it's not #defined as a constant in unicode.h... */
13#ifndef unicode_status
14uint8_t unicode_status;
15#endif
16
17#ifdef __BIONIC__
18# define VOID
19#else
20# define VOID void
21#endif
22
23/* This file is compiled only if UNICODE_SUPPORT is on.
24 * We check other options and decide whether to use libc support
25 * via locale, or use our own logic:
26 */
27
28#if ENABLE_UNICODE_USING_LOCALE
29
30/* Unicode support using libc locale support. */
31
32void FAST_FUNC reinit_unicode(const char *LANG)
33{
34 static const char unicode_0x394[] = { 0xce, 0x94, 0 };
35 size_t width;
36
37 /* We pass "" instead of "C" because some libc's have
38 * non-ASCII default locale for setlocale("") call
39 * (this allows users of such libc to have Unicoded
40 * system without having to mess with env).
41 *
42 * We set LC_CTYPE because (a) we may be called with $LC_CTYPE
43 * value in LANG, not with $LC_ALL, (b) internationalized
44 * LC_NUMERIC and LC_TIME are more PITA than benefit
45 * (for one, some utilities have hard time with comma
46 * used as a fractional separator).
47 */
48//TODO: avoid repeated calls by caching last string?
49 setlocale(LC_CTYPE, LANG ? LANG : "");
50
51 /* In unicode, this is a one character string */
52 width = unicode_strlen(unicode_0x394);
53 unicode_status = (width == 1 ? UNICODE_ON : UNICODE_OFF);
54}
55
56void FAST_FUNC init_unicode(VOID)
57{
58 /* Some people set only $LC_CTYPE, not $LC_ALL, because they want
59 * only Unicode to be activated on their system, not the whole
60 * shebang of wrong decimal points, strange date formats and so on.
61 */
62 if (unicode_status == UNICODE_UNKNOWN) {
63 char *s = getenv("LC_ALL");
64 if (!s) s = getenv("LC_CTYPE");
65 if (!s) s = getenv("LANG");
66 reinit_unicode(s);
67 }
68}
69
70#else
71
72/* Homegrown Unicode support. It knows only C and Unicode locales. */
73
74# if ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
75void FAST_FUNC reinit_unicode(const char *LANG)
76{
77 unicode_status = UNICODE_OFF;
78 if (!LANG || !(strstr(LANG, ".utf") || strstr(LANG, ".UTF")))
79 return;
80 unicode_status = UNICODE_ON;
81}
82
83void FAST_FUNC init_unicode(VOID)
84{
85 if (unicode_status == UNICODE_UNKNOWN) {
86 char *s = getenv("LC_ALL");
87 if (!s) s = getenv("LC_CTYPE");
88 if (!s) s = getenv("LANG");
89 reinit_unicode(s);
90 }
91}
92# endif
93
94static size_t wcrtomb_internal(char *s, wchar_t wc)
95{
96 int n, i;
97 uint32_t v = wc;
98
99 if (v <= 0x7f) {
100 *s = v;
101 return 1;
102 }
103
104 /* RFC 3629 says that Unicode ends at 10FFFF,
105 * but we cover entire 32 bits */
106
107 /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
108 /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
109 /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
110 /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
111 /* 80-7FF -> 110yyyxx 10xxxxxx */
112
113 /* How many bytes do we need? */
114 n = 2;
115 /* (0x80000000+ would result in n = 7, limiting n to 6) */
116 while (v >= 0x800 && n < 6) {
117 v >>= 5;
118 n++;
119 }
120 /* Fill bytes n-1..1 */
121 i = n;
122 while (--i) {
123 s[i] = (wc & 0x3f) | 0x80;
124 wc >>= 6;
125 }
126 /* Fill byte 0 */
127 s[0] = wc | (uint8_t)(0x3f00 >> n);
128 return n;
129}
130size_t FAST_FUNC wcrtomb(char *s, wchar_t wc, mbstate_t *ps UNUSED_PARAM)
131{
132 if (unicode_status != UNICODE_ON) {
133 *s = wc;
134 return 1;
135 }
136
137 return wcrtomb_internal(s, wc);
138}
139size_t FAST_FUNC wcstombs(char *dest, const wchar_t *src, size_t n)
140{
141 size_t org_n = n;
142
143 if (unicode_status != UNICODE_ON) {
144 while (n) {
145 wchar_t c = *src++;
146 *dest++ = c;
147 if (c == 0)
148 break;
149 n--;
150 }
151 return org_n - n;
152 }
153
154 while (n >= MB_CUR_MAX) {
155 wchar_t wc = *src++;
156 size_t len = wcrtomb_internal(dest, wc);
157
158 if (wc == L'\0')
159 return org_n - n;
160 dest += len;
161 n -= len;
162 }
163 while (n) {
164 char tbuf[MB_CUR_MAX];
165 wchar_t wc = *src++;
166 size_t len = wcrtomb_internal(tbuf, wc);
167
168 if (len > n)
169 break;
170 memcpy(dest, tbuf, len);
171 if (wc == L'\0')
172 return org_n - n;
173 dest += len;
174 n -= len;
175 }
176 return org_n - n;
177}
178
179# define ERROR_WCHAR (~(wchar_t)0)
180
181static const char *mbstowc_internal(wchar_t *res, const char *src)
182{
183 int bytes;
184 unsigned c = (unsigned char) *src++;
185
186 if (c <= 0x7f) {
187 *res = c;
188 return src;
189 }
190
191 /* 80-7FF -> 110yyyxx 10xxxxxx */
192 /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
193 /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
194 /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
195 /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
196 bytes = 0;
197 do {
198 c <<= 1;
199 bytes++;
200 } while ((c & 0x80) && bytes < 6);
201 if (bytes == 1) {
202 /* A bare "continuation" byte. Say, 80 */
203 *res = ERROR_WCHAR;
204 return src;
205 }
206 c = (uint8_t)(c) >> bytes;
207
208 while (--bytes) {
209 unsigned ch = (unsigned char) *src;
210 if ((ch & 0xc0) != 0x80) {
211 /* Missing "continuation" byte. Example: e0 80 */
212 *res = ERROR_WCHAR;
213 return src;
214 }
215 c = (c << 6) + (ch & 0x3f);
216 src++;
217 }
218
219 /* TODO */
220 /* Need to check that c isn't produced by overlong encoding */
221 /* Example: 11000000 10000000 converts to NUL */
222 /* 11110000 10000000 10000100 10000000 converts to 0x100 */
223 /* correct encoding: 11000100 10000000 */
224 if (c <= 0x7f) { /* crude check */
225 *res = ERROR_WCHAR;
226 return src;
227 }
228
229 *res = c;
230 return src;
231}
232size_t FAST_FUNC mbstowcs(wchar_t *dest, const char *src, size_t n)
233{
234 size_t org_n = n;
235
236 if (!src) return 0;
237
238 if (unicode_status != UNICODE_ON) {
239 while (n) {
240 unsigned char c = *src++;
241
242 if (dest)
243 *dest++ = c;
244 if (c == 0)
245 break;
246 n--;
247 }
248 return org_n - n;
249 }
250
251 while (n) {
252 wchar_t wc;
253 src = mbstowc_internal(&wc, src);
254 if (wc == ERROR_WCHAR) /* error */
255 return (size_t) -1L;
256 if (dest)
257 *dest++ = wc;
258 if (wc == 0) /* end-of-string */
259 break;
260 n--;
261 }
262
263 return org_n - n;
264}
265
266int FAST_FUNC iswspace(wint_t wc)
267{
268 return (unsigned)wc <= 0x7f && isspace(wc);
269}
270
271int FAST_FUNC iswalnum(wint_t wc)
272{
273 return (unsigned)wc <= 0x7f && isalnum(wc);
274}
275
276int FAST_FUNC iswpunct(wint_t wc)
277{
278 return (unsigned)wc <= 0x7f && ispunct(wc);
279}
280
281
282# if CONFIG_LAST_SUPPORTED_WCHAR >= 0x300
283struct interval {
284 uint16_t first;
285 uint16_t last;
286};
287
288/* auxiliary function for binary search in interval table */
289static int in_interval_table(unsigned ucs, const struct interval *table, unsigned max)
290{
291 unsigned min;
292 unsigned mid;
293
294 if (ucs < table[0].first || ucs > table[max].last)
295 return 0;
296
297 min = 0;
298 while (max >= min) {
299 mid = (min + max) / 2;
300 if (ucs > table[mid].last)
301 min = mid + 1;
302 else if (ucs < table[mid].first)
303 max = mid - 1;
304 else
305 return 1;
306 }
307 return 0;
308}
309
310static int in_uint16_table(unsigned ucs, const uint16_t *table, unsigned max)
311{
312 unsigned min;
313 unsigned mid;
314 unsigned first, last;
315
316 first = table[0] >> 2;
317 last = first + (table[0] & 3);
318 if (ucs < first || ucs > last)
319 return 0;
320
321 min = 0;
322 while (max >= min) {
323 mid = (min + max) / 2;
324 first = table[mid] >> 2;
325 last = first + (table[mid] & 3);
326 if (ucs > last)
327 min = mid + 1;
328 else if (ucs < first)
329 max = mid - 1;
330 else
331 return 1;
332 }
333 return 0;
334}
335# endif
336
337
338/*
339 * This is an implementation of wcwidth() and wcswidth() (defined in
340 * IEEE Std 1002.1-2001) for Unicode.
341 *
342 * http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
343 * http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html
344 *
345 * In fixed-width output devices, Latin characters all occupy a single
346 * "cell" position of equal width, whereas ideographic CJK characters
347 * occupy two such cells. Interoperability between terminal-line
348 * applications and (teletype-style) character terminals using the
349 * UTF-8 encoding requires agreement on which character should advance
350 * the cursor by how many cell positions. No established formal
351 * standards exist at present on which Unicode character shall occupy
352 * how many cell positions on character terminals. These routines are
353 * a first attempt of defining such behavior based on simple rules
354 * applied to data provided by the Unicode Consortium.
355 *
356 * For some graphical characters, the Unicode standard explicitly
357 * defines a character-cell width via the definition of the East Asian
358 * FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes.
359 * In all these cases, there is no ambiguity about which width a
360 * terminal shall use. For characters in the East Asian Ambiguous (A)
361 * class, the width choice depends purely on a preference of backward
362 * compatibility with either historic CJK or Western practice.
363 * Choosing single-width for these characters is easy to justify as
364 * the appropriate long-term solution, as the CJK practice of
365 * displaying these characters as double-width comes from historic
366 * implementation simplicity (8-bit encoded characters were displayed
367 * single-width and 16-bit ones double-width, even for Greek,
368 * Cyrillic, etc.) and not any typographic considerations.
369 *
370 * Much less clear is the choice of width for the Not East Asian
371 * (Neutral) class. Existing practice does not dictate a width for any
372 * of these characters. It would nevertheless make sense
373 * typographically to allocate two character cells to characters such
374 * as for instance EM SPACE or VOLUME INTEGRAL, which cannot be
375 * represented adequately with a single-width glyph. The following
376 * routines at present merely assign a single-cell width to all
377 * neutral characters, in the interest of simplicity. This is not
378 * entirely satisfactory and should be reconsidered before
379 * establishing a formal standard in this area. At the moment, the
380 * decision which Not East Asian (Neutral) characters should be
381 * represented by double-width glyphs cannot yet be answered by
382 * applying a simple rule from the Unicode database content. Setting
383 * up a proper standard for the behavior of UTF-8 character terminals
384 * will require a careful analysis not only of each Unicode character,
385 * but also of each presentation form, something the author of these
386 * routines has avoided to do so far.
387 *
388 * http://www.unicode.org/unicode/reports/tr11/
389 *
390 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
391 *
392 * Permission to use, copy, modify, and distribute this software
393 * for any purpose and without fee is hereby granted. The author
394 * disclaims all warranties with regard to this software.
395 *
396 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
397 */
398
399/* Assigned Unicode character ranges:
400 * Plane Range
401 * 0 0000–FFFF Basic Multilingual Plane
402 * 1 10000–1FFFF Supplementary Multilingual Plane
403 * 2 20000–2FFFF Supplementary Ideographic Plane
404 * 3 30000-3FFFF Tertiary Ideographic Plane (no chars assigned yet)
405 * 4-13 40000–DFFFF currently unassigned
406 * 14 E0000–EFFFF Supplementary Special-purpose Plane
407 * 15 F0000–FFFFF Supplementary Private Use Area-A
408 * 16 100000–10FFFF Supplementary Private Use Area-B
409 *
410 * "Supplementary Special-purpose Plane currently contains non-graphical
411 * characters in two blocks of 128 and 240 characters. The first block
412 * is for language tag characters for use when language cannot be indicated
413 * through other protocols (such as the xml:lang attribute in XML).
414 * The other block contains glyph variation selectors to indicate
415 * an alternate glyph for a character that cannot be determined by context."
416 *
417 * In simpler terms: it is a tool to fix the "Han unification" mess
418 * created by Unicode committee, to select Chinese/Japanese/Korean/Taiwan
419 * version of a character. (They forgot that the whole purpose of the Unicode
420 * was to be able to write all chars in one charset without such tricks).
421 * Until East Asian users say it is actually necessary to support these
422 * code points in console applications like busybox
423 * (i.e. do these chars ever appear in filenames, hostnames, text files
424 * and such?), we are treating these code points as invalid.
425 *
426 * Tertiary Ideographic Plane is also ignored for now,
427 * until Unicode committee assigns something there.
428 */
429/* The following two functions define the column width of an ISO 10646
430 * character as follows:
431 *
432 * - The null character (U+0000) has a column width of 0.
433 *
434 * - Other C0/C1 control characters and DEL will lead to a return
435 * value of -1.
436 *
437 * - Non-spacing and enclosing combining characters (general
438 * category code Mn or Me in the Unicode database) have a
439 * column width of 0.
440 *
441 * - SOFT HYPHEN (U+00AD) has a column width of 1.
442 *
443 * - Other format characters (general category code Cf in the Unicode
444 * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
445 *
446 * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
447 * have a column width of 0.
448 *
449 * - Spacing characters in the East Asian Wide (W) or East Asian
450 * Full-width (F) category as defined in Unicode Technical
451 * Report #11 have a column width of 2.
452 *
453 * - All remaining characters (including all printable
454 * ISO 8859-1 and WGL4 characters, Unicode control characters,
455 * etc.) have a column width of 1.
456 *
457 * This implementation assumes that wchar_t characters are encoded
458 * in ISO 10646.
459 */
460int FAST_FUNC wcwidth(unsigned ucs)
461{
462# if CONFIG_LAST_SUPPORTED_WCHAR >= 0x300
463 /* sorted list of non-overlapping intervals of non-spacing characters */
464 /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */
465# define BIG_(a,b) { a, b },
466# define PAIR(a,b)
467# define ARRAY /* PAIR if < 0x4000 and no more than 4 chars big */ \
468 BIG_(0x0300, 0x036F) \
469 PAIR(0x0483, 0x0486) \
470 PAIR(0x0488, 0x0489) \
471 BIG_(0x0591, 0x05BD) \
472 PAIR(0x05BF, 0x05BF) \
473 PAIR(0x05C1, 0x05C2) \
474 PAIR(0x05C4, 0x05C5) \
475 PAIR(0x05C7, 0x05C7) \
476 PAIR(0x0600, 0x0603) \
477 BIG_(0x0610, 0x0615) \
478 BIG_(0x064B, 0x065E) \
479 PAIR(0x0670, 0x0670) \
480 BIG_(0x06D6, 0x06E4) \
481 PAIR(0x06E7, 0x06E8) \
482 PAIR(0x06EA, 0x06ED) \
483 PAIR(0x070F, 0x070F) \
484 PAIR(0x0711, 0x0711) \
485 BIG_(0x0730, 0x074A) \
486 BIG_(0x07A6, 0x07B0) \
487 BIG_(0x07EB, 0x07F3) \
488 PAIR(0x0901, 0x0902) \
489 PAIR(0x093C, 0x093C) \
490 BIG_(0x0941, 0x0948) \
491 PAIR(0x094D, 0x094D) \
492 PAIR(0x0951, 0x0954) \
493 PAIR(0x0962, 0x0963) \
494 PAIR(0x0981, 0x0981) \
495 PAIR(0x09BC, 0x09BC) \
496 PAIR(0x09C1, 0x09C4) \
497 PAIR(0x09CD, 0x09CD) \
498 PAIR(0x09E2, 0x09E3) \
499 PAIR(0x0A01, 0x0A02) \
500 PAIR(0x0A3C, 0x0A3C) \
501 PAIR(0x0A41, 0x0A42) \
502 PAIR(0x0A47, 0x0A48) \
503 PAIR(0x0A4B, 0x0A4D) \
504 PAIR(0x0A70, 0x0A71) \
505 PAIR(0x0A81, 0x0A82) \
506 PAIR(0x0ABC, 0x0ABC) \
507 BIG_(0x0AC1, 0x0AC5) \
508 PAIR(0x0AC7, 0x0AC8) \
509 PAIR(0x0ACD, 0x0ACD) \
510 PAIR(0x0AE2, 0x0AE3) \
511 PAIR(0x0B01, 0x0B01) \
512 PAIR(0x0B3C, 0x0B3C) \
513 PAIR(0x0B3F, 0x0B3F) \
514 PAIR(0x0B41, 0x0B43) \
515 PAIR(0x0B4D, 0x0B4D) \
516 PAIR(0x0B56, 0x0B56) \
517 PAIR(0x0B82, 0x0B82) \
518 PAIR(0x0BC0, 0x0BC0) \
519 PAIR(0x0BCD, 0x0BCD) \
520 PAIR(0x0C3E, 0x0C40) \
521 PAIR(0x0C46, 0x0C48) \
522 PAIR(0x0C4A, 0x0C4D) \
523 PAIR(0x0C55, 0x0C56) \
524 PAIR(0x0CBC, 0x0CBC) \
525 PAIR(0x0CBF, 0x0CBF) \
526 PAIR(0x0CC6, 0x0CC6) \
527 PAIR(0x0CCC, 0x0CCD) \
528 PAIR(0x0CE2, 0x0CE3) \
529 PAIR(0x0D41, 0x0D43) \
530 PAIR(0x0D4D, 0x0D4D) \
531 PAIR(0x0DCA, 0x0DCA) \
532 PAIR(0x0DD2, 0x0DD4) \
533 PAIR(0x0DD6, 0x0DD6) \
534 PAIR(0x0E31, 0x0E31) \
535 BIG_(0x0E34, 0x0E3A) \
536 BIG_(0x0E47, 0x0E4E) \
537 PAIR(0x0EB1, 0x0EB1) \
538 BIG_(0x0EB4, 0x0EB9) \
539 PAIR(0x0EBB, 0x0EBC) \
540 BIG_(0x0EC8, 0x0ECD) \
541 PAIR(0x0F18, 0x0F19) \
542 PAIR(0x0F35, 0x0F35) \
543 PAIR(0x0F37, 0x0F37) \
544 PAIR(0x0F39, 0x0F39) \
545 BIG_(0x0F71, 0x0F7E) \
546 BIG_(0x0F80, 0x0F84) \
547 PAIR(0x0F86, 0x0F87) \
548 PAIR(0x0FC6, 0x0FC6) \
549 BIG_(0x0F90, 0x0F97) \
550 BIG_(0x0F99, 0x0FBC) \
551 PAIR(0x102D, 0x1030) \
552 PAIR(0x1032, 0x1032) \
553 PAIR(0x1036, 0x1037) \
554 PAIR(0x1039, 0x1039) \
555 PAIR(0x1058, 0x1059) \
556 BIG_(0x1160, 0x11FF) \
557 PAIR(0x135F, 0x135F) \
558 PAIR(0x1712, 0x1714) \
559 PAIR(0x1732, 0x1734) \
560 PAIR(0x1752, 0x1753) \
561 PAIR(0x1772, 0x1773) \
562 PAIR(0x17B4, 0x17B5) \
563 BIG_(0x17B7, 0x17BD) \
564 PAIR(0x17C6, 0x17C6) \
565 BIG_(0x17C9, 0x17D3) \
566 PAIR(0x17DD, 0x17DD) \
567 PAIR(0x180B, 0x180D) \
568 PAIR(0x18A9, 0x18A9) \
569 PAIR(0x1920, 0x1922) \
570 PAIR(0x1927, 0x1928) \
571 PAIR(0x1932, 0x1932) \
572 PAIR(0x1939, 0x193B) \
573 PAIR(0x1A17, 0x1A18) \
574 PAIR(0x1B00, 0x1B03) \
575 PAIR(0x1B34, 0x1B34) \
576 BIG_(0x1B36, 0x1B3A) \
577 PAIR(0x1B3C, 0x1B3C) \
578 PAIR(0x1B42, 0x1B42) \
579 BIG_(0x1B6B, 0x1B73) \
580 BIG_(0x1DC0, 0x1DCA) \
581 PAIR(0x1DFE, 0x1DFF) \
582 BIG_(0x200B, 0x200F) \
583 BIG_(0x202A, 0x202E) \
584 PAIR(0x2060, 0x2063) \
585 BIG_(0x206A, 0x206F) \
586 BIG_(0x20D0, 0x20EF) \
587 BIG_(0x302A, 0x302F) \
588 PAIR(0x3099, 0x309A) \
589 /* Too big to be packed in PAIRs: */ \
590 BIG_(0xA806, 0xA806) \
591 BIG_(0xA80B, 0xA80B) \
592 BIG_(0xA825, 0xA826) \
593 BIG_(0xFB1E, 0xFB1E) \
594 BIG_(0xFE00, 0xFE0F) \
595 BIG_(0xFE20, 0xFE23) \
596 BIG_(0xFEFF, 0xFEFF) \
597 BIG_(0xFFF9, 0xFFFB)
598 static const struct interval combining[] = { ARRAY };
599# undef BIG_
600# undef PAIR
601# define BIG_(a,b)
602# define PAIR(a,b) (a << 2) | (b-a),
603 static const uint16_t combining1[] = { ARRAY };
604# undef BIG_
605# undef PAIR
606# define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
607# define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
608 struct CHECK { ARRAY };
609# undef BIG_
610# undef PAIR
611# undef ARRAY
612# endif
613
614 if (ucs == 0)
615 return 0;
616
617 /* Test for 8-bit control characters (00-1f, 80-9f, 7f) */
618 if ((ucs & ~0x80) < 0x20 || ucs == 0x7f)
619 return -1;
620 /* Quick abort if it is an obviously invalid char */
621 if (ucs > CONFIG_LAST_SUPPORTED_WCHAR)
622 return -1;
623
624 /* Optimization: no combining chars below 0x300 */
625 if (CONFIG_LAST_SUPPORTED_WCHAR < 0x300 || ucs < 0x300)
626 return 1;
627
628# if CONFIG_LAST_SUPPORTED_WCHAR >= 0x300
629 /* Binary search in table of non-spacing characters */
630 if (in_interval_table(ucs, combining, ARRAY_SIZE(combining) - 1))
631 return 0;
632 if (in_uint16_table(ucs, combining1, ARRAY_SIZE(combining1) - 1))
633 return 0;
634
635 /* Optimization: all chars below 0x1100 are not double-width */
636 if (CONFIG_LAST_SUPPORTED_WCHAR < 0x1100 || ucs < 0x1100)
637 return 1;
638
639# if CONFIG_LAST_SUPPORTED_WCHAR >= 0x1100
640 /* Invalid code points: */
641 /* High (d800..dbff) and low (dc00..dfff) surrogates (valid only in UTF16) */
642 /* Private Use Area (e000..f8ff) */
643 /* Noncharacters fdd0..fdef */
644 if ((CONFIG_LAST_SUPPORTED_WCHAR >= 0xd800 && ucs >= 0xd800 && ucs <= 0xf8ff)
645 || (CONFIG_LAST_SUPPORTED_WCHAR >= 0xfdd0 && ucs >= 0xfdd0 && ucs <= 0xfdef)
646 ) {
647 return -1;
648 }
649 /* 0xfffe and 0xffff in every plane are invalid */
650 if (CONFIG_LAST_SUPPORTED_WCHAR >= 0xfffe && ((ucs & 0xfffe) == 0xfffe)) {
651 return -1;
652 }
653
654# if CONFIG_LAST_SUPPORTED_WCHAR >= 0x10000
655 if (ucs >= 0x10000) {
656 /* Combining chars in Supplementary Multilingual Plane 0x1xxxx */
657 static const struct interval combining0x10000[] = {
658 { 0x0A01, 0x0A03 }, { 0x0A05, 0x0A06 }, { 0x0A0C, 0x0A0F },
659 { 0x0A38, 0x0A3A }, { 0x0A3F, 0x0A3F }, { 0xD167, 0xD169 },
660 { 0xD173, 0xD182 }, { 0xD185, 0xD18B }, { 0xD1AA, 0xD1AD },
661 { 0xD242, 0xD244 }
662 };
663 /* Binary search in table of non-spacing characters in Supplementary Multilingual Plane */
664 if (in_interval_table(ucs ^ 0x10000, combining0x10000, ARRAY_SIZE(combining0x10000) - 1))
665 return 0;
666 /* Check a few non-spacing chars in Supplementary Special-purpose Plane 0xExxxx */
667 if (CONFIG_LAST_SUPPORTED_WCHAR >= 0xE0001
668 && ( ucs == 0xE0001
669 || (ucs >= 0xE0020 && ucs <= 0xE007F)
670 || (ucs >= 0xE0100 && ucs <= 0xE01EF)
671 )
672 ) {
673 return 0;
674 }
675 }
676# endif
677
678 /* If we arrive here, ucs is not a combining or C0/C1 control character.
679 * Check whether it's 1 char or 2-shar wide.
680 */
681 return 1 +
682 ( (/*ucs >= 0x1100 &&*/ ucs <= 0x115f) /* Hangul Jamo init. consonants */
683 || ucs == 0x2329 /* left-pointing angle bracket; also CJK punct. char */
684 || ucs == 0x232a /* right-pointing angle bracket; also CJK punct. char */
685 || (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs != 0x303f) /* CJK ... Yi */
686# if CONFIG_LAST_SUPPORTED_WCHAR >= 0xac00
687 || (ucs >= 0xac00 && ucs <= 0xd7a3) /* Hangul Syllables */
688 || (ucs >= 0xf900 && ucs <= 0xfaff) /* CJK Compatibility Ideographs */
689 || (ucs >= 0xfe10 && ucs <= 0xfe19) /* Vertical forms */
690 || (ucs >= 0xfe30 && ucs <= 0xfe6f) /* CJK Compatibility Forms */
691 || (ucs >= 0xff00 && ucs <= 0xff60) /* Fullwidth Forms */
692 || (ucs >= 0xffe0 && ucs <= 0xffe6)
693 || ((ucs >> 17) == (2 >> 1)) /* 20000..3ffff: Supplementary and Tertiary Ideographic Planes */
694# endif
695 );
696# endif /* >= 0x1100 */
697# endif /* >= 0x300 */
698}
699
700
701# if ENABLE_UNICODE_BIDI_SUPPORT
702int FAST_FUNC unicode_bidi_isrtl(wint_t wc)
703{
704 /* ranges taken from
705 * http://www.unicode.org/Public/5.2.0/ucd/extracted/DerivedBidiClass.txt
706 * Bidi_Class=Left_To_Right | Bidi_Class=Arabic_Letter
707 */
708# define BIG_(a,b) { a, b },
709# define PAIR(a,b)
710# define ARRAY \
711 PAIR(0x0590, 0x0590) \
712 PAIR(0x05BE, 0x05BE) \
713 PAIR(0x05C0, 0x05C0) \
714 PAIR(0x05C3, 0x05C3) \
715 PAIR(0x05C6, 0x05C6) \
716 BIG_(0x05C8, 0x05FF) \
717 PAIR(0x0604, 0x0605) \
718 PAIR(0x0608, 0x0608) \
719 PAIR(0x060B, 0x060B) \
720 PAIR(0x060D, 0x060D) \
721 BIG_(0x061B, 0x064A) \
722 PAIR(0x065F, 0x065F) \
723 PAIR(0x066D, 0x066F) \
724 BIG_(0x0671, 0x06D5) \
725 PAIR(0x06E5, 0x06E6) \
726 PAIR(0x06EE, 0x06EF) \
727 BIG_(0x06FA, 0x070E) \
728 PAIR(0x0710, 0x0710) \
729 BIG_(0x0712, 0x072F) \
730 BIG_(0x074B, 0x07A5) \
731 BIG_(0x07B1, 0x07EA) \
732 PAIR(0x07F4, 0x07F5) \
733 BIG_(0x07FA, 0x0815) \
734 PAIR(0x081A, 0x081A) \
735 PAIR(0x0824, 0x0824) \
736 PAIR(0x0828, 0x0828) \
737 BIG_(0x082E, 0x08FF) \
738 PAIR(0x200F, 0x200F) \
739 PAIR(0x202B, 0x202B) \
740 PAIR(0x202E, 0x202E) \
741 BIG_(0xFB1D, 0xFB1D) \
742 BIG_(0xFB1F, 0xFB28) \
743 BIG_(0xFB2A, 0xFD3D) \
744 BIG_(0xFD40, 0xFDCF) \
745 BIG_(0xFDC8, 0xFDCF) \
746 BIG_(0xFDF0, 0xFDFC) \
747 BIG_(0xFDFE, 0xFDFF) \
748 BIG_(0xFE70, 0xFEFE)
749 /* Probably not necessary
750 {0x10800, 0x1091E},
751 {0x10920, 0x10A00},
752 {0x10A04, 0x10A04},
753 {0x10A07, 0x10A0B},
754 {0x10A10, 0x10A37},
755 {0x10A3B, 0x10A3E},
756 {0x10A40, 0x10A7F},
757 {0x10B36, 0x10B38},
758 {0x10B40, 0x10E5F},
759 {0x10E7F, 0x10FFF},
760 {0x1E800, 0x1EFFF}
761 */
762 static const struct interval rtl_b[] = { ARRAY };
763# undef BIG_
764# undef PAIR
765# define BIG_(a,b)
766# define PAIR(a,b) (a << 2) | (b-a),
767 static const uint16_t rtl_p[] = { ARRAY };
768# undef BIG_
769# undef PAIR
770# define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
771# define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
772 struct CHECK { ARRAY };
773# undef BIG_
774# undef PAIR
775# undef ARRAY
776
777 if (in_interval_table(wc, rtl_b, ARRAY_SIZE(rtl_b) - 1))
778 return 1;
779 if (in_uint16_table(wc, rtl_p, ARRAY_SIZE(rtl_p) - 1))
780 return 1;
781 return 0;
782}
783
784# if ENABLE_UNICODE_NEUTRAL_TABLE
785int FAST_FUNC unicode_bidi_is_neutral_wchar(wint_t wc)
786{
787 /* ranges taken from
788 * http://www.unicode.org/Public/5.2.0/ucd/extracted/DerivedBidiClass.txt
789 * Bidi_Classes: Paragraph_Separator, Segment_Separator,
790 * White_Space, Other_Neutral, European_Number, European_Separator,
791 * European_Terminator, Arabic_Number, Common_Separator
792 */
793# define BIG_(a,b) { a, b },
794# define PAIR(a,b)
795# define ARRAY \
796 BIG_(0x0009, 0x000D) \
797 BIG_(0x001C, 0x0040) \
798 BIG_(0x005B, 0x0060) \
799 PAIR(0x007B, 0x007E) \
800 PAIR(0x0085, 0x0085) \
801 BIG_(0x00A0, 0x00A9) \
802 PAIR(0x00AB, 0x00AC) \
803 BIG_(0x00AE, 0x00B4) \
804 PAIR(0x00B6, 0x00B9) \
805 BIG_(0x00BB, 0x00BF) \
806 PAIR(0x00D7, 0x00D7) \
807 PAIR(0x00F7, 0x00F7) \
808 PAIR(0x02B9, 0x02BA) \
809 BIG_(0x02C2, 0x02CF) \
810 BIG_(0x02D2, 0x02DF) \
811 BIG_(0x02E5, 0x02FF) \
812 PAIR(0x0374, 0x0375) \
813 PAIR(0x037E, 0x037E) \
814 PAIR(0x0384, 0x0385) \
815 PAIR(0x0387, 0x0387) \
816 PAIR(0x03F6, 0x03F6) \
817 PAIR(0x058A, 0x058A) \
818 PAIR(0x0600, 0x0603) \
819 PAIR(0x0606, 0x0607) \
820 PAIR(0x0609, 0x060A) \
821 PAIR(0x060C, 0x060C) \
822 PAIR(0x060E, 0x060F) \
823 BIG_(0x0660, 0x066C) \
824 PAIR(0x06DD, 0x06DD) \
825 PAIR(0x06E9, 0x06E9) \
826 BIG_(0x06F0, 0x06F9) \
827 PAIR(0x07F6, 0x07F9) \
828 PAIR(0x09F2, 0x09F3) \
829 PAIR(0x09FB, 0x09FB) \
830 PAIR(0x0AF1, 0x0AF1) \
831 BIG_(0x0BF3, 0x0BFA) \
832 BIG_(0x0C78, 0x0C7E) \
833 PAIR(0x0CF1, 0x0CF2) \
834 PAIR(0x0E3F, 0x0E3F) \
835 PAIR(0x0F3A, 0x0F3D) \
836 BIG_(0x1390, 0x1400) \
837 PAIR(0x1680, 0x1680) \
838 PAIR(0x169B, 0x169C) \
839 PAIR(0x17DB, 0x17DB) \
840 BIG_(0x17F0, 0x17F9) \
841 BIG_(0x1800, 0x180A) \
842 PAIR(0x180E, 0x180E) \
843 PAIR(0x1940, 0x1940) \
844 PAIR(0x1944, 0x1945) \
845 BIG_(0x19DE, 0x19FF) \
846 PAIR(0x1FBD, 0x1FBD) \
847 PAIR(0x1FBF, 0x1FC1) \
848 PAIR(0x1FCD, 0x1FCF) \
849 PAIR(0x1FDD, 0x1FDF) \
850 PAIR(0x1FED, 0x1FEF) \
851 PAIR(0x1FFD, 0x1FFE) \
852 BIG_(0x2000, 0x200A) \
853 BIG_(0x2010, 0x2029) \
854 BIG_(0x202F, 0x205F) \
855 PAIR(0x2070, 0x2070) \
856 BIG_(0x2074, 0x207E) \
857 BIG_(0x2080, 0x208E) \
858 BIG_(0x20A0, 0x20B8) \
859 PAIR(0x2100, 0x2101) \
860 PAIR(0x2103, 0x2106) \
861 PAIR(0x2108, 0x2109) \
862 PAIR(0x2114, 0x2114) \
863 PAIR(0x2116, 0x2118) \
864 BIG_(0x211E, 0x2123) \
865 PAIR(0x2125, 0x2125) \
866 PAIR(0x2127, 0x2127) \
867 PAIR(0x2129, 0x2129) \
868 PAIR(0x212E, 0x212E) \
869 PAIR(0x213A, 0x213B) \
870 BIG_(0x2140, 0x2144) \
871 PAIR(0x214A, 0x214D) \
872 BIG_(0x2150, 0x215F) \
873 PAIR(0x2189, 0x2189) \
874 BIG_(0x2190, 0x2335) \
875 BIG_(0x237B, 0x2394) \
876 BIG_(0x2396, 0x23E8) \
877 BIG_(0x2400, 0x2426) \
878 BIG_(0x2440, 0x244A) \
879 BIG_(0x2460, 0x249B) \
880 BIG_(0x24EA, 0x26AB) \
881 BIG_(0x26AD, 0x26CD) \
882 BIG_(0x26CF, 0x26E1) \
883 PAIR(0x26E3, 0x26E3) \
884 BIG_(0x26E8, 0x26FF) \
885 PAIR(0x2701, 0x2704) \
886 PAIR(0x2706, 0x2709) \
887 BIG_(0x270C, 0x2727) \
888 BIG_(0x2729, 0x274B) \
889 PAIR(0x274D, 0x274D) \
890 PAIR(0x274F, 0x2752) \
891 BIG_(0x2756, 0x275E) \
892 BIG_(0x2761, 0x2794) \
893 BIG_(0x2798, 0x27AF) \
894 BIG_(0x27B1, 0x27BE) \
895 BIG_(0x27C0, 0x27CA) \
896 PAIR(0x27CC, 0x27CC) \
897 BIG_(0x27D0, 0x27FF) \
898 BIG_(0x2900, 0x2B4C) \
899 BIG_(0x2B50, 0x2B59) \
900 BIG_(0x2CE5, 0x2CEA) \
901 BIG_(0x2CF9, 0x2CFF) \
902 BIG_(0x2E00, 0x2E99) \
903 BIG_(0x2E9B, 0x2EF3) \
904 BIG_(0x2F00, 0x2FD5) \
905 BIG_(0x2FF0, 0x2FFB) \
906 BIG_(0x3000, 0x3004) \
907 BIG_(0x3008, 0x3020) \
908 PAIR(0x3030, 0x3030) \
909 PAIR(0x3036, 0x3037) \
910 PAIR(0x303D, 0x303D) \
911 PAIR(0x303E, 0x303F) \
912 PAIR(0x309B, 0x309C) \
913 PAIR(0x30A0, 0x30A0) \
914 PAIR(0x30FB, 0x30FB) \
915 BIG_(0x31C0, 0x31E3) \
916 PAIR(0x321D, 0x321E) \
917 BIG_(0x3250, 0x325F) \
918 PAIR(0x327C, 0x327E) \
919 BIG_(0x32B1, 0x32BF) \
920 PAIR(0x32CC, 0x32CF) \
921 PAIR(0x3377, 0x337A) \
922 PAIR(0x33DE, 0x33DF) \
923 PAIR(0x33FF, 0x33FF) \
924 BIG_(0x4DC0, 0x4DFF) \
925 BIG_(0xA490, 0xA4C6) \
926 BIG_(0xA60D, 0xA60F) \
927 BIG_(0xA673, 0xA673) \
928 BIG_(0xA67E, 0xA67F) \
929 BIG_(0xA700, 0xA721) \
930 BIG_(0xA788, 0xA788) \
931 BIG_(0xA828, 0xA82B) \
932 BIG_(0xA838, 0xA839) \
933 BIG_(0xA874, 0xA877) \
934 BIG_(0xFB29, 0xFB29) \
935 BIG_(0xFD3E, 0xFD3F) \
936 BIG_(0xFDFD, 0xFDFD) \
937 BIG_(0xFE10, 0xFE19) \
938 BIG_(0xFE30, 0xFE52) \
939 BIG_(0xFE54, 0xFE66) \
940 BIG_(0xFE68, 0xFE6B) \
941 BIG_(0xFF01, 0xFF20) \
942 BIG_(0xFF3B, 0xFF40) \
943 BIG_(0xFF5B, 0xFF65) \
944 BIG_(0xFFE0, 0xFFE6) \
945 BIG_(0xFFE8, 0xFFEE) \
946 BIG_(0xFFF9, 0xFFFD)
947 /*
948 {0x10101, 0x10101},
949 {0x10140, 0x1019B},
950 {0x1091F, 0x1091F},
951 {0x10B39, 0x10B3F},
952 {0x10E60, 0x10E7E},
953 {0x1D200, 0x1D241},
954 {0x1D245, 0x1D245},
955 {0x1D300, 0x1D356},
956 {0x1D6DB, 0x1D6DB},
957 {0x1D715, 0x1D715},
958 {0x1D74F, 0x1D74F},
959 {0x1D789, 0x1D789},
960 {0x1D7C3, 0x1D7C3},
961 {0x1D7CE, 0x1D7FF},
962 {0x1F000, 0x1F02B},
963 {0x1F030, 0x1F093},
964 {0x1F100, 0x1F10A}
965 */
966 static const struct interval neutral_b[] = { ARRAY };
967# undef BIG_
968# undef PAIR
969# define BIG_(a,b)
970# define PAIR(a,b) (a << 2) | (b-a),
971 static const uint16_t neutral_p[] = { ARRAY };
972# undef BIG_
973# undef PAIR
974# define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
975# define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
976 struct CHECK { ARRAY };
977# undef BIG_
978# undef PAIR
979# undef ARRAY
980
981 if (in_interval_table(wc, neutral_b, ARRAY_SIZE(neutral_b) - 1))
982 return 1;
983 if (in_uint16_table(wc, neutral_p, ARRAY_SIZE(neutral_p) - 1))
984 return 1;
985 return 0;
986}
987# endif
988
989# endif /* UNICODE_BIDI_SUPPORT */
990
991#endif /* Homegrown Unicode support */
992
993
994/* The rest is mostly same for libc and for "homegrown" support */
995
996size_t FAST_FUNC unicode_strlen(const char *string)
997{
998 size_t width = mbstowcs(NULL, string, INT_MAX);
999 if (width == (size_t)-1L)
1000 return strlen(string);
1001 return width;
1002}
1003
1004size_t FAST_FUNC unicode_strwidth(const char *string)
1005{
1006 uni_stat_t uni_stat;
1007 printable_string(&uni_stat, string);
1008 return uni_stat.unicode_width;
1009}
1010
1011static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags)
1012{
1013 char *dst;
1014 unsigned dst_len;
1015 unsigned uni_count;
1016 unsigned uni_width;
1017
1018 if (unicode_status != UNICODE_ON) {
1019 char *d;
1020 if (flags & UNI_FLAG_PAD) {
1021 d = dst = xmalloc(width + 1);
1022 while ((int)--width >= 0) {
1023 unsigned char c = *src;
1024 if (c == '\0') {
1025 do
1026 *d++ = ' ';
1027 while ((int)--width >= 0);
1028 break;
1029 }
1030 *d++ = (c >= ' ' && c < 0x7f) ? c : '?';
1031 src++;
1032 }
1033 *d = '\0';
1034 } else {
1035 d = dst = xstrndup(src, width);
1036 while (*d) {
1037#if !ENABLE_UNICODE_PRESERVE_BROKEN /* Unicode checks are not working in 1.19.0 but can be displayed if not filtered */
1038 unsigned char c = *d;
1039 if (c < ' ' || c >= 0x7f)
1040 *d = '?';
1041#endif
1042 d++;
1043 }
1044 }
1045 if (stats) {
1046 stats->byte_count = (d - dst);
1047 stats->unicode_count = (d - dst);
1048 stats->unicode_width = (d - dst);
1049 }
1050 return dst;
1051 }
1052
1053 dst = NULL;
1054 uni_count = uni_width = 0;
1055 dst_len = 0;
1056 while (1) {
1057 int w;
1058 wchar_t wc;
1059
1060#if ENABLE_UNICODE_USING_LOCALE
1061 {
1062 mbstate_t mbst = { 0 };
1063 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
1064 /* If invalid sequence is seen: -1 is returned,
1065 * src points to the invalid sequence, errno = EILSEQ.
1066 * Else number of wchars (excluding terminating L'\0')
1067 * written to dest is returned.
1068 * If len (here: 1) non-L'\0' wchars stored at dest,
1069 * src points to the next char to be converted.
1070 * If string is completely converted: src = NULL.
1071 */
1072 if (rc == 0) /* end-of-string */
1073 break;
1074 if (rc < 0) { /* error */
1075 src++;
1076 goto subst;
1077 }
1078 if (!iswprint(wc))
1079 goto subst;
1080 }
1081#else
1082 src = mbstowc_internal(&wc, src);
1083 /* src is advanced to next mb char
1084 * wc == ERROR_WCHAR: invalid sequence is seen
1085 * else: wc is set
1086 */
1087 if (wc == ERROR_WCHAR) /* error */
1088 goto subst;
1089 if (wc == 0) /* end-of-string */
1090 break;
1091#endif
1092 if (CONFIG_LAST_SUPPORTED_WCHAR && wc > CONFIG_LAST_SUPPORTED_WCHAR)
1093 goto subst;
1094 w = wcwidth(wc);
1095 if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0) /* non-printable wchar */
1096 || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
1097 || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
1098 ) {
1099 subst:
1100 wc = CONFIG_SUBST_WCHAR;
1101 w = 1;
1102 }
1103 width -= w;
1104 /* Note: if width == 0, we still may add more chars,
1105 * they may be zero-width or combining ones */
1106 if ((int)width < 0) {
1107 /* can't add this wc, string would become longer than width */
1108 width += w;
1109 break;
1110 }
1111
1112 uni_count++;
1113 uni_width += w;
1114 dst = xrealloc(dst, dst_len + MB_CUR_MAX);
1115#if ENABLE_UNICODE_USING_LOCALE
1116 {
1117 mbstate_t mbst = { 0 };
1118 dst_len += wcrtomb(&dst[dst_len], wc, &mbst);
1119 }
1120#else
1121 dst_len += wcrtomb_internal(&dst[dst_len], wc);
1122#endif
1123 }
1124
1125 /* Pad to remaining width */
1126 if (flags & UNI_FLAG_PAD) {
1127 dst = xrealloc(dst, dst_len + width + 1);
1128 uni_count += width;
1129 uni_width += width;
1130 while ((int)--width >= 0) {
1131 dst[dst_len++] = ' ';
1132 }
1133 }
1134 dst[dst_len] = '\0';
1135 if (stats) {
1136 stats->byte_count = dst_len;
1137 stats->unicode_count = uni_count;
1138 stats->unicode_width = uni_width;
1139 }
1140
1141 return dst;
1142}
1143char* FAST_FUNC unicode_conv_to_printable(uni_stat_t *stats, const char *src)
1144{
1145 return unicode_conv_to_printable2(stats, src, INT_MAX, 0);
1146}
1147char* FAST_FUNC unicode_conv_to_printable_fixedwidth(/*uni_stat_t *stats,*/ const char *src, unsigned width)
1148{
1149 return unicode_conv_to_printable2(/*stats:*/ NULL, src, width, UNI_FLAG_PAD);
1150}
1151
1152#ifdef UNUSED
1153char* FAST_FUNC unicode_conv_to_printable_maxwidth(uni_stat_t *stats, const char *src, unsigned maxwidth)
1154{
1155 return unicode_conv_to_printable2(stats, src, maxwidth, 0);
1156}
1157
1158unsigned FAST_FUNC unicode_padding_to_width(unsigned width, const char *src)
1159{
1160 if (unicode_status != UNICODE_ON) {
1161 return width - strnlen(src, width);
1162 }
1163
1164 while (1) {
1165 int w;
1166 wchar_t wc;
1167
1168#if ENABLE_UNICODE_USING_LOCALE
1169 {
1170 mbstate_t mbst = { 0 };
1171 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
1172 if (rc <= 0) /* error, or end-of-string */
1173 return width;
1174 }
1175#else
1176 src = mbstowc_internal(&wc, src);
1177 if (wc == ERROR_WCHAR || wc == 0) /* error, or end-of-string */
1178 return width;
1179#endif
1180 w = wcwidth(wc);
1181 if (w < 0) /* non-printable wchar */
1182 return width;
1183 width -= w;
1184 if ((int)width <= 0) /* string is longer than width */
1185 return 0;
1186 }
1187}
1188#endif
1189