2010-01-31 04:15:38 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Unicode support routines.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Denys Vlasenko
|
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2010-01-31 04:15:38 +00:00
|
|
|
*/
|
|
|
|
#include "libbb.h"
|
|
|
|
#include "unicode.h"
|
|
|
|
|
|
|
|
const char* FAST_FUNC printable_string(uni_stat_t *stats, const char *str)
|
|
|
|
{
|
|
|
|
char *dst;
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
s = str;
|
|
|
|
while (1) {
|
|
|
|
unsigned char c = *s;
|
|
|
|
if (c == '\0') {
|
|
|
|
/* 99+% of inputs do not need conversion */
|
|
|
|
if (stats) {
|
|
|
|
stats->byte_count = (s - str);
|
|
|
|
stats->unicode_count = (s - str);
|
|
|
|
stats->unicode_width = (s - str);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
if (c < ' ')
|
|
|
|
break;
|
|
|
|
if (c >= 0x7f)
|
|
|
|
break;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
2010-03-26 13:06:56 +00:00
|
|
|
#if ENABLE_UNICODE_SUPPORT
|
2010-01-31 04:15:38 +00:00
|
|
|
dst = unicode_conv_to_printable(stats, str);
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
char *d = dst = xstrdup(str);
|
|
|
|
while (1) {
|
|
|
|
unsigned char c = *d;
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
if (c < ' ' || c >= 0x7f)
|
|
|
|
*d = '?';
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
if (stats) {
|
|
|
|
stats->byte_count = (d - dst);
|
|
|
|
stats->unicode_count = (d - dst);
|
|
|
|
stats->unicode_width = (d - dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2015-10-09 15:59:56 +00:00
|
|
|
return auto_string(dst);
|
2010-01-31 04:15:38 +00:00
|
|
|
}
|