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