aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomas Heinrich <heinrich.tomas@gmail.com>2010-01-04 16:21:31 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-01-04 16:21:31 +0100
commitd2b1ba6fdee59645763e915ade3ec55e67d5839a (patch)
tree9b1a97996255647452851908dc76e4e91a57420b
parent4928f3b90b3925e6f3cc234df48e46f88fc5689b (diff)
downloadbusybox-w32-d2b1ba6fdee59645763e915ade3ec55e67d5839a.tar.gz
busybox-w32-d2b1ba6fdee59645763e915ade3ec55e67d5839a.tar.bz2
busybox-w32-d2b1ba6fdee59645763e915ade3ec55e67d5839a.zip
[un]expand: unicode support
function old new delta expand_main 633 663 +30 Signed-off-by: Tomas Heinrich <heinrich.tomas@gmail.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/expand.c51
-rwxr-xr-xtestsuite/expand.tests6
-rwxr-xr-xtestsuite/unexpand.tests3
3 files changed, 42 insertions, 18 deletions
diff --git a/coreutils/expand.c b/coreutils/expand.c
index 7137c3b89..e08eb1d0a 100644
--- a/coreutils/expand.c
+++ b/coreutils/expand.c
@@ -20,8 +20,8 @@
20 * 20 *
21 * Caveat: this versions of expand and unexpand don't accept tab lists. 21 * Caveat: this versions of expand and unexpand don't accept tab lists.
22 */ 22 */
23
24#include "libbb.h" 23#include "libbb.h"
24#include "unicode.h"
25 25
26enum { 26enum {
27 OPT_INITIAL = 1 << 0, 27 OPT_INITIAL = 1 << 0,
@@ -30,35 +30,37 @@ enum {
30}; 30};
31 31
32#if ENABLE_EXPAND 32#if ENABLE_EXPAND
33static void expand(FILE *file, int tab_size, unsigned opt) 33static void expand(FILE *file, unsigned tab_size, unsigned opt)
34{ 34{
35 char *line; 35 char *line;
36 36
37 tab_size = -tab_size;
38
39 while ((line = xmalloc_fgets(file)) != NULL) { 37 while ((line = xmalloc_fgets(file)) != NULL) {
40 int pos;
41 unsigned char c; 38 unsigned char c;
42 char *ptr = line; 39 char *ptr;
40 char *ptr_strbeg;
43 41
44 goto start; 42 ptr = ptr_strbeg = line;
45 while ((c = *ptr) != '\0') { 43 while ((c = *ptr) != '\0') {
46 if ((opt & OPT_INITIAL) && !isblank(c)) { 44 if ((opt & OPT_INITIAL) && !isblank(c)) {
47 fputs(ptr, stdout); 45 /* not space or tab */
48 break; 46 break;
49 } 47 }
50 ptr++;
51 if (c == '\t') { 48 if (c == '\t') {
52 c = ' '; 49 unsigned len;
53 while (++pos < 0) 50 *ptr = '\0';
54 bb_putchar(c); 51# if ENABLE_FEATURE_ASSUME_UNICODE
55 } 52 len = bb_mbstrlen(ptr_strbeg);
56 bb_putchar(c); 53# else
57 if (++pos >= 0) { 54 len = ptr - ptr_strbeg;
58 start: 55# endif
59 pos = tab_size; 56 len = tab_size - (len % tab_size);
57 /*while (ptr[1] == '\t') { ptr++; len += tab_size; } - can handle many tabs at once */
58 printf("%s%*s", ptr_strbeg, len, "");
59 ptr_strbeg = ptr + 1;
60 } 60 }
61 ptr++;
61 } 62 }
63 fputs(ptr_strbeg, stdout);
62 free(line); 64 free(line);
63 } 65 }
64} 66}
@@ -75,6 +77,7 @@ static void unexpand(FILE *file, unsigned tab_size, unsigned opt)
75 77
76 while (*ptr) { 78 while (*ptr) {
77 unsigned n; 79 unsigned n;
80 unsigned len;
78 81
79 while (*ptr == ' ') { 82 while (*ptr == ' ') {
80 column++; 83 column++;
@@ -97,8 +100,19 @@ static void unexpand(FILE *file, unsigned tab_size, unsigned opt)
97 } 100 }
98 n = strcspn(ptr, "\t "); 101 n = strcspn(ptr, "\t ");
99 printf("%*s%.*s", column, "", n, ptr); 102 printf("%*s%.*s", column, "", n, ptr);
103# if ENABLE_FEATURE_ASSUME_UNICODE
104 {
105 char c;
106 c = ptr[n];
107 ptr[n] = '\0';
108 len = bb_mbstrlen(ptr);
109 ptr[n] = c;
110 }
111# else
112 len = n;
113# endif
100 ptr += n; 114 ptr += n;
101 column = (column + n) % tab_size; 115 column = (column + len) % tab_size;
102 } 116 }
103 free(line); 117 free(line);
104 } 118 }
@@ -130,6 +144,7 @@ int expand_main(int argc UNUSED_PARAM, char **argv)
130 "all\0" No_argument "a" 144 "all\0" No_argument "a"
131 ; 145 ;
132#endif 146#endif
147 check_unicode_in_env();
133 148
134 if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) { 149 if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) {
135 IF_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts); 150 IF_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts);
diff --git a/testsuite/expand.tests b/testsuite/expand.tests
index 57498a201..996631450 100755
--- a/testsuite/expand.tests
+++ b/testsuite/expand.tests
@@ -12,4 +12,10 @@ testing "expand" \
12 "" \ 12 "" \
13 "\t12345678\t12345678\n" \ 13 "\t12345678\t12345678\n" \
14 14
15testing "expand with unicode characher 0x394" \
16 "expand" \
17 "Δ 12345ΔΔΔ 12345678\n" \
18 "" \
19 "Δ\t12345ΔΔΔ\t12345678\n" \
20
15exit $FAILCOUNT 21exit $FAILCOUNT
diff --git a/testsuite/unexpand.tests b/testsuite/unexpand.tests
index 5c82a1e50..5c2a29b5f 100755
--- a/testsuite/unexpand.tests
+++ b/testsuite/unexpand.tests
@@ -27,4 +27,7 @@ testing "unexpand case 6" "unexpand" \
27testing "unexpand case 7" "unexpand" \ 27testing "unexpand case 7" "unexpand" \
28 "123\t 45678\n" "" "123 \t 45678\n" \ 28 "123\t 45678\n" "" "123 \t 45678\n" \
29 29
30testing "unexpand with unicode characher 0x394" "unexpand" \
31 "1ΔΔΔ5\t99999\n" "" "1ΔΔΔ5 99999\n" \
32
30exit $FAILCOUNT 33exit $FAILCOUNT