Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Unicode support routines. |
| 4 | * |
| 5 | * Copyright (C) 2010 Denys Vlasenko |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 8 | */ |
| 9 | #include "libbb.h" |
| 10 | #include "unicode.h" |
| 11 | |
Denys Vlasenko | 349d72c | 2018-09-30 16:56:56 +0200 | [diff] [blame] | 12 | const char* FAST_FUNC printable_string2(uni_stat_t *stats, const char *str) |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 13 | { |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 14 | char *dst; |
| 15 | const char *s; |
| 16 | |
| 17 | s = str; |
| 18 | while (1) { |
| 19 | unsigned char c = *s; |
| 20 | if (c == '\0') { |
| 21 | /* 99+% of inputs do not need conversion */ |
| 22 | if (stats) { |
| 23 | stats->byte_count = (s - str); |
| 24 | stats->unicode_count = (s - str); |
| 25 | stats->unicode_width = (s - str); |
| 26 | } |
| 27 | return str; |
| 28 | } |
| 29 | if (c < ' ') |
| 30 | break; |
| 31 | if (c >= 0x7f) |
| 32 | break; |
| 33 | s++; |
| 34 | } |
| 35 | |
Denys Vlasenko | 19158a8 | 2010-03-26 14:06:56 +0100 | [diff] [blame] | 36 | #if ENABLE_UNICODE_SUPPORT |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 37 | dst = unicode_conv_to_printable(stats, str); |
| 38 | #else |
| 39 | { |
| 40 | char *d = dst = xstrdup(str); |
| 41 | while (1) { |
| 42 | unsigned char c = *d; |
| 43 | if (c == '\0') |
| 44 | break; |
| 45 | if (c < ' ' || c >= 0x7f) |
| 46 | *d = '?'; |
| 47 | d++; |
| 48 | } |
| 49 | if (stats) { |
| 50 | stats->byte_count = (d - dst); |
| 51 | stats->unicode_count = (d - dst); |
| 52 | stats->unicode_width = (d - dst); |
| 53 | } |
| 54 | } |
| 55 | #endif |
Denys Vlasenko | e52da55 | 2015-10-09 17:59:56 +0200 | [diff] [blame] | 56 | return auto_string(dst); |
Denys Vlasenko | d8528b8 | 2010-01-31 05:15:38 +0100 | [diff] [blame] | 57 | } |
Denys Vlasenko | 349d72c | 2018-09-30 16:56:56 +0200 | [diff] [blame] | 58 | |
| 59 | const char* FAST_FUNC printable_string(const char *str) |
| 60 | { |
| 61 | return printable_string2(NULL, str); |
| 62 | } |