unexpand: fix incorrect expansion, add test for it
function old new delta expand_main 676 656 -20
This commit is contained in:
		| @@ -68,51 +68,39 @@ static void expand(FILE *file, int tab_size, unsigned opt) | |||||||
| static void unexpand(FILE *file, unsigned tab_size, unsigned opt) | static void unexpand(FILE *file, unsigned tab_size, unsigned opt) | ||||||
| { | { | ||||||
| 	char *line; | 	char *line; | ||||||
| 	char *ptr; |  | ||||||
| 	int convert; |  | ||||||
| 	int pos; |  | ||||||
| 	int i = 0; |  | ||||||
| 	unsigned column = 0; |  | ||||||
|  |  | ||||||
| 	while ((line = xmalloc_fgets(file)) != NULL) { | 	while ((line = xmalloc_fgets(file)) != NULL) { | ||||||
| 		convert = 1; | 		char *ptr = line; | ||||||
| 		pos = 0; | 		unsigned column = 0; | ||||||
| 		ptr = line; |  | ||||||
| 		while (*line) { | 		while (*ptr) { | ||||||
| 			while ((*line == ' ' || *line == '\t') && convert) { | 			unsigned n; | ||||||
| 				pos += (*line == ' ') ? 1 : tab_size; |  | ||||||
| 				line++; | 			while (*ptr == ' ') { | ||||||
| 				column++; | 				column++; | ||||||
| 				if ((opt & OPT_ALL) && column == tab_size) { | 				ptr++; | ||||||
| 					column = 0; |  | ||||||
| 					goto put_tab; |  | ||||||
| 				} |  | ||||||
| 			} | 			} | ||||||
| 			if (pos) { | 			if (*ptr == '\t') { | ||||||
| 				i = pos / tab_size; | 				column += tab_size - (column % tab_size); | ||||||
| 				if (i) { | 				ptr++; | ||||||
| 					for (; i > 0; i--) { | 				continue; | ||||||
|  put_tab: |  | ||||||
| 						bb_putchar('\t'); |  | ||||||
| 					} |  | ||||||
| 				} else { |  | ||||||
| 					for (i = pos % tab_size; i > 0; i--) { |  | ||||||
| 						bb_putchar(' '); |  | ||||||
| 					} |  | ||||||
| 				} |  | ||||||
| 				pos = 0; |  | ||||||
| 			} else { |  | ||||||
| 				if (opt & OPT_INITIAL) { |  | ||||||
| 					convert = 0; |  | ||||||
| 				} |  | ||||||
| 				if (opt & OPT_ALL) { |  | ||||||
| 					column++; |  | ||||||
| 				} |  | ||||||
| 				bb_putchar(*line); |  | ||||||
| 				line++; |  | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | 			n = column / tab_size; | ||||||
|  | 			column = column % tab_size; | ||||||
|  | 			while (n--) | ||||||
|  | 				putchar('\t'); | ||||||
|  |  | ||||||
|  | 			if ((opt & OPT_INITIAL) && ptr != line) { | ||||||
|  | 				printf("%*s%s", column, "", ptr); | ||||||
|  | 				break; | ||||||
|  | 			} | ||||||
|  | 			n = strcspn(ptr, "\t "); | ||||||
|  | 			printf("%*s%.*s", column, "", n, ptr); | ||||||
|  | 			ptr += n; | ||||||
|  | 			column = (column + n) % tab_size; | ||||||
| 		} | 		} | ||||||
| 		free(ptr); | 		free(line); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|   | |||||||
							
								
								
									
										30
									
								
								testsuite/unexpand.tests
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										30
									
								
								testsuite/unexpand.tests
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,30 @@ | |||||||
|  | #!/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 "unexpand case 1" "unexpand" \ | ||||||
|  | 	"\t12345678\n" "" "        12345678\n" \ | ||||||
|  |  | ||||||
|  | testing "unexpand case 2" "unexpand" \ | ||||||
|  | 	"\t 12345678\n" "" "         12345678\n" \ | ||||||
|  |  | ||||||
|  | testing "unexpand case 3" "unexpand" \ | ||||||
|  | 	"\t  12345678\n" "" "          12345678\n" \ | ||||||
|  |  | ||||||
|  | testing "unexpand case 4" "unexpand" \ | ||||||
|  | 	"\t12345678\n" "" "       \t12345678\n" \ | ||||||
|  |  | ||||||
|  | testing "unexpand case 5" "unexpand" \ | ||||||
|  | 	"\t12345678\n" "" "      \t12345678\n" \ | ||||||
|  |  | ||||||
|  | testing "unexpand case 6" "unexpand" \ | ||||||
|  | 	"\t12345678\n" "" "     \t12345678\n" \ | ||||||
|  |  | ||||||
|  | testing "unexpand case 7" "unexpand" \ | ||||||
|  | 	"123\t 45678\n" "" "123 \t 45678\n" \ | ||||||
|  |  | ||||||
|  | exit $FAILCOUNT | ||||||
		Reference in New Issue
	
	Block a user