diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-12-26 14:56:03 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-12-26 14:56:03 +0000 |
commit | e40e76f3cd3702b1891231f585e5f38867a52b29 (patch) | |
tree | a18368c79c1a40ad84db25aaaaf4b759aaa657a5 | |
parent | 73ac056f5042b34e4009cdf8d6d08112683825f4 (diff) | |
download | busybox-w32-e40e76f3cd3702b1891231f585e5f38867a52b29.tar.gz busybox-w32-e40e76f3cd3702b1891231f585e5f38867a52b29.tar.bz2 busybox-w32-e40e76f3cd3702b1891231f585e5f38867a52b29.zip |
unexpand: fix incorrect expansion, add test for it
function old new delta
expand_main 676 656 -20
-rw-r--r-- | coreutils/expand.c | 66 | ||||
-rwxr-xr-x | testsuite/unexpand.tests | 30 |
2 files changed, 57 insertions, 39 deletions
diff --git a/coreutils/expand.c b/coreutils/expand.c index 3ca7e5c29..0967e2534 100644 --- a/coreutils/expand.c +++ b/coreutils/expand.c | |||
@@ -68,51 +68,39 @@ static void expand(FILE *file, int tab_size, unsigned opt) | |||
68 | static void unexpand(FILE *file, unsigned tab_size, unsigned opt) | 68 | static void unexpand(FILE *file, unsigned tab_size, unsigned opt) |
69 | { | 69 | { |
70 | char *line; | 70 | char *line; |
71 | char *ptr; | ||
72 | int convert; | ||
73 | int pos; | ||
74 | int i = 0; | ||
75 | unsigned column = 0; | ||
76 | 71 | ||
77 | while ((line = xmalloc_fgets(file)) != NULL) { | 72 | while ((line = xmalloc_fgets(file)) != NULL) { |
78 | convert = 1; | 73 | char *ptr = line; |
79 | pos = 0; | 74 | unsigned column = 0; |
80 | ptr = line; | 75 | |
81 | while (*line) { | 76 | while (*ptr) { |
82 | while ((*line == ' ' || *line == '\t') && convert) { | 77 | unsigned n; |
83 | pos += (*line == ' ') ? 1 : tab_size; | 78 | |
84 | line++; | 79 | while (*ptr == ' ') { |
85 | column++; | 80 | column++; |
86 | if ((opt & OPT_ALL) && column == tab_size) { | 81 | ptr++; |
87 | column = 0; | 82 | } |
88 | goto put_tab; | 83 | if (*ptr == '\t') { |
89 | } | 84 | column += tab_size - (column % tab_size); |
85 | ptr++; | ||
86 | continue; | ||
90 | } | 87 | } |
91 | if (pos) { | 88 | |
92 | i = pos / tab_size; | 89 | n = column / tab_size; |
93 | if (i) { | 90 | column = column % tab_size; |
94 | for (; i > 0; i--) { | 91 | while (n--) |
95 | put_tab: | 92 | putchar('\t'); |
96 | bb_putchar('\t'); | 93 | |
97 | } | 94 | if ((opt & OPT_INITIAL) && ptr != line) { |
98 | } else { | 95 | printf("%*s%s", column, "", ptr); |
99 | for (i = pos % tab_size; i > 0; i--) { | 96 | break; |
100 | bb_putchar(' '); | ||
101 | } | ||
102 | } | ||
103 | pos = 0; | ||
104 | } else { | ||
105 | if (opt & OPT_INITIAL) { | ||
106 | convert = 0; | ||
107 | } | ||
108 | if (opt & OPT_ALL) { | ||
109 | column++; | ||
110 | } | ||
111 | bb_putchar(*line); | ||
112 | line++; | ||
113 | } | 97 | } |
98 | n = strcspn(ptr, "\t "); | ||
99 | printf("%*s%.*s", column, "", n, ptr); | ||
100 | ptr += n; | ||
101 | column = (column + n) % tab_size; | ||
114 | } | 102 | } |
115 | free(ptr); | 103 | free(line); |
116 | } | 104 | } |
117 | } | 105 | } |
118 | #endif | 106 | #endif |
diff --git a/testsuite/unexpand.tests b/testsuite/unexpand.tests new file mode 100755 index 000000000..e7f42c5fd --- /dev/null +++ b/testsuite/unexpand.tests | |||
@@ -0,0 +1,30 @@ | |||
1 | #!/bin/sh | ||
2 | # Copyright 2008 by Denys Vlasenko | ||
3 | # Licensed under GPL v2, see file LICENSE for details. | ||
4 | |||
5 | . testing.sh | ||
6 | |||
7 | # testing "test name" "options" "expected result" "file input" "stdin" | ||
8 | |||
9 | testing "unexpand case 1" "unexpand" \ | ||
10 | "\t12345678\n" "" " 12345678\n" \ | ||
11 | |||
12 | testing "unexpand case 2" "unexpand" \ | ||
13 | "\t 12345678\n" "" " 12345678\n" \ | ||
14 | |||
15 | testing "unexpand case 3" "unexpand" \ | ||
16 | "\t 12345678\n" "" " 12345678\n" \ | ||
17 | |||
18 | testing "unexpand case 4" "unexpand" \ | ||
19 | "\t12345678\n" "" " \t12345678\n" \ | ||
20 | |||
21 | testing "unexpand case 5" "unexpand" \ | ||
22 | "\t12345678\n" "" " \t12345678\n" \ | ||
23 | |||
24 | testing "unexpand case 6" "unexpand" \ | ||
25 | "\t12345678\n" "" " \t12345678\n" \ | ||
26 | |||
27 | testing "unexpand case 7" "unexpand" \ | ||
28 | "123\t 45678\n" "" "123 \t 45678\n" \ | ||
29 | |||
30 | exit $FAILCOUNT | ||