diff options
author | Tomas Heinrich <heinrich.tomas@gmail.com> | 2010-06-01 08:33:18 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-06-01 08:33:18 +0200 |
commit | 11bcf4b22449d08b61617183c1951db98457653a (patch) | |
tree | 34d1983e1f1b5eb38d8c764ab9f2b406404b862e | |
parent | 39a04f71ca8ccf81de2cdbd538df519cf34ef2e6 (diff) | |
download | busybox-w32-11bcf4b22449d08b61617183c1951db98457653a.tar.gz busybox-w32-11bcf4b22449d08b61617183c1951db98457653a.tar.bz2 busybox-w32-11bcf4b22449d08b61617183c1951db98457653a.zip |
lineedit: fix column display for wide and combining chars in TAB completion
function old new delta
unicode_strwidth - 20 +20
read_line_input 4945 4953 +8
unicode_strlen 31 - -31
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 1/0 up/down: 28/-31) Total: -3 bytes
Signed-off-by: Tomas Heinrich <heinrich.tomas@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | include/unicode.h | 6 | ||||
-rw-r--r-- | libbb/lineedit.c | 9 | ||||
-rw-r--r-- | libbb/unicode.c | 17 |
3 files changed, 25 insertions, 7 deletions
diff --git a/include/unicode.h b/include/unicode.h index eaf67c833..e9e2bd14a 100644 --- a/include/unicode.h +++ b/include/unicode.h | |||
@@ -23,7 +23,8 @@ enum { | |||
23 | 23 | ||
24 | #if !ENABLE_UNICODE_SUPPORT | 24 | #if !ENABLE_UNICODE_SUPPORT |
25 | 25 | ||
26 | # define unicode_strlen(string) strlen(string) | 26 | # define unicode_strlen(string) strlen(string) |
27 | # define unicode_strwidth(string) strlen(string) | ||
27 | # define unicode_status UNICODE_OFF | 28 | # define unicode_status UNICODE_OFF |
28 | # define init_unicode() ((void)0) | 29 | # define init_unicode() ((void)0) |
29 | 30 | ||
@@ -49,7 +50,10 @@ enum { | |||
49 | # define ENABLE_UNICODE_BIDI_SUPPORT 0 | 50 | # define ENABLE_UNICODE_BIDI_SUPPORT 0 |
50 | # endif | 51 | # endif |
51 | 52 | ||
53 | /* Number of unicode chars. Falls back to strlen() on invalid unicode */ | ||
52 | size_t FAST_FUNC unicode_strlen(const char *string); | 54 | size_t FAST_FUNC unicode_strlen(const char *string); |
55 | /* Width on terminal */ | ||
56 | size_t FAST_FUNC unicode_strwidth(const char *string); | ||
53 | enum { | 57 | enum { |
54 | UNI_FLAG_PAD = (1 << 0), | 58 | UNI_FLAG_PAD = (1 << 0), |
55 | }; | 59 | }; |
diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 18664b8c1..8a2ea7974 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c | |||
@@ -992,7 +992,7 @@ static void showfiles(void) | |||
992 | 992 | ||
993 | /* find the longest file name - use that as the column width */ | 993 | /* find the longest file name - use that as the column width */ |
994 | for (row = 0; row < nrows; row++) { | 994 | for (row = 0; row < nrows; row++) { |
995 | l = unicode_strlen(matches[row]); | 995 | l = unicode_strwidth(matches[row]); |
996 | if (column_width < l) | 996 | if (column_width < l) |
997 | column_width = l; | 997 | column_width = l; |
998 | } | 998 | } |
@@ -1012,10 +1012,13 @@ static void showfiles(void) | |||
1012 | 1012 | ||
1013 | for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) { | 1013 | for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) { |
1014 | printf("%s%-*s", matches[n], | 1014 | printf("%s%-*s", matches[n], |
1015 | (int)(column_width - unicode_strlen(matches[n])), "" | 1015 | (int)(column_width - unicode_strwidth(matches[n])), "" |
1016 | ); | 1016 | ); |
1017 | } | 1017 | } |
1018 | puts(matches[n]); | 1018 | if (ENABLE_UNICODE_SUPPORT) |
1019 | puts(printable_string(NULL, matches[n])); | ||
1020 | else | ||
1021 | puts(matches[n]); | ||
1019 | } | 1022 | } |
1020 | } | 1023 | } |
1021 | 1024 | ||
diff --git a/libbb/unicode.c b/libbb/unicode.c index b2c28239b..d6fcf7a43 100644 --- a/libbb/unicode.c +++ b/libbb/unicode.c | |||
@@ -25,13 +25,15 @@ uint8_t unicode_status; | |||
25 | 25 | ||
26 | void FAST_FUNC init_unicode(void) | 26 | void FAST_FUNC init_unicode(void) |
27 | { | 27 | { |
28 | /* In unicode, this is a one character string */ | ||
29 | static const char unicode_0x394[] = { 0xce, 0x94, 0 }; | 28 | static const char unicode_0x394[] = { 0xce, 0x94, 0 }; |
29 | size_t width; | ||
30 | 30 | ||
31 | if (unicode_status != UNICODE_UNKNOWN) | 31 | if (unicode_status != UNICODE_UNKNOWN) |
32 | return; | 32 | return; |
33 | 33 | /* In unicode, this is a one character string */ | |
34 | unicode_status = unicode_strlen(unicode_0x394) == 1 ? UNICODE_ON : UNICODE_OFF; | 34 | // can use unicode_strlen(string) too, but otherwise unicode_strlen() is unused |
35 | width = mbstowcs(NULL, unicode_0x394, INT_MAX); | ||
36 | unicode_status = (width == 1 ? UNICODE_ON : UNICODE_OFF); | ||
35 | } | 37 | } |
36 | 38 | ||
37 | #else | 39 | #else |
@@ -954,6 +956,7 @@ int FAST_FUNC unicode_bidi_is_neutral_wchar(wint_t wc) | |||
954 | 956 | ||
955 | /* The rest is mostly same for libc and for "homegrown" support */ | 957 | /* The rest is mostly same for libc and for "homegrown" support */ |
956 | 958 | ||
959 | #if 0 // UNUSED | ||
957 | size_t FAST_FUNC unicode_strlen(const char *string) | 960 | size_t FAST_FUNC unicode_strlen(const char *string) |
958 | { | 961 | { |
959 | size_t width = mbstowcs(NULL, string, INT_MAX); | 962 | size_t width = mbstowcs(NULL, string, INT_MAX); |
@@ -961,6 +964,14 @@ size_t FAST_FUNC unicode_strlen(const char *string) | |||
961 | return strlen(string); | 964 | return strlen(string); |
962 | return width; | 965 | return width; |
963 | } | 966 | } |
967 | #endif | ||
968 | |||
969 | size_t FAST_FUNC unicode_strwidth(const char *string) | ||
970 | { | ||
971 | uni_stat_t uni_stat; | ||
972 | printable_string(&uni_stat, string); | ||
973 | return uni_stat.unicode_width; | ||
974 | } | ||
964 | 975 | ||
965 | static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags) | 976 | static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags) |
966 | { | 977 | { |