aboutsummaryrefslogtreecommitdiff
path: root/libbb/unicode_wcwidth.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/unicode_wcwidth.c')
-rw-r--r--libbb/unicode_wcwidth.c24
1 files changed, 22 insertions, 2 deletions
diff --git a/libbb/unicode_wcwidth.c b/libbb/unicode_wcwidth.c
index 8d301f7c3..ab62b18f6 100644
--- a/libbb/unicode_wcwidth.c
+++ b/libbb/unicode_wcwidth.c
@@ -59,6 +59,13 @@
59 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c 59 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
60 */ 60 */
61 61
62#if CONFIG_LAST_SUPPORTED_WCHAR == 0
63# define LAST_SUPPORTED_WCHAR ((1 << 31) - 1)
64#else
65# define LAST_SUPPORTED_WCHAR CONFIG_LAST_SUPPORTED_WCHAR
66#endif
67
68#if LAST_SUPPORTED_WCHAR >= 0x0300
62struct interval { 69struct interval {
63 uint16_t first; 70 uint16_t first;
64 uint16_t last; 71 uint16_t last;
@@ -111,6 +118,7 @@ static int in_uint16_table(unsigned ucs, const uint16_t *table, unsigned max)
111 } 118 }
112 return 0; 119 return 0;
113} 120}
121#endif
114 122
115 123
116/* The following two functions define the column width of an ISO 10646 124/* The following two functions define the column width of an ISO 10646
@@ -146,6 +154,7 @@ static int in_uint16_table(unsigned ucs, const uint16_t *table, unsigned max)
146 */ 154 */
147static int wcwidth(unsigned ucs) 155static int wcwidth(unsigned ucs)
148{ 156{
157#if LAST_SUPPORTED_WCHAR >= 0x0300
149 /* sorted list of non-overlapping intervals of non-spacing characters */ 158 /* sorted list of non-overlapping intervals of non-spacing characters */
150 /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */ 159 /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */
151 static const struct interval combining[] = { 160 static const struct interval combining[] = {
@@ -420,12 +429,15 @@ static int wcwidth(unsigned ucs)
420#undef BIG_ 429#undef BIG_
421#undef PAIR 430#undef PAIR
422 }; 431 };
432# if LAST_SUPPORTED_WCHAR >= 0x1100
423 static const struct interval combining0x10000[] = { 433 static const struct interval combining0x10000[] = {
424 { 0x0A01, 0x0A03 }, { 0x0A05, 0x0A06 }, { 0x0A0C, 0x0A0F }, 434 { 0x0A01, 0x0A03 }, { 0x0A05, 0x0A06 }, { 0x0A0C, 0x0A0F },
425 { 0x0A38, 0x0A3A }, { 0x0A3F, 0x0A3F }, { 0xD167, 0xD169 }, 435 { 0x0A38, 0x0A3A }, { 0x0A3F, 0x0A3F }, { 0xD167, 0xD169 },
426 { 0xD173, 0xD182 }, { 0xD185, 0xD18B }, { 0xD1AA, 0xD1AD }, 436 { 0xD173, 0xD182 }, { 0xD185, 0xD18B }, { 0xD1AA, 0xD1AD },
427 { 0xD242, 0xD244 } 437 { 0xD242, 0xD244 }
428 }; 438 };
439# endif
440#endif
429 441
430 if (ucs == 0) 442 if (ucs == 0)
431 return 0; 443 return 0;
@@ -435,6 +447,9 @@ static int wcwidth(unsigned ucs)
435 if (ucs < 0x0300) /* optimization */ 447 if (ucs < 0x0300) /* optimization */
436 return 1; 448 return 1;
437 449
450#if LAST_SUPPORTED_WCHAR < 0x0300
451 return -1;
452#else
438 /* binary search in table of non-spacing characters */ 453 /* binary search in table of non-spacing characters */
439 if (in_interval_table(ucs, combining, ARRAY_SIZE(combining) - 1)) 454 if (in_interval_table(ucs, combining, ARRAY_SIZE(combining) - 1))
440 return 0; 455 return 0;
@@ -444,6 +459,9 @@ static int wcwidth(unsigned ucs)
444 if (ucs < 0x1100) /* optimization */ 459 if (ucs < 0x1100) /* optimization */
445 return 1; 460 return 1;
446 461
462# if LAST_SUPPORTED_WCHAR < 0x1100
463 return -1;
464# else
447 /* binary search in table of non-spacing characters, cont. */ 465 /* binary search in table of non-spacing characters, cont. */
448 if (in_interval_table(ucs ^ 0x10000, combining0x10000, ARRAY_SIZE(combining0x10000) - 1)) 466 if (in_interval_table(ucs ^ 0x10000, combining0x10000, ARRAY_SIZE(combining0x10000) - 1))
449 return 0; 467 return 0;
@@ -458,8 +476,8 @@ static int wcwidth(unsigned ucs)
458 476
459 return 1 + 477 return 1 +
460 ( (/*ucs >= 0x1100 &&*/ ucs <= 0x115f) /* Hangul Jamo init. consonants */ 478 ( (/*ucs >= 0x1100 &&*/ ucs <= 0x115f) /* Hangul Jamo init. consonants */
461 || ucs == 0x2329 479 || ucs == 0x2329 /* left-pointing angle bracket; also CJK punct. char */
462 || ucs == 0x232a 480 || ucs == 0x232a /* right-pointing angle bracket; also CJK punct. char */
463 || (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs != 0x303f) /* CJK ... Yi */ 481 || (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs != 0x303f) /* CJK ... Yi */
464 || (ucs >= 0xac00 && ucs <= 0xd7a3) /* Hangul Syllables */ 482 || (ucs >= 0xac00 && ucs <= 0xd7a3) /* Hangul Syllables */
465 || (ucs >= 0xf900 && ucs <= 0xfaff) /* CJK Compatibility Ideographs */ 483 || (ucs >= 0xf900 && ucs <= 0xfaff) /* CJK Compatibility Ideographs */
@@ -470,4 +488,6 @@ static int wcwidth(unsigned ucs)
470 || (ucs >= 0x20000 && ucs <= 0x2fffd) 488 || (ucs >= 0x20000 && ucs <= 0x2fffd)
471 || (ucs >= 0x30000 && ucs <= 0x3fffd) 489 || (ucs >= 0x30000 && ucs <= 0x3fffd)
472 ); 490 );
491# endif
492#endif
473} 493}