2010-01-31 09:45:38 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Unicode support routines.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Denys Vlasenko
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2010-01-31 09:45:38 +05:30
|
|
|
*/
|
|
|
|
#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 18:36:56 +05:30
|
|
|
#if ENABLE_UNICODE_SUPPORT
|
2010-01-31 09:45:38 +05:30
|
|
|
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 21:29:56 +05:30
|
|
|
return auto_string(dst);
|
2010-01-31 09:45:38 +05:30
|
|
|
}
|