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