Start 1.33.0 development cycle
[oweals/busybox.git] / libbb / printable_string.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Unicode support routines.
4  *
5  * Copyright (C) 2010 Denys Vlasenko
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9 #include "libbb.h"
10 #include "unicode.h"
11
12 const char* FAST_FUNC printable_string2(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                         /* 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
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
59 const char* FAST_FUNC printable_string(const char *str)
60 {
61         return printable_string2(NULL, str);
62 }