svn commit: trunk/busybox: coreutils testsuite

vda at busybox.net vda at busybox.net
Sun Dec 14 15:45:25 UTC 2008


Author: vda
Date: 2008-12-14 07:45:25 -0800 (Sun, 14 Dec 2008)
New Revision: 24413

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

function                                             old     new   delta
expand_main                                          698     676     -22
xputchar                                              53       -     -53



Added:
   trunk/busybox/testsuite/expand.tests

Modified:
   trunk/busybox/coreutils/expand.c


Changeset:
Modified: trunk/busybox/coreutils/expand.c
===================================================================
--- trunk/busybox/coreutils/expand.c	2008-12-14 14:49:06 UTC (rev 24412)
+++ trunk/busybox/coreutils/expand.c	2008-12-14 15:45:25 UTC (rev 24413)
@@ -29,51 +29,43 @@
 	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
-static void expand(FILE *file, unsigned tab_size, unsigned opt)
+static void expand(FILE *file, int tab_size, unsigned opt)
 {
 	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) {
-		convert = 1;
-		pos = 0;
-		ptr = line;
-		while (*line) {
-			pos++;
-			if (*line == '\t' && convert) {
-				for (; pos < tab_size; pos++) {
-					xputchar(' ');
-				}
-			} else {
-				if ((opt & OPT_INITIAL) && !isblank(*line)) {
-					convert = 0;
-				}
-				xputchar(*line);
+		int pos;
+		unsigned char c;
+		char *ptr = line;
+
+		goto start;
+		while ((c = *ptr) != '\0') {
+			if ((opt & OPT_INITIAL) && !isblank(c)) {
+				fputs(ptr, stdout);
+				break;
 			}
-			if (pos == tab_size) {
-				pos = 0;
+			ptr++;
+			if (c == '\t') {
+				c = ' ';
+				while (++pos < 0)
+					bb_putchar(c);
 			}
-			line++;
+			bb_putchar(c);
+			if (++pos >= 0) {
+ start:
+				pos = tab_size;
+			}
 		}
-		free(ptr);
+		free(line);
 	}
 }
 #endif
 
 #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 *ptr;
@@ -101,11 +93,11 @@
 				if (i) {
 					for (; i > 0; i--) {
  put_tab:
-						xputchar('\t');
+						bb_putchar('\t');
 					}
 				} else {
 					for (i = pos % tab_size; i > 0; i--) {
-						xputchar(' ');
+						bb_putchar(' ');
 					}
 				}
 				pos = 0;
@@ -116,7 +108,7 @@
 				if (opt & OPT_ALL) {
 					column++;
 				}
-				xputchar(*line);
+				bb_putchar(*line);
 				line++;
 			}
 		}

Added: trunk/busybox/testsuite/expand.tests
===================================================================
--- trunk/busybox/testsuite/expand.tests	                        (rev 0)
+++ trunk/busybox/testsuite/expand.tests	2008-12-14 15:45:25 UTC (rev 24413)
@@ -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


Property changes on: trunk/busybox/testsuite/expand.tests
___________________________________________________________________
Name: svn:executable
   + *




More information about the busybox-cvs mailing list