blob: a814fd03c08ae185e22411499a9b8b20d5e989fb [file] [log] [blame]
Denys Vlasenkod8528b82010-01-31 05:15:38 +01001/* vi: set sw=4 ts=4: */
2/*
3 * Unicode support routines.
4 *
5 * Copyright (C) 2010 Denys Vlasenko
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denys Vlasenkod8528b82010-01-31 05:15:38 +01008 */
9#include "libbb.h"
10#include "unicode.h"
11
Denys Vlasenko349d72c2018-09-30 16:56:56 +020012const char* FAST_FUNC printable_string2(uni_stat_t *stats, const char *str)
Denys Vlasenkod8528b82010-01-31 05:15:38 +010013{
Denys Vlasenkod8528b82010-01-31 05:15:38 +010014 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 Vlasenko19158a82010-03-26 14:06:56 +010036#if ENABLE_UNICODE_SUPPORT
Denys Vlasenkod8528b82010-01-31 05:15:38 +010037 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 Vlasenkoe52da552015-10-09 17:59:56 +020056 return auto_string(dst);
Denys Vlasenkod8528b82010-01-31 05:15:38 +010057}
Denys Vlasenko349d72c2018-09-30 16:56:56 +020058
59const char* FAST_FUNC printable_string(const char *str)
60{
61 return printable_string2(NULL, str);
62}