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 a constant... */
13 #ifndef unicode_status
14 uint8_t unicode_status;
17 size_t FAST_FUNC bb_mbstrlen(const char *string)
19 size_t width = mbstowcs(NULL, string, INT_MAX);
20 if (width == (size_t)-1L)
21 return strlen(string);
25 #if ENABLE_LOCALE_SUPPORT
27 /* Unicode support using libc */
29 void FAST_FUNC init_unicode(void)
31 /* In unicode, this is a one character string */
32 static const char unicode_0x394[] = { 0xce, 0x94, 0 };
34 if (unicode_status != UNICODE_UNKNOWN)
37 unicode_status = bb_mbstrlen(unicode_0x394) == 1 ? UNICODE_ON : UNICODE_OFF;
42 /* Crude "locale support" which knows only C and Unicode locales */
44 # if ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
45 void FAST_FUNC init_unicode(void)
49 if (unicode_status != UNICODE_UNKNOWN)
52 unicode_status = UNICODE_OFF;
53 lang = getenv("LANG");
54 if (!lang || !(strstr(lang, ".utf") || strstr(lang, ".UTF")))
56 unicode_status = UNICODE_ON;
60 static size_t wcrtomb_internal(char *s, wchar_t wc)
70 /* RFC 3629 says that Unicode ends at 10FFFF,
71 * but we cover entire 32 bits */
73 /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
74 /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
75 /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
76 /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
77 /* 80-7FF -> 110yyyxx 10xxxxxx */
79 /* How many bytes do we need? */
81 /* (0x80000000+ would result in n = 7, limiting n to 6) */
82 while (v >= 0x800 && n < 6) {
86 /* Fill bytes n-1..1 */
89 s[i] = (wc & 0x3f) | 0x80;
93 s[0] = wc | (uint8_t)(0x3f00 >> n);
97 size_t FAST_FUNC wcrtomb(char *s, wchar_t wc, mbstate_t *ps UNUSED_PARAM)
99 if (unicode_status != UNICODE_ON) {
104 return wcrtomb_internal(s, wc);
107 size_t FAST_FUNC wcstombs(char *dest, const wchar_t *src, size_t n)
111 if (unicode_status != UNICODE_ON) {
122 while (n >= MB_CUR_MAX) {
124 size_t len = wcrtomb_internal(dest, wc);
132 char tbuf[MB_CUR_MAX];
134 size_t len = wcrtomb_internal(tbuf, wc);
138 memcpy(dest, tbuf, len);
147 size_t FAST_FUNC mbstowcs(wchar_t *dest, const char *src, size_t n)
151 if (unicode_status != UNICODE_ON) {
153 unsigned char c = *src++;
166 unsigned c = (unsigned char) *src++;
177 /* 80-7FF -> 110yyyxx 10xxxxxx */
178 /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
179 /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
180 /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
181 /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
186 } while ((c & 0x80) && bytes < 6);
189 c = (uint8_t)(c) >> bytes;
192 unsigned ch = (unsigned char) *src++;
193 if ((ch & 0xc0) != 0x80) {
196 c = (c << 6) + (ch & 0x3f);
200 /* Need to check that c isn't produced by overlong encoding */
201 /* Example: 11000000 10000000 converts to NUL */
202 /* 11110000 10000000 10000100 10000000 converts to 0x100 */
203 /* correct encoding: 11000100 10000000 */
204 if (c <= 0x7f) { /* crude check */
206 //or maybe: c = 0xfffd; /* replacement character */
217 int FAST_FUNC iswspace(wint_t wc)
219 return (unsigned)wc <= 0x7f && isspace(wc);
222 int FAST_FUNC iswalnum(wint_t wc)
224 return (unsigned)wc <= 0x7f && isalnum(wc);
227 int FAST_FUNC iswpunct(wint_t wc)
229 return (unsigned)wc <= 0x7f && ispunct(wc);