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