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