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