6 * Returns the length of a line, without trailing spaces.
8 * If the line ends with newline, it will be removed too.
10 static size_t cleanup(char *line
, size_t len
)
13 if (line
[len
- 1] == '\n')
17 unsigned char c
= line
[len
- 1];
27 * Remove empty lines from the beginning and end
28 * and also trailing spaces from every line.
30 * Note that the buffer will not be NUL-terminated.
32 * Turn multiple consecutive empty lines between paragraphs
33 * into just one empty line.
35 * If the input has only empty lines and spaces,
36 * no output will be produced.
38 * If last line has a newline at the end, it will be removed.
40 * Enable skip_comments to skip every line starting with "#".
42 size_t stripspace(char *buffer
, size_t length
, int skip_comments
)
45 size_t i
, j
, len
, newlen
;
48 for (i
= j
= 0; i
< length
; i
+= len
, j
+= newlen
) {
49 eol
= memchr(buffer
+ i
, '\n', length
- i
);
50 len
= eol
? eol
- (buffer
+ i
) + 1 : length
- i
;
52 if (skip_comments
&& len
&& buffer
[i
] == '#') {
56 newlen
= cleanup(buffer
+ i
, len
);
58 /* Not just an empty line? */
65 memmove(buffer
+ j
, buffer
+ i
, newlen
);
76 int cmd_stripspace(int argc
, const char **argv
, const char *prefix
)
79 int strip_comments
= 0;
81 if (argc
> 1 && (!strcmp(argv
[1], "-s") ||
82 !strcmp(argv
[1], "--strip-comments")))
86 if (strbuf_read(&buf
, 0, 1024) < 0)
87 die("could not read the input");
89 strbuf_setlen(&buf
, stripspace(buf
.buf
, buf
.len
, strip_comments
));
91 strbuf_addch(&buf
, '\n');
93 write_or_die(1, buf
.buf
, buf
.len
);