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 static const char *mbstowc_internal(wchar_t *res, const char *src)
145 unsigned c = (unsigned char) *src++;
152 /* 80-7FF -> 110yyyxx 10xxxxxx */
153 /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
154 /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
155 /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
156 /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
161 } while ((c & 0x80) && bytes < 6);
164 c = (uint8_t)(c) >> bytes;
167 unsigned ch = (unsigned char) *src++;
168 if ((ch & 0xc0) != 0x80) {
171 c = (c << 6) + (ch & 0x3f);
175 /* Need to check that c isn't produced by overlong encoding */
176 /* Example: 11000000 10000000 converts to NUL */
177 /* 11110000 10000000 10000100 10000000 converts to 0x100 */
178 /* correct encoding: 11000100 10000000 */
179 if (c <= 0x7f) { /* crude check */
181 //or maybe 0xfffd; /* replacement character */
187 size_t FAST_FUNC mbstowcs(wchar_t *dest, const char *src, size_t n)
191 if (unicode_status != UNICODE_ON) {
193 unsigned char c = *src++;
206 const char *rc = mbstowc_internal(dest ? dest : &wc, src);
207 if (rc == NULL) /* error */
217 #include "unicode_wcwidth.c"
219 int FAST_FUNC iswspace(wint_t wc)
221 return (unsigned)wc <= 0x7f && isspace(wc);
224 int FAST_FUNC iswalnum(wint_t wc)
226 return (unsigned)wc <= 0x7f && isalnum(wc);
229 int FAST_FUNC iswpunct(wint_t wc)
231 return (unsigned)wc <= 0x7f && ispunct(wc);
234 #endif /* Homegrown Unicode support */
237 /* The rest is mostly same for libc and for "homegrown" support */
239 size_t FAST_FUNC unicode_strlen(const char *string)
241 size_t width = mbstowcs(NULL, string, INT_MAX);
242 if (width == (size_t)-1L)
243 return strlen(string);
247 char* FAST_FUNC unicode_cut_nchars(unsigned width, const char *src)
252 if (unicode_status != UNICODE_ON)
253 return xasprintf("%-*.*s", width, width, src);
261 dst = xrealloc(dst, dst_len + 2 * MB_CUR_MAX);
262 #if ENABLE_LOCALE_SUPPORT
264 mbstate_t mbst = { 0 };
265 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
266 if (rc <= 0) /* error, or end-of-string */
270 src = mbstowc_internal(&wc, src);
271 if (!src || wc == 0) /* error, or end-of-string */
275 if (w < 0) /* non-printable wchar */
278 if ((int)width < 0) { /* string is longer than width */
281 dst[dst_len++] = ' ';
286 #if ENABLE_LOCALE_SUPPORT
288 mbstate_t mbst = { 0 };
289 dst_len += wcrtomb(&dst[dst_len], wc, &mbst);
292 dst_len += wcrtomb_internal(&dst[dst_len], wc);
299 unsigned FAST_FUNC unicode_padding_to_width(unsigned width, const char *src)
301 if (unicode_status != UNICODE_ON) {
302 return width - strnlen(src, width);
309 #if ENABLE_LOCALE_SUPPORT
311 mbstate_t mbst = { 0 };
312 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
313 if (rc <= 0) /* error, or end-of-string */
317 src = mbstowc_internal(&wc, src);
318 if (!src || wc == 0) /* error, or end-of-string */
322 if (w < 0) /* non-printable wchar */
325 if ((int)width <= 0) /* string is longer than width */