1 /* vi: set sw=4 ts=4: */
3 * Unicode support routines.
5 * Copyright (C) 2009 Denys Vlasenko
7 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
12 /* If it's not #defined as a constant in unicode.h... */
13 #ifndef unicode_status
14 uint8_t unicode_status;
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:
22 #if ENABLE_UNICODE_USING_LOCALE
24 /* Unicode support using libc locale support. */
26 void FAST_FUNC init_unicode(void)
28 /* In unicode, this is a one character string */
29 static const char unicode_0x394[] = { 0xce, 0x94, 0 };
31 if (unicode_status != UNICODE_UNKNOWN)
34 unicode_status = unicode_strlen(unicode_0x394) == 1 ? UNICODE_ON : UNICODE_OFF;
39 /* Homegrown Unicode support. It knows only C and Unicode locales. */
41 # if ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
42 void FAST_FUNC init_unicode(void)
46 if (unicode_status != UNICODE_UNKNOWN)
49 unicode_status = UNICODE_OFF;
50 lang = getenv("LANG");
51 if (!lang || !(strstr(lang, ".utf") || strstr(lang, ".UTF")))
53 unicode_status = UNICODE_ON;
57 static size_t wcrtomb_internal(char *s, wchar_t wc)
67 /* RFC 3629 says that Unicode ends at 10FFFF,
68 * but we cover entire 32 bits */
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 */
76 /* How many bytes do we need? */
78 /* (0x80000000+ would result in n = 7, limiting n to 6) */
79 while (v >= 0x800 && n < 6) {
83 /* Fill bytes n-1..1 */
86 s[i] = (wc & 0x3f) | 0x80;
90 s[0] = wc | (uint8_t)(0x3f00 >> n);
93 size_t FAST_FUNC wcrtomb(char *s, wchar_t wc, mbstate_t *ps UNUSED_PARAM)
95 if (unicode_status != UNICODE_ON) {
100 return wcrtomb_internal(s, wc);
102 size_t FAST_FUNC wcstombs(char *dest, const wchar_t *src, size_t n)
106 if (unicode_status != UNICODE_ON) {
117 while (n >= MB_CUR_MAX) {
119 size_t len = wcrtomb_internal(dest, wc);
127 char tbuf[MB_CUR_MAX];
129 size_t len = wcrtomb_internal(tbuf, wc);
133 memcpy(dest, tbuf, len);
142 # define ERROR_WCHAR (~(wchar_t)0)
144 static const char *mbstowc_internal(wchar_t *res, const char *src)
147 unsigned c = (unsigned char) *src++;
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 */
163 } while ((c & 0x80) && bytes < 6);
165 /* A bare "continuation" byte. Say, 80 */
169 c = (uint8_t)(c) >> bytes;
172 unsigned ch = (unsigned char) *src;
173 if ((ch & 0xc0) != 0x80) {
174 /* Missing "continuation" byte. Example: e0 80 */
178 c = (c << 6) + (ch & 0x3f);
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 */
195 size_t FAST_FUNC mbstowcs(wchar_t *dest, const char *src, size_t n)
199 if (unicode_status != UNICODE_ON) {
201 unsigned char c = *src++;
214 src = mbstowc_internal(&wc, src);
215 if (wc == ERROR_WCHAR) /* error */
219 if (wc == 0) /* end-of-string */
227 int FAST_FUNC iswspace(wint_t wc)
229 return (unsigned)wc <= 0x7f && isspace(wc);
232 int FAST_FUNC iswalnum(wint_t wc)
234 return (unsigned)wc <= 0x7f && isalnum(wc);
237 int FAST_FUNC iswpunct(wint_t wc)
239 return (unsigned)wc <= 0x7f && ispunct(wc);
243 # if LAST_SUPPORTED_WCHAR >= 0x300
249 /* auxiliary function for binary search in interval table */
250 static int in_interval_table(unsigned ucs, const struct interval *table, unsigned max)
255 if (ucs < table[0].first || ucs > table[max].last)
260 mid = (min + max) / 2;
261 if (ucs > table[mid].last)
263 else if (ucs < table[mid].first)
271 static int in_uint16_table(unsigned ucs, const uint16_t *table, unsigned max)
275 unsigned first, last;
277 first = table[0] >> 2;
278 last = first + (table[0] & 3);
279 if (ucs < first || ucs > last)
284 mid = (min + max) / 2;
285 first = table[mid] >> 2;
286 last = first + (table[mid] & 3);
289 else if (ucs < first)
300 * This is an implementation of wcwidth() and wcswidth() (defined in
301 * IEEE Std 1002.1-2001) for Unicode.
303 * http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
304 * http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html
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.
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.
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.
349 * http://www.unicode.org/unicode/reports/tr11/
351 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
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.
357 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
360 /* Assigned Unicode character ranges:
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
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."
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.
387 * Tertiary Ideographic Plane is also ignored for now,
388 * until Unicode committee assigns something there.
390 /* The following two functions define the column width of an ISO 10646
391 * character as follows:
393 * - The null character (U+0000) has a column width of 0.
395 * - Other C0/C1 control characters and DEL will lead to a return
398 * - Non-spacing and enclosing combining characters (general
399 * category code Mn or Me in the Unicode database) have a
402 * - SOFT HYPHEN (U+00AD) has a column width of 1.
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.
407 * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
408 * have a column width of 0.
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.
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.
418 * This implementation assumes that wchar_t characters are encoded
421 static int wcwidth(unsigned ucs)
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 static const struct interval combining[] = {
427 # define BIG_(a,b) { a, b },
429 # define ARRAY /* PAIR if < 0x4000 and no more than 4 chars big */ \
430 BIG_(0x0300, 0x036F) \
431 PAIR(0x0483, 0x0486) \
432 PAIR(0x0488, 0x0489) \
433 BIG_(0x0591, 0x05BD) \
434 PAIR(0x05BF, 0x05BF) \
435 PAIR(0x05C1, 0x05C2) \
436 PAIR(0x05C4, 0x05C5) \
437 PAIR(0x05C7, 0x05C7) \
438 PAIR(0x0600, 0x0603) \
439 BIG_(0x0610, 0x0615) \
440 BIG_(0x064B, 0x065E) \
441 PAIR(0x0670, 0x0670) \
442 BIG_(0x06D6, 0x06E4) \
443 PAIR(0x06E7, 0x06E8) \
444 PAIR(0x06EA, 0x06ED) \
445 PAIR(0x070F, 0x070F) \
446 PAIR(0x0711, 0x0711) \
447 BIG_(0x0730, 0x074A) \
448 BIG_(0x07A6, 0x07B0) \
449 BIG_(0x07EB, 0x07F3) \
450 PAIR(0x0901, 0x0902) \
451 PAIR(0x093C, 0x093C) \
452 BIG_(0x0941, 0x0948) \
453 PAIR(0x094D, 0x094D) \
454 PAIR(0x0951, 0x0954) \
455 PAIR(0x0962, 0x0963) \
456 PAIR(0x0981, 0x0981) \
457 PAIR(0x09BC, 0x09BC) \
458 PAIR(0x09C1, 0x09C4) \
459 PAIR(0x09CD, 0x09CD) \
460 PAIR(0x09E2, 0x09E3) \
461 PAIR(0x0A01, 0x0A02) \
462 PAIR(0x0A3C, 0x0A3C) \
463 PAIR(0x0A41, 0x0A42) \
464 PAIR(0x0A47, 0x0A48) \
465 PAIR(0x0A4B, 0x0A4D) \
466 PAIR(0x0A70, 0x0A71) \
467 PAIR(0x0A81, 0x0A82) \
468 PAIR(0x0ABC, 0x0ABC) \
469 BIG_(0x0AC1, 0x0AC5) \
470 PAIR(0x0AC7, 0x0AC8) \
471 PAIR(0x0ACD, 0x0ACD) \
472 PAIR(0x0AE2, 0x0AE3) \
473 PAIR(0x0B01, 0x0B01) \
474 PAIR(0x0B3C, 0x0B3C) \
475 PAIR(0x0B3F, 0x0B3F) \
476 PAIR(0x0B41, 0x0B43) \
477 PAIR(0x0B4D, 0x0B4D) \
478 PAIR(0x0B56, 0x0B56) \
479 PAIR(0x0B82, 0x0B82) \
480 PAIR(0x0BC0, 0x0BC0) \
481 PAIR(0x0BCD, 0x0BCD) \
482 PAIR(0x0C3E, 0x0C40) \
483 PAIR(0x0C46, 0x0C48) \
484 PAIR(0x0C4A, 0x0C4D) \
485 PAIR(0x0C55, 0x0C56) \
486 PAIR(0x0CBC, 0x0CBC) \
487 PAIR(0x0CBF, 0x0CBF) \
488 PAIR(0x0CC6, 0x0CC6) \
489 PAIR(0x0CCC, 0x0CCD) \
490 PAIR(0x0CE2, 0x0CE3) \
491 PAIR(0x0D41, 0x0D43) \
492 PAIR(0x0D4D, 0x0D4D) \
493 PAIR(0x0DCA, 0x0DCA) \
494 PAIR(0x0DD2, 0x0DD4) \
495 PAIR(0x0DD6, 0x0DD6) \
496 PAIR(0x0E31, 0x0E31) \
497 BIG_(0x0E34, 0x0E3A) \
498 BIG_(0x0E47, 0x0E4E) \
499 PAIR(0x0EB1, 0x0EB1) \
500 BIG_(0x0EB4, 0x0EB9) \
501 PAIR(0x0EBB, 0x0EBC) \
502 BIG_(0x0EC8, 0x0ECD) \
503 PAIR(0x0F18, 0x0F19) \
504 PAIR(0x0F35, 0x0F35) \
505 PAIR(0x0F37, 0x0F37) \
506 PAIR(0x0F39, 0x0F39) \
507 BIG_(0x0F71, 0x0F7E) \
508 BIG_(0x0F80, 0x0F84) \
509 PAIR(0x0F86, 0x0F87) \
510 PAIR(0x0FC6, 0x0FC6) \
511 BIG_(0x0F90, 0x0F97) \
512 BIG_(0x0F99, 0x0FBC) \
513 PAIR(0x102D, 0x1030) \
514 PAIR(0x1032, 0x1032) \
515 PAIR(0x1036, 0x1037) \
516 PAIR(0x1039, 0x1039) \
517 PAIR(0x1058, 0x1059) \
518 BIG_(0x1160, 0x11FF) \
519 PAIR(0x135F, 0x135F) \
520 PAIR(0x1712, 0x1714) \
521 PAIR(0x1732, 0x1734) \
522 PAIR(0x1752, 0x1753) \
523 PAIR(0x1772, 0x1773) \
524 PAIR(0x17B4, 0x17B5) \
525 BIG_(0x17B7, 0x17BD) \
526 PAIR(0x17C6, 0x17C6) \
527 BIG_(0x17C9, 0x17D3) \
528 PAIR(0x17DD, 0x17DD) \
529 PAIR(0x180B, 0x180D) \
530 PAIR(0x18A9, 0x18A9) \
531 PAIR(0x1920, 0x1922) \
532 PAIR(0x1927, 0x1928) \
533 PAIR(0x1932, 0x1932) \
534 PAIR(0x1939, 0x193B) \
535 PAIR(0x1A17, 0x1A18) \
536 PAIR(0x1B00, 0x1B03) \
537 PAIR(0x1B34, 0x1B34) \
538 BIG_(0x1B36, 0x1B3A) \
539 PAIR(0x1B3C, 0x1B3C) \
540 PAIR(0x1B42, 0x1B42) \
541 BIG_(0x1B6B, 0x1B73) \
542 BIG_(0x1DC0, 0x1DCA) \
543 PAIR(0x1DFE, 0x1DFF) \
544 BIG_(0x200B, 0x200F) \
545 BIG_(0x202A, 0x202E) \
546 PAIR(0x2060, 0x2063) \
547 BIG_(0x206A, 0x206F) \
548 BIG_(0x20D0, 0x20EF) \
549 BIG_(0x302A, 0x302F) \
550 PAIR(0x3099, 0x309A) \
551 /* Too big to be packed in PAIRs: */ \
552 BIG_(0xA806, 0xA806) \
553 BIG_(0xA80B, 0xA80B) \
554 BIG_(0xA825, 0xA826) \
555 BIG_(0xFB1E, 0xFB1E) \
556 BIG_(0xFE00, 0xFE0F) \
557 BIG_(0xFE20, 0xFE23) \
558 BIG_(0xFEFF, 0xFEFF) \
565 # define PAIR(a,b) (a << 2) | (b-a),
566 static const uint16_t combining1[] = { ARRAY };
569 # define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
570 # define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
571 struct CHECK { ARRAY };
580 /* Test for 8-bit control characters (00-1f, 80-9f, 7f) */
581 if ((ucs & ~0x80) < 0x20 || ucs == 0x7f)
583 /* Quick abort if it is an obviously invalid char */
584 if (ucs > LAST_SUPPORTED_WCHAR)
587 /* Optimization: no combining chars below 0x300 */
588 if (LAST_SUPPORTED_WCHAR < 0x300 || ucs < 0x300)
591 # if LAST_SUPPORTED_WCHAR >= 0x300
592 /* Binary search in table of non-spacing characters */
593 if (in_interval_table(ucs, combining, ARRAY_SIZE(combining) - 1))
595 if (in_uint16_table(ucs, combining1, ARRAY_SIZE(combining1) - 1))
598 /* Optimization: all chars below 0x1100 are not double-width */
599 if (LAST_SUPPORTED_WCHAR < 0x1100 || ucs < 0x1100)
602 # if LAST_SUPPORTED_WCHAR >= 0x1100
603 /* Invalid code points: */
604 /* High (d800..dbff) and low (dc00..dfff) surrogates (valid only in UTF16) */
605 /* Private Use Area (e000..f8ff) */
606 /* Noncharacters fdd0..fdef */
607 if ((LAST_SUPPORTED_WCHAR >= 0xd800 && ucs >= 0xd800 && ucs <= 0xf8ff)
608 || (LAST_SUPPORTED_WCHAR >= 0xfdd0 && ucs >= 0xfdd0 && ucs <= 0xfdef)
612 /* 0xfffe and 0xffff in every plane are invalid */
613 if (LAST_SUPPORTED_WCHAR >= 0xfffe && ((ucs & 0xfffe) == 0xfffe)) {
617 # if LAST_SUPPORTED_WCHAR >= 0x10000
618 if (ucs >= 0x10000) {
619 /* Combining chars in Supplementary Multilingual Plane 0x1xxxx */
620 static const struct interval combining0x10000[] = {
621 { 0x0A01, 0x0A03 }, { 0x0A05, 0x0A06 }, { 0x0A0C, 0x0A0F },
622 { 0x0A38, 0x0A3A }, { 0x0A3F, 0x0A3F }, { 0xD167, 0xD169 },
623 { 0xD173, 0xD182 }, { 0xD185, 0xD18B }, { 0xD1AA, 0xD1AD },
626 /* Binary search in table of non-spacing characters in Supplementary Multilingual Plane */
627 if (in_interval_table(ucs ^ 0x10000, combining0x10000, ARRAY_SIZE(combining0x10000) - 1))
629 /* Check a few non-spacing chars in Supplementary Special-purpose Plane 0xExxxx */
630 if (LAST_SUPPORTED_WCHAR >= 0xE0001
632 || (ucs >= 0xE0020 && ucs <= 0xE007F)
633 || (ucs >= 0xE0100 && ucs <= 0xE01EF)
641 /* If we arrive here, ucs is not a combining or C0/C1 control character.
642 * Check whether it's 1 char or 2-shar wide.
645 ( (/*ucs >= 0x1100 &&*/ ucs <= 0x115f) /* Hangul Jamo init. consonants */
646 || ucs == 0x2329 /* left-pointing angle bracket; also CJK punct. char */
647 || ucs == 0x232a /* right-pointing angle bracket; also CJK punct. char */
648 || (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs != 0x303f) /* CJK ... Yi */
649 # if LAST_SUPPORTED_WCHAR >= 0xac00
650 || (ucs >= 0xac00 && ucs <= 0xd7a3) /* Hangul Syllables */
651 || (ucs >= 0xf900 && ucs <= 0xfaff) /* CJK Compatibility Ideographs */
652 || (ucs >= 0xfe10 && ucs <= 0xfe19) /* Vertical forms */
653 || (ucs >= 0xfe30 && ucs <= 0xfe6f) /* CJK Compatibility Forms */
654 || (ucs >= 0xff00 && ucs <= 0xff60) /* Fullwidth Forms */
655 || (ucs >= 0xffe0 && ucs <= 0xffe6)
656 || ((ucs >> 17) == (2 >> 1)) /* 20000..3ffff: Supplementary and Tertiary Ideographic Planes */
659 # endif /* >= 0x1100 */
660 # endif /* >= 0x300 */
664 # if ENABLE_UNICODE_BIDI_SUPPORT
665 int FAST_FUNC unicode_bidi_isrtl(wint_t wc)
668 * http://www.unicode.org/Public/5.2.0/ucd/extracted/DerivedBidiClass.txt
669 * Bidi_Class=Left_To_Right | Bidi_Class=Arabic_Letter
671 static const struct interval rtl_b[] = {
672 # define BIG_(a,b) { a, b },
675 PAIR(0x0590, 0x0590) \
676 PAIR(0x05BE, 0x05BE) \
677 PAIR(0x05C0, 0x05C0) \
678 PAIR(0x05C3, 0x05C3) \
679 PAIR(0x05C6, 0x05C6) \
680 BIG_(0x05C8, 0x05FF) \
681 PAIR(0x0604, 0x0605) \
682 PAIR(0x0608, 0x0608) \
683 PAIR(0x060B, 0x060B) \
684 PAIR(0x060D, 0x060D) \
685 BIG_(0x061B, 0x064A) \
686 PAIR(0x065F, 0x065F) \
687 PAIR(0x066D, 0x066F) \
688 BIG_(0x0671, 0x06D5) \
689 PAIR(0x06E5, 0x06E6) \
690 PAIR(0x06EE, 0x06EF) \
691 BIG_(0x06FA, 0x070E) \
692 PAIR(0x0710, 0x0710) \
693 BIG_(0x0712, 0x072F) \
694 BIG_(0x074B, 0x07A5) \
695 BIG_(0x07B1, 0x07EA) \
696 PAIR(0x07F4, 0x07F5) \
697 BIG_(0x07FA, 0x0815) \
698 PAIR(0x081A, 0x081A) \
699 PAIR(0x0824, 0x0824) \
700 PAIR(0x0828, 0x0828) \
701 BIG_(0x082E, 0x08FF) \
702 PAIR(0x200F, 0x200F) \
703 PAIR(0x202B, 0x202B) \
704 PAIR(0x202E, 0x202E) \
705 BIG_(0xFB1D, 0xFB1D) \
706 BIG_(0xFB1F, 0xFB28) \
707 BIG_(0xFB2A, 0xFD3D) \
708 BIG_(0xFD40, 0xFDCF) \
709 BIG_(0xFDC8, 0xFDCF) \
710 BIG_(0xFDF0, 0xFDFC) \
711 BIG_(0xFDFE, 0xFDFF) \
713 /* Probably not necessary
731 # define PAIR(a,b) (a << 2) | (b-a),
732 static const uint16_t rtl_p[] = { ARRAY };
735 # define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
736 # define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
737 struct CHECK { ARRAY };
742 if (in_interval_table(wc, rtl_b, ARRAY_SIZE(rtl_b) - 1))
744 if (in_uint16_table(wc, rtl_p, ARRAY_SIZE(rtl_p) - 1))
749 # if ENABLE_UNICODE_NEUTRAL_TABLE
750 int FAST_FUNC unicode_bidi_is_neutral_wchar(wint_t wc)
753 * http://www.unicode.org/Public/5.2.0/ucd/extracted/DerivedBidiClass.txt
754 * Bidi_Classes: Paragraph_Separator, Segment_Separator,
755 * White_Space, Other_Neutral, European_Number, European_Separator,
756 * European_Terminator, Arabic_Number, Common_Separator
758 static const struct interval neutral_b[] = {
759 # define BIG_(a,b) { a, b },
762 BIG_(0x0009, 0x000D) \
763 BIG_(0x001C, 0x0040) \
764 BIG_(0x005B, 0x0060) \
765 PAIR(0x007B, 0x007E) \
766 PAIR(0x0085, 0x0085) \
767 BIG_(0x00A0, 0x00A9) \
768 PAIR(0x00AB, 0x00AC) \
769 BIG_(0x00AE, 0x00B4) \
770 PAIR(0x00B6, 0x00B9) \
771 BIG_(0x00BB, 0x00BF) \
772 PAIR(0x00D7, 0x00D7) \
773 PAIR(0x00F7, 0x00F7) \
774 PAIR(0x02B9, 0x02BA) \
775 BIG_(0x02C2, 0x02CF) \
776 BIG_(0x02D2, 0x02DF) \
777 BIG_(0x02E5, 0x02FF) \
778 PAIR(0x0374, 0x0375) \
779 PAIR(0x037E, 0x037E) \
780 PAIR(0x0384, 0x0385) \
781 PAIR(0x0387, 0x0387) \
782 PAIR(0x03F6, 0x03F6) \
783 PAIR(0x058A, 0x058A) \
784 PAIR(0x0600, 0x0603) \
785 PAIR(0x0606, 0x0607) \
786 PAIR(0x0609, 0x060A) \
787 PAIR(0x060C, 0x060C) \
788 PAIR(0x060E, 0x060F) \
789 BIG_(0x0660, 0x066C) \
790 PAIR(0x06DD, 0x06DD) \
791 PAIR(0x06E9, 0x06E9) \
792 BIG_(0x06F0, 0x06F9) \
793 PAIR(0x07F6, 0x07F9) \
794 PAIR(0x09F2, 0x09F3) \
795 PAIR(0x09FB, 0x09FB) \
796 PAIR(0x0AF1, 0x0AF1) \
797 BIG_(0x0BF3, 0x0BFA) \
798 BIG_(0x0C78, 0x0C7E) \
799 PAIR(0x0CF1, 0x0CF2) \
800 PAIR(0x0E3F, 0x0E3F) \
801 PAIR(0x0F3A, 0x0F3D) \
802 BIG_(0x1390, 0x1400) \
803 PAIR(0x1680, 0x1680) \
804 PAIR(0x169B, 0x169C) \
805 PAIR(0x17DB, 0x17DB) \
806 BIG_(0x17F0, 0x17F9) \
807 BIG_(0x1800, 0x180A) \
808 PAIR(0x180E, 0x180E) \
809 PAIR(0x1940, 0x1940) \
810 PAIR(0x1944, 0x1945) \
811 BIG_(0x19DE, 0x19FF) \
812 PAIR(0x1FBD, 0x1FBD) \
813 PAIR(0x1FBF, 0x1FC1) \
814 PAIR(0x1FCD, 0x1FCF) \
815 PAIR(0x1FDD, 0x1FDF) \
816 PAIR(0x1FED, 0x1FEF) \
817 PAIR(0x1FFD, 0x1FFE) \
818 BIG_(0x2000, 0x200A) \
819 BIG_(0x2010, 0x2029) \
820 BIG_(0x202F, 0x205F) \
821 PAIR(0x2070, 0x2070) \
822 BIG_(0x2074, 0x207E) \
823 BIG_(0x2080, 0x208E) \
824 BIG_(0x20A0, 0x20B8) \
825 PAIR(0x2100, 0x2101) \
826 PAIR(0x2103, 0x2106) \
827 PAIR(0x2108, 0x2109) \
828 PAIR(0x2114, 0x2114) \
829 PAIR(0x2116, 0x2118) \
830 BIG_(0x211E, 0x2123) \
831 PAIR(0x2125, 0x2125) \
832 PAIR(0x2127, 0x2127) \
833 PAIR(0x2129, 0x2129) \
834 PAIR(0x212E, 0x212E) \
835 PAIR(0x213A, 0x213B) \
836 BIG_(0x2140, 0x2144) \
837 PAIR(0x214A, 0x214D) \
838 BIG_(0x2150, 0x215F) \
839 PAIR(0x2189, 0x2189) \
840 BIG_(0x2190, 0x2335) \
841 BIG_(0x237B, 0x2394) \
842 BIG_(0x2396, 0x23E8) \
843 BIG_(0x2400, 0x2426) \
844 BIG_(0x2440, 0x244A) \
845 BIG_(0x2460, 0x249B) \
846 BIG_(0x24EA, 0x26AB) \
847 BIG_(0x26AD, 0x26CD) \
848 BIG_(0x26CF, 0x26E1) \
849 PAIR(0x26E3, 0x26E3) \
850 BIG_(0x26E8, 0x26FF) \
851 PAIR(0x2701, 0x2704) \
852 PAIR(0x2706, 0x2709) \
853 BIG_(0x270C, 0x2727) \
854 BIG_(0x2729, 0x274B) \
855 PAIR(0x274D, 0x274D) \
856 PAIR(0x274F, 0x2752) \
857 BIG_(0x2756, 0x275E) \
858 BIG_(0x2761, 0x2794) \
859 BIG_(0x2798, 0x27AF) \
860 BIG_(0x27B1, 0x27BE) \
861 BIG_(0x27C0, 0x27CA) \
862 PAIR(0x27CC, 0x27CC) \
863 BIG_(0x27D0, 0x27FF) \
864 BIG_(0x2900, 0x2B4C) \
865 BIG_(0x2B50, 0x2B59) \
866 BIG_(0x2CE5, 0x2CEA) \
867 BIG_(0x2CF9, 0x2CFF) \
868 BIG_(0x2E00, 0x2E99) \
869 BIG_(0x2E9B, 0x2EF3) \
870 BIG_(0x2F00, 0x2FD5) \
871 BIG_(0x2FF0, 0x2FFB) \
872 BIG_(0x3000, 0x3004) \
873 BIG_(0x3008, 0x3020) \
874 PAIR(0x3030, 0x3030) \
875 PAIR(0x3036, 0x3037) \
876 PAIR(0x303D, 0x303D) \
877 PAIR(0x303E, 0x303F) \
878 PAIR(0x309B, 0x309C) \
879 PAIR(0x30A0, 0x30A0) \
880 PAIR(0x30FB, 0x30FB) \
881 BIG_(0x31C0, 0x31E3) \
882 PAIR(0x321D, 0x321E) \
883 BIG_(0x3250, 0x325F) \
884 PAIR(0x327C, 0x327E) \
885 BIG_(0x32B1, 0x32BF) \
886 PAIR(0x32CC, 0x32CF) \
887 PAIR(0x3377, 0x337A) \
888 PAIR(0x33DE, 0x33DF) \
889 PAIR(0x33FF, 0x33FF) \
890 BIG_(0x4DC0, 0x4DFF) \
891 BIG_(0xA490, 0xA4C6) \
892 BIG_(0xA60D, 0xA60F) \
893 BIG_(0xA673, 0xA673) \
894 BIG_(0xA67E, 0xA67F) \
895 BIG_(0xA700, 0xA721) \
896 BIG_(0xA788, 0xA788) \
897 BIG_(0xA828, 0xA82B) \
898 BIG_(0xA838, 0xA839) \
899 BIG_(0xA874, 0xA877) \
900 BIG_(0xFB29, 0xFB29) \
901 BIG_(0xFD3E, 0xFD3F) \
902 BIG_(0xFDFD, 0xFDFD) \
903 BIG_(0xFE10, 0xFE19) \
904 BIG_(0xFE30, 0xFE52) \
905 BIG_(0xFE54, 0xFE66) \
906 BIG_(0xFE68, 0xFE6B) \
907 BIG_(0xFF01, 0xFF20) \
908 BIG_(0xFF3B, 0xFF40) \
909 BIG_(0xFF5B, 0xFF65) \
910 BIG_(0xFFE0, 0xFFE6) \
911 BIG_(0xFFE8, 0xFFEE) \
937 # define PAIR(a,b) (a << 2) | (b-a),
938 static const uint16_t neutral_p[] = { ARRAY };
941 # define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
942 # define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
943 struct CHECK { ARRAY };
948 if (in_interval_table(wc, neutral_b, ARRAY_SIZE(neutral_b) - 1))
950 if (in_uint16_table(wc, neutral_p, ARRAY_SIZE(neutral_p) - 1))
956 # endif /* UNICODE_BIDI_SUPPORT */
958 #endif /* Homegrown Unicode support */
961 /* The rest is mostly same for libc and for "homegrown" support */
963 size_t FAST_FUNC unicode_strlen(const char *string)
965 size_t width = mbstowcs(NULL, string, INT_MAX);
966 if (width == (size_t)-1L)
967 return strlen(string);
971 static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags)
978 if (unicode_status != UNICODE_ON) {
980 if (flags & UNI_FLAG_PAD) {
981 d = dst = xmalloc(width + 1);
982 while ((int)--width >= 0) {
983 unsigned char c = *src;
987 while ((int)--width >= 0);
990 *d++ = (c >= ' ' && c < 0x7f) ? c : '?';
995 d = dst = xstrndup(src, width);
997 unsigned char c = *d;
998 if (c < ' ' || c >= 0x7f)
1004 stats->byte_count = stats->unicode_count = (d - dst);
1009 uni_count = uni_width = 0;
1015 #if ENABLE_UNICODE_USING_LOCALE
1017 mbstate_t mbst = { 0 };
1018 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
1019 /* If invalid sequence is seen: -1 is returned,
1020 * src points to the invalid sequence, errno = EILSEQ.
1021 * Else number of wchars (excluding terminating L'\0')
1022 * written to dest is returned.
1023 * If len (here: 1) non-L'\0' wchars stored at dest,
1024 * src points to the next char to be converted.
1025 * If string is completely converted: src = NULL.
1027 if (rc == 0) /* end-of-string */
1029 if (rc < 0) { /* error */
1037 src = mbstowc_internal(&wc, src);
1038 /* src is advanced to next mb char
1039 * wc == ERROR_WCHAR: invalid sequence is seen
1042 if (wc == ERROR_WCHAR) /* error */
1044 if (wc == 0) /* end-of-string */
1047 if (CONFIG_LAST_SUPPORTED_WCHAR && wc > CONFIG_LAST_SUPPORTED_WCHAR)
1050 if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0) /* non-printable wchar */
1051 || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
1052 || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
1055 wc = CONFIG_SUBST_WCHAR;
1059 /* Note: if width == 0, we still may add more chars,
1060 * they may be zero-width or combining ones */
1061 if ((int)width < 0) {
1062 /* can't add this wc, string would become longer than width */
1069 dst = xrealloc(dst, dst_len + MB_CUR_MAX);
1070 #if ENABLE_UNICODE_USING_LOCALE
1072 mbstate_t mbst = { 0 };
1073 dst_len += wcrtomb(&dst[dst_len], wc, &mbst);
1076 dst_len += wcrtomb_internal(&dst[dst_len], wc);
1080 /* Pad to remaining width */
1081 if (flags & UNI_FLAG_PAD) {
1082 dst = xrealloc(dst, dst_len + width + 1);
1085 while ((int)--width >= 0) {
1086 dst[dst_len++] = ' ';
1089 dst[dst_len] = '\0';
1091 stats->byte_count = dst_len;
1092 stats->unicode_count = uni_count;
1093 stats->unicode_width = uni_width;
1098 char* FAST_FUNC unicode_conv_to_printable(uni_stat_t *stats, const char *src)
1100 return unicode_conv_to_printable2(stats, src, INT_MAX, 0);
1102 char* FAST_FUNC unicode_conv_to_printable_maxwidth(uni_stat_t *stats, const char *src, unsigned maxwidth)
1104 return unicode_conv_to_printable2(stats, src, maxwidth, 0);
1106 char* FAST_FUNC unicode_conv_to_printable_fixedwidth(uni_stat_t *stats, const char *src, unsigned width)
1108 return unicode_conv_to_printable2(stats, src, width, UNI_FLAG_PAD);
1112 unsigned FAST_FUNC unicode_padding_to_width(unsigned width, const char *src)
1114 if (unicode_status != UNICODE_ON) {
1115 return width - strnlen(src, width);
1122 #if ENABLE_UNICODE_USING_LOCALE
1124 mbstate_t mbst = { 0 };
1125 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
1126 if (rc <= 0) /* error, or end-of-string */
1130 src = mbstowc_internal(&wc, src);
1131 if (wc == ERROR_WCHAR || wc == 0) /* error, or end-of-string */
1135 if (w < 0) /* non-printable wchar */
1138 if ((int)width <= 0) /* string is longer than width */