expand: fix incorrect expansion exactly on tab boundary; shrink the code

function                                             old     new   delta
expand_main                                          698     676     -22
xputchar                                              53       -     -53
This commit is contained in:
Denis Vlasenko 2008-12-14 15:45:25 +00:00
parent 005ff882ba
commit 3139ea7f15
2 changed files with 41 additions and 34 deletions

View File

@ -29,51 +29,43 @@ enum {
OPT_ALL = 1 << 2, OPT_ALL = 1 << 2,
}; };
static void xputchar(char c)
{
if (putchar(c) < 0)
bb_error_msg_and_die(bb_msg_write_error);
}
#if ENABLE_EXPAND #if ENABLE_EXPAND
static void expand(FILE *file, unsigned tab_size, unsigned opt) static void expand(FILE *file, int tab_size, unsigned opt)
{ {
char *line; char *line;
char *ptr;
int convert;
unsigned pos;
/* Increment tab_size by 1 locally.*/ tab_size = -tab_size;
tab_size++;
while ((line = xmalloc_fgets(file)) != NULL) { while ((line = xmalloc_fgets(file)) != NULL) {
convert = 1; int pos;
pos = 0; unsigned char c;
ptr = line; char *ptr = line;
while (*line) {
pos++; goto start;
if (*line == '\t' && convert) { while ((c = *ptr) != '\0') {
for (; pos < tab_size; pos++) { if ((opt & OPT_INITIAL) && !isblank(c)) {
xputchar(' '); fputs(ptr, stdout);
} break;
} else {
if ((opt & OPT_INITIAL) && !isblank(*line)) {
convert = 0;
}
xputchar(*line);
} }
if (pos == tab_size) { ptr++;
pos = 0; if (c == '\t') {
c = ' ';
while (++pos < 0)
bb_putchar(c);
}
bb_putchar(c);
if (++pos >= 0) {
start:
pos = tab_size;
} }
line++;
} }
free(ptr); free(line);
} }
} }
#endif #endif
#if ENABLE_UNEXPAND #if ENABLE_UNEXPAND
static void unexpand(FILE *file, unsigned int tab_size, unsigned opt) static void unexpand(FILE *file, unsigned tab_size, unsigned opt)
{ {
char *line; char *line;
char *ptr; char *ptr;
@ -101,11 +93,11 @@ static void unexpand(FILE *file, unsigned int tab_size, unsigned opt)
if (i) { if (i) {
for (; i > 0; i--) { for (; i > 0; i--) {
put_tab: put_tab:
xputchar('\t'); bb_putchar('\t');
} }
} else { } else {
for (i = pos % tab_size; i > 0; i--) { for (i = pos % tab_size; i > 0; i--) {
xputchar(' '); bb_putchar(' ');
} }
} }
pos = 0; pos = 0;
@ -116,7 +108,7 @@ static void unexpand(FILE *file, unsigned int tab_size, unsigned opt)
if (opt & OPT_ALL) { if (opt & OPT_ALL) {
column++; column++;
} }
xputchar(*line); bb_putchar(*line);
line++; line++;
} }
} }

15
testsuite/expand.tests Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
# Copyright 2008 by Denys Vlasenko
# Licensed under GPL v2, see file LICENSE for details.
. testing.sh
# testing "test name" "options" "expected result" "file input" "stdin"
testing "expand" \
"expand" \
" 12345678 12345678\n" \
"" \
"\t12345678\t12345678\n" \
exit $FAILCOUNT