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 #endif /* Homegrown Unicode support */
247 /* The rest is mostly same for libc and for "homegrown" support */
249 size_t FAST_FUNC unicode_strlen(const char *string)
251 size_t width = mbstowcs(NULL, string, INT_MAX);
252 if (width == (size_t)-1L)
253 return strlen(string);
257 static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags)
264 if (unicode_status != UNICODE_ON) {
266 if (flags & UNI_FLAG_PAD) {
267 d = dst = xmalloc(width + 1);
268 while ((int)--width >= 0) {
269 unsigned char c = *src;
273 while ((int)--width >= 0);
276 *d++ = (c >= ' ' && c < 0x7f) ? c : '?';
281 d = dst = xstrndup(src, width);
283 unsigned char c = *d;
284 if (c < ' ' || c >= 0x7f)
290 stats->byte_count = stats->unicode_count = (d - dst);
295 uni_count = uni_width = 0;
301 #if ENABLE_LOCALE_SUPPORT
303 mbstate_t mbst = { 0 };
304 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
305 /* If invalid sequence is seen: -1 is returned,
306 * src points to the invalid sequence, errno = EILSEQ.
307 * Else number of wchars (excluding terminating L'\0')
308 * written to dest is returned.
309 * If len (here: 1) non-L'\0' wchars stored at dest,
310 * src points to the next char to be converted.
311 * If string is completely converted: src = NULL.
313 if (rc == 0) /* end-of-string */
315 if (rc < 0) { /* error */
323 src = mbstowc_internal(&wc, src);
324 /* src is advanced to next mb char
325 * wc == ERROR_WCHAR: invalid sequence is seen
328 if (wc == ERROR_WCHAR) /* error */
330 if (wc == 0) /* end-of-string */
333 if (CONFIG_LAST_SUPPORTED_WCHAR && wc > CONFIG_LAST_SUPPORTED_WCHAR)
336 if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0) /* non-printable wchar */
337 || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
338 || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
341 wc = CONFIG_SUBST_WCHAR;
345 /* Note: if width == 0, we still may add more chars,
346 * they may be zero-width or combining ones */
347 if ((int)width < 0) {
348 /* can't add this wc, string would become longer than width */
355 dst = xrealloc(dst, dst_len + MB_CUR_MAX);
356 #if ENABLE_LOCALE_SUPPORT
358 mbstate_t mbst = { 0 };
359 dst_len += wcrtomb(&dst[dst_len], wc, &mbst);
362 dst_len += wcrtomb_internal(&dst[dst_len], wc);
366 /* Pad to remaining width */
367 if (flags & UNI_FLAG_PAD) {
368 dst = xrealloc(dst, dst_len + width + 1);
371 while ((int)--width >= 0) {
372 dst[dst_len++] = ' ';
377 stats->byte_count = dst_len;
378 stats->unicode_count = uni_count;
379 stats->unicode_width = uni_width;
384 char* FAST_FUNC unicode_conv_to_printable(uni_stat_t *stats, const char *src)
386 return unicode_conv_to_printable2(stats, src, INT_MAX, 0);
388 char* FAST_FUNC unicode_conv_to_printable_maxwidth(uni_stat_t *stats, const char *src, unsigned maxwidth)
390 return unicode_conv_to_printable2(stats, src, maxwidth, 0);
392 char* FAST_FUNC unicode_conv_to_printable_fixedwidth(uni_stat_t *stats, const char *src, unsigned width)
394 return unicode_conv_to_printable2(stats, src, width, UNI_FLAG_PAD);
398 unsigned FAST_FUNC unicode_padding_to_width(unsigned width, const char *src)
400 if (unicode_status != UNICODE_ON) {
401 return width - strnlen(src, width);
408 #if ENABLE_LOCALE_SUPPORT
410 mbstate_t mbst = { 0 };
411 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
412 if (rc <= 0) /* error, or end-of-string */
416 src = mbstowc_internal(&wc, src);
417 if (wc == ERROR_WCHAR || wc == 0) /* error, or end-of-string */
421 if (w < 0) /* non-printable wchar */
424 if ((int)width <= 0) /* string is longer than width */