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