Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Unicode support routines. |
| 4 | * |
Denys Vlasenko | f6106e6 | 2009-07-16 02:27:04 +0200 | [diff] [blame] | 5 | * Copyright (C) 2009 Denys Vlasenko |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 6 | * |
| 7 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
| 8 | */ |
| 9 | #include "libbb.h" |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 10 | #include "unicode.h" |
| 11 | |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 12 | /* If it's not #defined as a constant in unicode.h... */ |
Denys Vlasenko | 94ca694 | 2010-01-20 02:51:09 +0100 | [diff] [blame] | 13 | #ifndef unicode_status |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 14 | uint8_t unicode_status; |
Denys Vlasenko | 94ca694 | 2010-01-20 02:51:09 +0100 | [diff] [blame] | 15 | #endif |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 16 | |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 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: |
| 20 | */ |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 21 | |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 22 | #if ENABLE_LOCALE_SUPPORT |
| 23 | |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 24 | /* Unicode support using libc locale support. */ |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 25 | |
| 26 | void 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 Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 34 | unicode_status = unicode_strlen(unicode_0x394) == 1 ? UNICODE_ON : UNICODE_OFF; |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | #else |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 38 | |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 39 | /* Homegrown Unicode support. It knows only C and Unicode locales. */ |
Denys Vlasenko | fda8f57 | 2009-07-11 22:26:48 +0200 | [diff] [blame] | 40 | |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 41 | # if ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 42 | void FAST_FUNC init_unicode(void) |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 43 | { |
| 44 | char *lang; |
| 45 | |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 46 | if (unicode_status != UNICODE_UNKNOWN) |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 47 | return; |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 48 | |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 49 | unicode_status = UNICODE_OFF; |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 50 | lang = getenv("LANG"); |
Denys Vlasenko | fff7364 | 2009-07-16 16:09:25 +0200 | [diff] [blame] | 51 | if (!lang || !(strstr(lang, ".utf") || strstr(lang, ".UTF"))) |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 52 | return; |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 53 | unicode_status = UNICODE_ON; |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 54 | } |
| 55 | # endif |
| 56 | |
| 57 | static size_t wcrtomb_internal(char *s, wchar_t wc) |
| 58 | { |
Denys Vlasenko | 01ba167 | 2009-07-16 03:06:22 +0200 | [diff] [blame] | 59 | int n, i; |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 60 | uint32_t v = wc; |
| 61 | |
| 62 | if (v <= 0x7f) { |
| 63 | *s = v; |
| 64 | return 1; |
| 65 | } |
| 66 | |
Denys Vlasenko | fda8f57 | 2009-07-11 22:26:48 +0200 | [diff] [blame] | 67 | /* RFC 3629 says that Unicode ends at 10FFFF, |
| 68 | * but we cover entire 32 bits */ |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 69 | |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 70 | /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */ |
Denys Vlasenko | fda8f57 | 2009-07-11 22:26:48 +0200 | [diff] [blame] | 71 | /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */ |
Denys Vlasenko | fda8f57 | 2009-07-11 22:26:48 +0200 | [diff] [blame] | 72 | /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */ |
Denys Vlasenko | fda8f57 | 2009-07-11 22:26:48 +0200 | [diff] [blame] | 73 | /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */ |
Denys Vlasenko | 01ba167 | 2009-07-16 03:06:22 +0200 | [diff] [blame] | 74 | /* 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 Vlasenko | fda8f57 | 2009-07-11 22:26:48 +0200 | [diff] [blame] | 81 | n++; |
| 82 | } |
Denys Vlasenko | 01ba167 | 2009-07-16 03:06:22 +0200 | [diff] [blame] | 83 | /* 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 Vlasenko | fda8f57 | 2009-07-11 22:26:48 +0200 | [diff] [blame] | 90 | s[0] = wc | (uint8_t)(0x3f00 >> n); |
| 91 | return n; |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 92 | } |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 93 | size_t FAST_FUNC wcrtomb(char *s, wchar_t wc, mbstate_t *ps UNUSED_PARAM) |
| 94 | { |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 95 | if (unicode_status != UNICODE_ON) { |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 96 | *s = wc; |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | return wcrtomb_internal(s, wc); |
| 101 | } |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 102 | size_t FAST_FUNC wcstombs(char *dest, const wchar_t *src, size_t n) |
| 103 | { |
| 104 | size_t org_n = n; |
| 105 | |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 106 | if (unicode_status != UNICODE_ON) { |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 107 | 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 Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 142 | static const char *mbstowc_internal(wchar_t *res, const char *src) |
| 143 | { |
| 144 | int bytes; |
| 145 | unsigned c = (unsigned char) *src++; |
| 146 | |
| 147 | if (c <= 0x7f) { |
| 148 | *res = c; |
| 149 | return src; |
| 150 | } |
| 151 | |
| 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 */ |
| 157 | bytes = 0; |
| 158 | do { |
| 159 | c <<= 1; |
| 160 | bytes++; |
| 161 | } while ((c & 0x80) && bytes < 6); |
| 162 | if (bytes == 1) |
| 163 | return NULL; |
| 164 | c = (uint8_t)(c) >> bytes; |
| 165 | |
| 166 | while (--bytes) { |
| 167 | unsigned ch = (unsigned char) *src++; |
| 168 | if ((ch & 0xc0) != 0x80) { |
| 169 | return NULL; |
| 170 | } |
| 171 | c = (c << 6) + (ch & 0x3f); |
| 172 | } |
| 173 | |
| 174 | /* TODO */ |
| 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 */ |
| 180 | return NULL; |
| 181 | //or maybe 0xfffd; /* replacement character */ |
| 182 | } |
| 183 | |
| 184 | *res = c; |
| 185 | return src; |
| 186 | } |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 187 | size_t FAST_FUNC mbstowcs(wchar_t *dest, const char *src, size_t n) |
| 188 | { |
| 189 | size_t org_n = n; |
| 190 | |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 191 | if (unicode_status != UNICODE_ON) { |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 192 | while (n) { |
| 193 | unsigned char c = *src++; |
Denys Vlasenko | fda8f57 | 2009-07-11 22:26:48 +0200 | [diff] [blame] | 194 | |
| 195 | if (dest) |
| 196 | *dest++ = c; |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 197 | if (c == 0) |
| 198 | break; |
| 199 | n--; |
| 200 | } |
| 201 | return org_n - n; |
| 202 | } |
| 203 | |
| 204 | while (n) { |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 205 | wchar_t wc; |
Denys Vlasenko | 307b24c | 2010-01-25 02:00:16 +0100 | [diff] [blame] | 206 | src = mbstowc_internal(&wc, src); |
| 207 | if (src == NULL) /* error */ |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 208 | return (size_t) -1L; |
Denys Vlasenko | fda8f57 | 2009-07-11 22:26:48 +0200 | [diff] [blame] | 209 | if (dest) |
Denys Vlasenko | 307b24c | 2010-01-25 02:00:16 +0100 | [diff] [blame] | 210 | *dest++ = wc; |
| 211 | if (wc == 0) /* end-of-string */ |
| 212 | break; |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 213 | n--; |
| 214 | } |
| 215 | |
| 216 | return org_n - n; |
| 217 | } |
| 218 | |
| 219 | int FAST_FUNC iswspace(wint_t wc) |
| 220 | { |
| 221 | return (unsigned)wc <= 0x7f && isspace(wc); |
| 222 | } |
| 223 | |
| 224 | int FAST_FUNC iswalnum(wint_t wc) |
| 225 | { |
| 226 | return (unsigned)wc <= 0x7f && isalnum(wc); |
| 227 | } |
| 228 | |
| 229 | int FAST_FUNC iswpunct(wint_t wc) |
| 230 | { |
| 231 | return (unsigned)wc <= 0x7f && ispunct(wc); |
| 232 | } |
| 233 | |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 234 | #include "unicode_wcwidth.c" |
| 235 | |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 236 | #endif /* Homegrown Unicode support */ |
| 237 | |
| 238 | |
| 239 | /* The rest is mostly same for libc and for "homegrown" support */ |
| 240 | |
| 241 | size_t FAST_FUNC unicode_strlen(const char *string) |
| 242 | { |
| 243 | size_t width = mbstowcs(NULL, string, INT_MAX); |
| 244 | if (width == (size_t)-1L) |
| 245 | return strlen(string); |
| 246 | return width; |
| 247 | } |
| 248 | |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 249 | static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags) |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 250 | { |
| 251 | char *dst; |
| 252 | unsigned dst_len; |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 253 | unsigned uni_count; |
| 254 | unsigned uni_width; |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 255 | |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 256 | if (unicode_status != UNICODE_ON) { |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 257 | char *d; |
| 258 | if (flags & UNI_FLAG_PAD) { |
| 259 | d = dst = xmalloc(width + 1); |
| 260 | while ((int)--width >= 0) { |
| 261 | unsigned char c = *src; |
| 262 | if (c == '\0') { |
| 263 | do |
| 264 | *d++ = ' '; |
| 265 | while ((int)--width >= 0); |
| 266 | break; |
| 267 | } |
| 268 | *d++ = (c >= ' ' && c < 0x7f) ? c : '?'; |
| 269 | src++; |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 270 | } |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 271 | *d = '\0'; |
| 272 | } else { |
| 273 | d = dst = xstrndup(src, width); |
| 274 | while (*d) { |
| 275 | unsigned char c = *d; |
| 276 | if (c < ' ' || c >= 0x7f) |
| 277 | *d = '?'; |
| 278 | d++; |
| 279 | } |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 280 | } |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 281 | if (stats) |
| 282 | stats->byte_count = stats->unicode_count = (d - dst); |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 283 | return dst; |
| 284 | } |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 285 | |
| 286 | dst = NULL; |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 287 | uni_count = uni_width = 0; |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 288 | dst_len = 0; |
| 289 | while (1) { |
| 290 | int w; |
| 291 | wchar_t wc; |
| 292 | |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 293 | #if ENABLE_LOCALE_SUPPORT |
| 294 | { |
| 295 | mbstate_t mbst = { 0 }; |
| 296 | ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst); |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 297 | /* If invalid sequence is seen: -1 is returned, |
| 298 | * src points to the invalid sequence, errno = EILSEQ. |
| 299 | * Else number of wchars (excluding terminating L'\0') |
| 300 | * written to dest is returned. |
| 301 | * If len (here: 1) non-L'\0' wchars stored at dest, |
| 302 | * src points to the next char to be converted. |
| 303 | * If string is completely converted: src = NULL. |
| 304 | */ |
| 305 | if (rc == 0) /* end-of-string */ |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 306 | break; |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 307 | if (rc < 0) { /* error */ |
| 308 | src++; |
| 309 | goto subst; |
| 310 | } |
| 311 | if (!iswprint(wc)) |
| 312 | goto subst; |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 313 | } |
| 314 | #else |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 315 | { |
| 316 | const char *src1 = mbstowc_internal(&wc, src); |
| 317 | /* src = NULL: invalid sequence is seen, |
| 318 | * else: wc is set, src is advanced to next mb char |
| 319 | */ |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 320 | if (src1) { /* no error */ |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 321 | if (wc == 0) /* end-of-string */ |
| 322 | break; |
| 323 | src = src1; |
| 324 | } else { /* error */ |
| 325 | src++; |
| 326 | goto subst; |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 327 | } |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 328 | } |
| 329 | #endif |
| 330 | if (CONFIG_LAST_SUPPORTED_WCHAR && wc > CONFIG_LAST_SUPPORTED_WCHAR) |
| 331 | goto subst; |
| 332 | w = wcwidth(wc); |
| 333 | if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0) /* non-printable wchar */ |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 334 | || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0) |
| 335 | || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1) |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 336 | ) { |
| 337 | subst: |
| 338 | wc = CONFIG_SUBST_WCHAR; |
| 339 | w = 1; |
| 340 | } |
| 341 | width -= w; |
| 342 | /* Note: if width == 0, we still may add more chars, |
| 343 | * they may be zero-width or combining ones */ |
| 344 | if ((int)width < 0) { |
| 345 | /* can't add this wc, string would become longer than width */ |
| 346 | width += w; |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 347 | break; |
| 348 | } |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 349 | |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 350 | uni_count++; |
| 351 | uni_width += w; |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 352 | dst = xrealloc(dst, dst_len + MB_CUR_MAX); |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 353 | #if ENABLE_LOCALE_SUPPORT |
| 354 | { |
| 355 | mbstate_t mbst = { 0 }; |
| 356 | dst_len += wcrtomb(&dst[dst_len], wc, &mbst); |
| 357 | } |
| 358 | #else |
| 359 | dst_len += wcrtomb_internal(&dst[dst_len], wc); |
| 360 | #endif |
| 361 | } |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 362 | |
| 363 | /* Pad to remaining width */ |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 364 | if (flags & UNI_FLAG_PAD) { |
| 365 | dst = xrealloc(dst, dst_len + width + 1); |
| 366 | uni_count += width; |
| 367 | uni_width += width; |
| 368 | while ((int)--width >= 0) { |
| 369 | dst[dst_len++] = ' '; |
| 370 | } |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 371 | } |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 372 | dst[dst_len] = '\0'; |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 373 | if (stats) { |
| 374 | stats->byte_count = dst_len; |
| 375 | stats->unicode_count = uni_count; |
| 376 | stats->unicode_width = uni_width; |
| 377 | } |
Denys Vlasenko | 2edba21 | 2010-01-29 09:11:47 +0100 | [diff] [blame] | 378 | |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 379 | return dst; |
| 380 | } |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 381 | char* FAST_FUNC unicode_conv_to_printable(uni_stat_t *stats, const char *src) |
| 382 | { |
| 383 | return unicode_conv_to_printable2(stats, src, INT_MAX, 0); |
| 384 | } |
| 385 | char* FAST_FUNC unicode_conv_to_printable_maxwidth(uni_stat_t *stats, const char *src, unsigned maxwidth) |
| 386 | { |
| 387 | return unicode_conv_to_printable2(stats, src, maxwidth, 0); |
| 388 | } |
| 389 | char* FAST_FUNC unicode_conv_to_printable_fixedwidth(uni_stat_t *stats, const char *src, unsigned width) |
| 390 | { |
| 391 | return unicode_conv_to_printable2(stats, src, width, UNI_FLAG_PAD); |
| 392 | } |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 393 | |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 394 | #ifdef UNUSED |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 395 | unsigned FAST_FUNC unicode_padding_to_width(unsigned width, const char *src) |
| 396 | { |
| 397 | if (unicode_status != UNICODE_ON) { |
| 398 | return width - strnlen(src, width); |
| 399 | } |
| 400 | |
| 401 | while (1) { |
| 402 | int w; |
| 403 | wchar_t wc; |
| 404 | |
| 405 | #if ENABLE_LOCALE_SUPPORT |
| 406 | { |
| 407 | mbstate_t mbst = { 0 }; |
| 408 | ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst); |
| 409 | if (rc <= 0) /* error, or end-of-string */ |
| 410 | return width; |
| 411 | } |
| 412 | #else |
| 413 | src = mbstowc_internal(&wc, src); |
| 414 | if (!src || wc == 0) /* error, or end-of-string */ |
| 415 | return width; |
| 416 | #endif |
| 417 | w = wcwidth(wc); |
| 418 | if (w < 0) /* non-printable wchar */ |
| 419 | return width; |
| 420 | width -= w; |
| 421 | if ((int)width <= 0) /* string is longer than width */ |
| 422 | return 0; |
| 423 | } |
| 424 | } |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame^] | 425 | #endif |