-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix git-stripspace to process correctly long lines and spaces.
Now the implementation gets more memory to store completely each line before removing trailing spaces, and does it right when the last line of the file ends with spaces and no newline at the end. Function stripspace needs again to be non-static in order to call it from "builtin-tag.c" and the upcoming "builtin-commit.c". A new parameter skip_comments was also added to the stripspace function to optionally strips every shell #comment from the input, needed for doing this task on those programs. Signed-off-by: Carlos Rica <jasampler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
- Loading branch information
Carlos Rica
authored and
Junio C Hamano
committed
Jun 27, 2007
1 parent
384f122
commit 9690c11
Showing
2 changed files
with
46 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,79 @@ | ||
#include "builtin.h" | ||
#include "cache.h" | ||
|
||
/* | ||
* Remove empty lines from the beginning and end. | ||
* Remove trailing spaces from a line. | ||
* | ||
* Turn multiple consecutive empty lines into just one | ||
* empty line. Return true if it is an incomplete line. | ||
* If the line ends with newline, it will be removed too. | ||
* Returns the new length of the string. | ||
*/ | ||
static int cleanup(char *line) | ||
static int cleanup(char *line, int len) | ||
{ | ||
int len = strlen(line); | ||
if (len) { | ||
if (line[len - 1] == '\n') | ||
len--; | ||
|
||
if (len && line[len-1] == '\n') { | ||
if (len == 1) | ||
return 0; | ||
do { | ||
unsigned char c = line[len-2]; | ||
while (len) { | ||
unsigned char c = line[len - 1]; | ||
if (!isspace(c)) | ||
break; | ||
line[len-2] = '\n'; | ||
len--; | ||
line[len] = 0; | ||
} while (len > 1); | ||
return 0; | ||
} | ||
line[len] = 0; | ||
} | ||
return 1; | ||
return len; | ||
} | ||
|
||
static void stripspace(FILE *in, FILE *out) | ||
/* | ||
* Remove empty lines from the beginning and end | ||
* and also trailing spaces from every line. | ||
* | ||
* Turn multiple consecutive empty lines between paragraphs | ||
* into just one empty line. | ||
* | ||
* If the input has only empty lines and spaces, | ||
* no output will be produced. | ||
* | ||
* Enable skip_comments to skip every line starting with "#". | ||
*/ | ||
void stripspace(FILE *in, FILE *out, int skip_comments) | ||
{ | ||
int empties = -1; | ||
int incomplete = 0; | ||
char line[1024]; | ||
int alloc = 1024; | ||
char *line = xmalloc(alloc); | ||
|
||
while (fgets(line, alloc, in)) { | ||
int len = strlen(line); | ||
|
||
while (fgets(line, sizeof(line), in)) { | ||
incomplete = cleanup(line); | ||
while (len == alloc - 1 && line[len - 1] != '\n') { | ||
alloc = alloc_nr(alloc); | ||
line = xrealloc(line, alloc); | ||
fgets(line + len, alloc - len, in); | ||
len += strlen(line + len); | ||
} | ||
|
||
if (skip_comments && line[0] == '#') | ||
continue; | ||
len = cleanup(line, len); | ||
|
||
/* Not just an empty line? */ | ||
if (line[0] != '\n') { | ||
if (len) { | ||
if (empties > 0) | ||
fputc('\n', out); | ||
empties = 0; | ||
fputs(line, out); | ||
fputc('\n', out); | ||
continue; | ||
} | ||
if (empties < 0) | ||
continue; | ||
empties++; | ||
} | ||
if (incomplete) | ||
fputc('\n', out); | ||
free(line); | ||
} | ||
|
||
int cmd_stripspace(int argc, const char **argv, const char *prefix) | ||
{ | ||
stripspace(stdin, stdout); | ||
stripspace(stdin, stdout, 0); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters