lineedit: fixes for CONFIG_UNICODE_USING_LOCALE=y

function                                             old     new   delta
load_string                                           45      91     +46
save_string                                           40      82     +42
reinit_unicode                                        34      61     +27
BB_PUTCHAR                                            97     120     +23
init_unicode                                          17      37     +20
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 5/0 up/down: 158/0)             Total: 158 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2011-03-27 01:18:07 +01:00
parent 19311bfa7b
commit 353680aa46
2 changed files with 66 additions and 48 deletions

View File

@ -204,14 +204,22 @@ static void deinit_S(void)
#if ENABLE_UNICODE_SUPPORT
static size_t load_string(const char *src, int maxsize)
{
if (unicode_status == UNICODE_ON) {
ssize_t len = mbstowcs(command_ps, src, maxsize - 1);
if (len < 0)
len = 0;
command_ps[len] = BB_NUL;
return len;
} else {
unsigned i = 0;
while ((command_ps[i] = src[i]) != 0)
i++;
return i;
}
}
static unsigned save_string(char *dst, unsigned maxsize)
{
if (unicode_status == UNICODE_ON) {
# if !ENABLE_UNICODE_PRESERVE_BROKEN
ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
if (len < 0)
@ -251,19 +259,28 @@ static unsigned save_string(char *dst, unsigned maxsize)
dst[dstpos] = '\0';
return dstpos;
# endif
} else {
unsigned i = 0;
while ((dst[i] = command_ps[i]) != 0)
i++;
return i;
}
}
/* I thought just fputwc(c, stdout) would work. But no... */
static void BB_PUTCHAR(wchar_t c)
{
if (unicode_status == UNICODE_ON) {
char buf[MB_CUR_MAX + 1];
mbstate_t mbst = { 0 };
ssize_t len;
len = wcrtomb(buf, c, &mbst);
ssize_t len = wcrtomb(buf, c, &mbst);
if (len > 0) {
buf[len] = '\0';
fputs(buf, stdout);
}
} else {
/* In this case, c is always one byte */
putchar(c);
}
}
# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
static wchar_t adjust_width_and_validate_wc(unsigned *width_adj, wchar_t wc)

View File

@ -23,12 +23,13 @@ uint8_t unicode_status;
/* Unicode support using libc locale support. */
void FAST_FUNC reinit_unicode(const char *LANG UNUSED_PARAM)
void FAST_FUNC reinit_unicode(const char *LANG)
{
static const char unicode_0x394[] = { 0xce, 0x94, 0 };
size_t width;
//TODO: call setlocale(LC_ALL, LANG) here?
//TODO: avoid repeated calls by caching last string?
setlocale(LC_ALL, (LANG && LANG[0]) ? LANG : "C");
/* In unicode, this is a one character string */
// can use unicode_strlen(string) too, but otherwise unicode_strlen() is unused
@ -39,7 +40,7 @@ void FAST_FUNC reinit_unicode(const char *LANG UNUSED_PARAM)
void FAST_FUNC init_unicode(void)
{
if (unicode_status == UNICODE_UNKNOWN)
reinit_unicode(NULL /*getenv("LANG")*/);
reinit_unicode(getenv("LANG"));
}
#else