1 /* vi: set sw=4 ts=4: */
3 * Unicode support routines.
5 * Copyright (C) 2009 Denys Vlasenko
7 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
12 /* If it's not #defined as a constant in unicode.h... */
13 #ifndef unicode_status
14 uint8_t unicode_status;
17 /* This file is compiled only if FEATURE_ASSUME_UNICODE is on.
18 * We check other options and decide whether to use libc support
19 * via locale, or use our own logic:
22 #if ENABLE_LOCALE_SUPPORT
24 /* Unicode support using libc locale support. */
26 void FAST_FUNC init_unicode(void)
28 /* In unicode, this is a one character string */
29 static const char unicode_0x394[] = { 0xce, 0x94, 0 };
31 if (unicode_status != UNICODE_UNKNOWN)
34 unicode_status = unicode_strlen(unicode_0x394) == 1 ? UNICODE_ON : UNICODE_OFF;
39 /* Homegrown Unicode support. It knows only C and Unicode locales. */
41 # if ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
42 void FAST_FUNC init_unicode(void)
46 if (unicode_status != UNICODE_UNKNOWN)
49 unicode_status = UNICODE_OFF;
50 lang = getenv("LANG");
51 if (!lang || !(strstr(lang, ".utf") || strstr(lang, ".UTF")))
53 unicode_status = UNICODE_ON;
57 static size_t wcrtomb_internal(char *s, wchar_t wc)
67 /* RFC 3629 says that Unicode ends at 10FFFF,
68 * but we cover entire 32 bits */
70 /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
71 /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
72 /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
73 /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
74 /* 80-7FF -> 110yyyxx 10xxxxxx */
76 /* How many bytes do we need? */
78 /* (0x80000000+ would result in n = 7, limiting n to 6) */
79 while (v >= 0x800 && n < 6) {
83 /* Fill bytes n-1..1 */
86 s[i] = (wc & 0x3f) | 0x80;
90 s[0] = wc | (uint8_t)(0x3f00 >> n);
93 size_t FAST_FUNC wcrtomb(char *s, wchar_t wc, mbstate_t *ps UNUSED_PARAM)
95 if (unicode_status != UNICODE_ON) {
100 return wcrtomb_internal(s, wc);
102 size_t FAST_FUNC wcstombs(char *dest, const wchar_t *src, size_t n)
106 if (unicode_status != UNICODE_ON) {
117 while (n >= MB_CUR_MAX) {
119 size_t len = wcrtomb_internal(dest, wc);
127 char tbuf[MB_CUR_MAX];
129 size_t len = wcrtomb_internal(tbuf, wc);
133 memcpy(dest, tbuf, len);
142 #define ERROR_WCHAR (~(wchar_t)0)
144 static const char *mbstowc_internal(wchar_t *res, const char *src)
147 unsigned c = (unsigned char) *src++;
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 */
163 } while ((c & 0x80) && bytes < 6);
165 /* A bare "continuation" byte. Say, 80 */
169 c = (uint8_t)(c) >> bytes;
172 unsigned ch = (unsigned char) *src;
173 if ((ch & 0xc0) != 0x80) {
174 /* Missing "continuation" byte. Example: e0 80 */
178 c = (c << 6) + (ch & 0x3f);
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 */
195 size_t FAST_FUNC mbstowcs(wchar_t *dest, const char *src, size_t n)
199 if (unicode_status != UNICODE_ON) {
201 unsigned char c = *src++;
214 src = mbstowc_internal(&wc, src);
215 if (wc == ERROR_WCHAR) /* error */
219 if (wc == 0) /* end-of-string */
227 int FAST_FUNC iswspace(wint_t wc)
229 return (unsigned)wc <= 0x7f && isspace(wc);
232 int FAST_FUNC iswalnum(wint_t wc)
234 return (unsigned)wc <= 0x7f && isalnum(wc);
237 int FAST_FUNC iswpunct(wint_t wc)
239 return (unsigned)wc <= 0x7f && ispunct(wc);
242 #include "unicode_wcwidth.c"
244 # if ENABLE_UNICODE_BIDI_SUPPORT
245 int FAST_FUNC unicode_bidi_isrtl(wint_t wc)
248 * http://www.unicode.org/Public/5.2.0/ucd/extracted/DerivedBidiClass.txt
249 * Bidi_Class=Left_To_Right | Bidi_Class=Arabic_Letter
251 static const struct interval rtl_b[] = {
252 # define BIG_(a,b) { a, b },
255 PAIR(0x0590, 0x0590) \
256 PAIR(0x05BE, 0x05BE) \
257 PAIR(0x05C0, 0x05C0) \
258 PAIR(0x05C3, 0x05C3) \
259 PAIR(0x05C6, 0x05C6) \
260 BIG_(0x05C8, 0x05FF) \
261 PAIR(0x0604, 0x0605) \
262 PAIR(0x0608, 0x0608) \
263 PAIR(0x060B, 0x060B) \
264 PAIR(0x060D, 0x060D) \
265 BIG_(0x061B, 0x064A) \
266 PAIR(0x065F, 0x065F) \
267 PAIR(0x066D, 0x066F) \
268 BIG_(0x0671, 0x06D5) \
269 PAIR(0x06E5, 0x06E6) \
270 PAIR(0x06EE, 0x06EF) \
271 BIG_(0x06FA, 0x070E) \
272 PAIR(0x0710, 0x0710) \
273 BIG_(0x0712, 0x072F) \
274 BIG_(0x074B, 0x07A5) \
275 BIG_(0x07B1, 0x07EA) \
276 PAIR(0x07F4, 0x07F5) \
277 BIG_(0x07FA, 0x0815) \
278 PAIR(0x081A, 0x081A) \
279 PAIR(0x0824, 0x0824) \
280 PAIR(0x0828, 0x0828) \
281 BIG_(0x082E, 0x08FF) \
282 PAIR(0x200F, 0x200F) \
283 PAIR(0x202B, 0x202B) \
284 PAIR(0x202E, 0x202E) \
285 BIG_(0xFB1D, 0xFB1D) \
286 BIG_(0xFB1F, 0xFB28) \
287 BIG_(0xFB2A, 0xFD3D) \
288 BIG_(0xFD40, 0xFDCF) \
289 BIG_(0xFDC8, 0xFDCF) \
290 BIG_(0xFDF0, 0xFDFC) \
291 BIG_(0xFDFE, 0xFDFF) \
293 /* Probably not necessary
311 # define PAIR(a,b) (a << 2) | (b-a),
312 static const uint16_t rtl_p[] = { ARRAY };
315 # define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
316 # define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
317 struct CHECK { ARRAY };
322 if (in_interval_table(wc, rtl_b, ARRAY_SIZE(rtl_b) - 1))
324 if (in_uint16_table(wc, rtl_p, ARRAY_SIZE(rtl_p) - 1))
329 # if ENABLE_UNICODE_NEUTRAL_TABLE
330 int FAST_FUNC unicode_bidi_is_neutral_wchar(wint_t wc)
333 * http://www.unicode.org/Public/5.2.0/ucd/extracted/DerivedBidiClass.txt
334 * Bidi_Classes: Paragraph_Separator, Segment_Separator,
335 * White_Space, Other_Neutral, European_Number, European_Separator,
336 * European_Terminator, Arabic_Number, Common_Separator
338 static const struct interval neutral_b[] = {
339 # define BIG_(a,b) { a, b },
342 BIG_(0x0009, 0x000D) \
343 BIG_(0x001C, 0x0040) \
344 BIG_(0x005B, 0x0060) \
345 PAIR(0x007B, 0x007E) \
346 PAIR(0x0085, 0x0085) \
347 BIG_(0x00A0, 0x00A9) \
348 PAIR(0x00AB, 0x00AC) \
349 BIG_(0x00AE, 0x00B4) \
350 PAIR(0x00B6, 0x00B9) \
351 BIG_(0x00BB, 0x00BF) \
352 PAIR(0x00D7, 0x00D7) \
353 PAIR(0x00F7, 0x00F7) \
354 PAIR(0x02B9, 0x02BA) \
355 BIG_(0x02C2, 0x02CF) \
356 BIG_(0x02D2, 0x02DF) \
357 BIG_(0x02E5, 0x02FF) \
358 PAIR(0x0374, 0x0375) \
359 PAIR(0x037E, 0x037E) \
360 PAIR(0x0384, 0x0385) \
361 PAIR(0x0387, 0x0387) \
362 PAIR(0x03F6, 0x03F6) \
363 PAIR(0x058A, 0x058A) \
364 PAIR(0x0600, 0x0603) \
365 PAIR(0x0606, 0x0607) \
366 PAIR(0x0609, 0x060A) \
367 PAIR(0x060C, 0x060C) \
368 PAIR(0x060E, 0x060F) \
369 BIG_(0x0660, 0x066C) \
370 PAIR(0x06DD, 0x06DD) \
371 PAIR(0x06E9, 0x06E9) \
372 BIG_(0x06F0, 0x06F9) \
373 PAIR(0x07F6, 0x07F9) \
374 PAIR(0x09F2, 0x09F3) \
375 PAIR(0x09FB, 0x09FB) \
376 PAIR(0x0AF1, 0x0AF1) \
377 BIG_(0x0BF3, 0x0BFA) \
378 BIG_(0x0C78, 0x0C7E) \
379 PAIR(0x0CF1, 0x0CF2) \
380 PAIR(0x0E3F, 0x0E3F) \
381 PAIR(0x0F3A, 0x0F3D) \
382 BIG_(0x1390, 0x1400) \
383 PAIR(0x1680, 0x1680) \
384 PAIR(0x169B, 0x169C) \
385 PAIR(0x17DB, 0x17DB) \
386 BIG_(0x17F0, 0x17F9) \
387 BIG_(0x1800, 0x180A) \
388 PAIR(0x180E, 0x180E) \
389 PAIR(0x1940, 0x1940) \
390 PAIR(0x1944, 0x1945) \
391 BIG_(0x19DE, 0x19FF) \
392 PAIR(0x1FBD, 0x1FBD) \
393 PAIR(0x1FBF, 0x1FC1) \
394 PAIR(0x1FCD, 0x1FCF) \
395 PAIR(0x1FDD, 0x1FDF) \
396 PAIR(0x1FED, 0x1FEF) \
397 PAIR(0x1FFD, 0x1FFE) \
398 BIG_(0x2000, 0x200A) \
399 BIG_(0x2010, 0x2029) \
400 BIG_(0x202F, 0x205F) \
401 PAIR(0x2070, 0x2070) \
402 BIG_(0x2074, 0x207E) \
403 BIG_(0x2080, 0x208E) \
404 BIG_(0x20A0, 0x20B8) \
405 PAIR(0x2100, 0x2101) \
406 PAIR(0x2103, 0x2106) \
407 PAIR(0x2108, 0x2109) \
408 PAIR(0x2114, 0x2114) \
409 PAIR(0x2116, 0x2118) \
410 BIG_(0x211E, 0x2123) \
411 PAIR(0x2125, 0x2125) \
412 PAIR(0x2127, 0x2127) \
413 PAIR(0x2129, 0x2129) \
414 PAIR(0x212E, 0x212E) \
415 PAIR(0x213A, 0x213B) \
416 BIG_(0x2140, 0x2144) \
417 PAIR(0x214A, 0x214D) \
418 BIG_(0x2150, 0x215F) \
419 PAIR(0x2189, 0x2189) \
420 BIG_(0x2190, 0x2335) \
421 BIG_(0x237B, 0x2394) \
422 BIG_(0x2396, 0x23E8) \
423 BIG_(0x2400, 0x2426) \
424 BIG_(0x2440, 0x244A) \
425 BIG_(0x2460, 0x249B) \
426 BIG_(0x24EA, 0x26AB) \
427 BIG_(0x26AD, 0x26CD) \
428 BIG_(0x26CF, 0x26E1) \
429 PAIR(0x26E3, 0x26E3) \
430 BIG_(0x26E8, 0x26FF) \
431 PAIR(0x2701, 0x2704) \
432 PAIR(0x2706, 0x2709) \
433 BIG_(0x270C, 0x2727) \
434 BIG_(0x2729, 0x274B) \
435 PAIR(0x274D, 0x274D) \
436 PAIR(0x274F, 0x2752) \
437 BIG_(0x2756, 0x275E) \
438 BIG_(0x2761, 0x2794) \
439 BIG_(0x2798, 0x27AF) \
440 BIG_(0x27B1, 0x27BE) \
441 BIG_(0x27C0, 0x27CA) \
442 PAIR(0x27CC, 0x27CC) \
443 BIG_(0x27D0, 0x27FF) \
444 BIG_(0x2900, 0x2B4C) \
445 BIG_(0x2B50, 0x2B59) \
446 BIG_(0x2CE5, 0x2CEA) \
447 BIG_(0x2CF9, 0x2CFF) \
448 BIG_(0x2E00, 0x2E99) \
449 BIG_(0x2E9B, 0x2EF3) \
450 BIG_(0x2F00, 0x2FD5) \
451 BIG_(0x2FF0, 0x2FFB) \
452 BIG_(0x3000, 0x3004) \
453 BIG_(0x3008, 0x3020) \
454 PAIR(0x3030, 0x3030) \
455 PAIR(0x3036, 0x3037) \
456 PAIR(0x303D, 0x303D) \
457 PAIR(0x303E, 0x303F) \
458 PAIR(0x309B, 0x309C) \
459 PAIR(0x30A0, 0x30A0) \
460 PAIR(0x30FB, 0x30FB) \
461 BIG_(0x31C0, 0x31E3) \
462 PAIR(0x321D, 0x321E) \
463 BIG_(0x3250, 0x325F) \
464 PAIR(0x327C, 0x327E) \
465 BIG_(0x32B1, 0x32BF) \
466 PAIR(0x32CC, 0x32CF) \
467 PAIR(0x3377, 0x337A) \
468 PAIR(0x33DE, 0x33DF) \
469 PAIR(0x33FF, 0x33FF) \
470 BIG_(0x4DC0, 0x4DFF) \
471 BIG_(0xA490, 0xA4C6) \
472 BIG_(0xA60D, 0xA60F) \
473 BIG_(0xA673, 0xA673) \
474 BIG_(0xA67E, 0xA67F) \
475 BIG_(0xA700, 0xA721) \
476 BIG_(0xA788, 0xA788) \
477 BIG_(0xA828, 0xA82B) \
478 BIG_(0xA838, 0xA839) \
479 BIG_(0xA874, 0xA877) \
480 BIG_(0xFB29, 0xFB29) \
481 BIG_(0xFD3E, 0xFD3F) \
482 BIG_(0xFDFD, 0xFDFD) \
483 BIG_(0xFE10, 0xFE19) \
484 BIG_(0xFE30, 0xFE52) \
485 BIG_(0xFE54, 0xFE66) \
486 BIG_(0xFE68, 0xFE6B) \
487 BIG_(0xFF01, 0xFF20) \
488 BIG_(0xFF3B, 0xFF40) \
489 BIG_(0xFF5B, 0xFF65) \
490 BIG_(0xFFE0, 0xFFE6) \
491 BIG_(0xFFE8, 0xFFEE) \
517 # define PAIR(a,b) (a << 2) | (b-a),
518 static const uint16_t neutral_p[] = { ARRAY };
521 # define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
522 # define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
523 struct CHECK { ARRAY };
528 if (in_interval_table(wc, neutral_b, ARRAY_SIZE(neutral_b) - 1))
530 if (in_uint16_table(wc, neutral_p, ARRAY_SIZE(neutral_p) - 1))
536 # endif /* UNICODE_BIDI_SUPPORT */
538 #endif /* Homegrown Unicode support */
541 /* The rest is mostly same for libc and for "homegrown" support */
543 size_t FAST_FUNC unicode_strlen(const char *string)
545 size_t width = mbstowcs(NULL, string, INT_MAX);
546 if (width == (size_t)-1L)
547 return strlen(string);
551 static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags)
558 if (unicode_status != UNICODE_ON) {
560 if (flags & UNI_FLAG_PAD) {
561 d = dst = xmalloc(width + 1);
562 while ((int)--width >= 0) {
563 unsigned char c = *src;
567 while ((int)--width >= 0);
570 *d++ = (c >= ' ' && c < 0x7f) ? c : '?';
575 d = dst = xstrndup(src, width);
577 unsigned char c = *d;
578 if (c < ' ' || c >= 0x7f)
584 stats->byte_count = stats->unicode_count = (d - dst);
589 uni_count = uni_width = 0;
595 #if ENABLE_LOCALE_SUPPORT
597 mbstate_t mbst = { 0 };
598 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
599 /* If invalid sequence is seen: -1 is returned,
600 * src points to the invalid sequence, errno = EILSEQ.
601 * Else number of wchars (excluding terminating L'\0')
602 * written to dest is returned.
603 * If len (here: 1) non-L'\0' wchars stored at dest,
604 * src points to the next char to be converted.
605 * If string is completely converted: src = NULL.
607 if (rc == 0) /* end-of-string */
609 if (rc < 0) { /* error */
617 src = mbstowc_internal(&wc, src);
618 /* src is advanced to next mb char
619 * wc == ERROR_WCHAR: invalid sequence is seen
622 if (wc == ERROR_WCHAR) /* error */
624 if (wc == 0) /* end-of-string */
627 if (CONFIG_LAST_SUPPORTED_WCHAR && wc > CONFIG_LAST_SUPPORTED_WCHAR)
630 if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0) /* non-printable wchar */
631 || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
632 || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
635 wc = CONFIG_SUBST_WCHAR;
639 /* Note: if width == 0, we still may add more chars,
640 * they may be zero-width or combining ones */
641 if ((int)width < 0) {
642 /* can't add this wc, string would become longer than width */
649 dst = xrealloc(dst, dst_len + MB_CUR_MAX);
650 #if ENABLE_LOCALE_SUPPORT
652 mbstate_t mbst = { 0 };
653 dst_len += wcrtomb(&dst[dst_len], wc, &mbst);
656 dst_len += wcrtomb_internal(&dst[dst_len], wc);
660 /* Pad to remaining width */
661 if (flags & UNI_FLAG_PAD) {
662 dst = xrealloc(dst, dst_len + width + 1);
665 while ((int)--width >= 0) {
666 dst[dst_len++] = ' ';
671 stats->byte_count = dst_len;
672 stats->unicode_count = uni_count;
673 stats->unicode_width = uni_width;
678 char* FAST_FUNC unicode_conv_to_printable(uni_stat_t *stats, const char *src)
680 return unicode_conv_to_printable2(stats, src, INT_MAX, 0);
682 char* FAST_FUNC unicode_conv_to_printable_maxwidth(uni_stat_t *stats, const char *src, unsigned maxwidth)
684 return unicode_conv_to_printable2(stats, src, maxwidth, 0);
686 char* FAST_FUNC unicode_conv_to_printable_fixedwidth(uni_stat_t *stats, const char *src, unsigned width)
688 return unicode_conv_to_printable2(stats, src, width, UNI_FLAG_PAD);
692 unsigned FAST_FUNC unicode_padding_to_width(unsigned width, const char *src)
694 if (unicode_status != UNICODE_ON) {
695 return width - strnlen(src, width);
702 #if ENABLE_LOCALE_SUPPORT
704 mbstate_t mbst = { 0 };
705 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
706 if (rc <= 0) /* error, or end-of-string */
710 src = mbstowc_internal(&wc, src);
711 if (wc == ERROR_WCHAR || wc == 0) /* error, or end-of-string */
715 if (w < 0) /* non-printable wchar */
718 if ((int)width <= 0) /* string is longer than width */