Function stripspace now gets a buffer instead file descriptors.
An implementation easier to call from builtins. It is designed to be used from the upcoming builtin-tag.c and builtin-commit.c, because both need to remove unwanted spaces from messages. Signed-off-by: Carlos Rica <jasampler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Carlos Rica committed
Jul 11, 2007 at 20:50 UTC
975e0daf5edba09060147d643e628b8c89e1d467
2 files changed
+38
-25
builtin-stripspace.c
+37
-24
@@ -2,12 +2,11 @@
2
#include "cache.h"
3
4
/*
5
- * Remove trailing spaces from a line.
5
+ * Returns the length of a line, without trailing spaces.
6
*
7
* If the line ends with newline, it will be removed too.
8
- * Returns the new length of the string.
8
*/
10
-static int cleanup(char *line, int len)
9
+static size_t cleanup(char *line, size_t len)
10
{
11
if (len) {
12
if (line[len - 1] == '\n')
@@ -19,7 +18,6 @@ static int cleanup(char *line, int len)
18
break;
19
len--;
20
}
22
- line[len] = 0;
21
}
22
return len;
23
}
@@ -28,52 +26,67 @@ static int cleanup(char *line, int len)
26
* Remove empty lines from the beginning and end
27
* and also trailing spaces from every line.
28
*
29
+ * Note that the buffer will not be NUL-terminated.
30
+ *
31
* Turn multiple consecutive empty lines between paragraphs
32
* into just one empty line.
33
*
34
* If the input has only empty lines and spaces,
35
* no output will be produced.
36
*
37
+ * If last line has a newline at the end, it will be removed.
38
+ *
39
* Enable skip_comments to skip every line starting with "#".
40
*/
39
-void stripspace(FILE *in, FILE *out, int skip_comments)
41
+size_t stripspace(char *buffer, size_t length, int skip_comments)
42
{
43
int empties = -1;
42
- int alloc = 1024;
43
- char *line = xmalloc(alloc);
44
+ size_t i, j, len, newlen;
45
+ char *eol;
46
45
- while (fgets(line, alloc, in)) {
46
- int len = strlen(line);
47
+ for (i = j = 0; i < length; i += len, j += newlen) {
48
+ eol = memchr(buffer + i, '\n', length - i);
49
+ len = eol ? eol - (buffer + i) + 1 : length - i;
50
48
- while (len == alloc - 1 && line[len - 1] != '\n') {
49
- alloc = alloc_nr(alloc);
50
- line = xrealloc(line, alloc);
51
- fgets(line + len, alloc - len, in);
52
- len += strlen(line + len);
53
- }
54
-
55
- if (skip_comments && line[0] == '#')
51
+ if (skip_comments && len && buffer[i] == '#') {
52
+ newlen = 0;
53
continue;
57
- len = cleanup(line, len);
54
+ }
55
+ newlen = cleanup(buffer + i, len);
56
57
/* Not just an empty line? */
60
- if (len) {
58
+ if (newlen) {
59
+ if (empties != -1)
60
+ buffer[j++] = '\n';
61
if (empties > 0)
62
- fputc('\n', out);
62
+ buffer[j++] = '\n';
63
empties = 0;
64
- fputs(line, out);
65
- fputc('\n', out);
64
+ memmove(buffer + j, buffer + i, newlen);
65
continue;
66
}
67
if (empties < 0)
68
continue;
69
empties++;
70
}
72
- free(line);
71
+
72
+ return j;
73
}
74
75
int cmd_stripspace(int argc, const char **argv, const char *prefix)
76
{
77
- stripspace(stdin, stdout, 0);
77
+ char *buffer;
78
+ unsigned long size;
79
+
80
+ size = 1024;
81
+ buffer = xmalloc(size);
82
+ if (read_pipe(0, &buffer, &size))
83
+ die("could not read the input");
84
+
85
+ size = stripspace(buffer, size, 0);
86
+ write_or_die(1, buffer, size);
87
+ if (size)
88
+ putc('\n', stdout);
89
+
90
+ free(buffer);
91
return 0;
92
}
builtin.h
+1
-1
@@ -7,7 +7,7 @@ extern const char git_version_string[];
7
extern const char git_usage_string[];
8
9
extern void help_unknown_cmd(const char *cmd);
10
-extern void stripspace(FILE *in, FILE *out, int skip_comments);
10
+extern size_t stripspace(char *buffer, size_t length, int skip_comments);
11
extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix);
12
extern void prune_packed_objects(int);
13