1
2
3
4
5
6
7
8
9#include "libbb.h"
10#include "unicode.h"
11
12const char* FAST_FUNC printable_string(uni_stat_t *stats, const char *str)
13{
14 char *dst;
15 const char *s;
16
17 s = str;
18 while (1) {
19 unsigned char c = *s;
20 if (c == '\0') {
21
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
36#if ENABLE_UNICODE_SUPPORT
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
56 return auto_string(dst);
57}
58