|
1 | 1 | #include "builtin.h" |
2 | 2 | #include "cache.h" |
3 | | - |
4 | | -/* |
5 | | - * Returns the length of a line, without trailing spaces. |
6 | | - * |
7 | | - * If the line ends with newline, it will be removed too. |
8 | | - */ |
9 | | -static size_t cleanup(char *line, size_t len) |
10 | | -{ |
11 | | - while (len) { |
12 | | - unsigned char c = line[len - 1]; |
13 | | - if (!isspace(c)) |
14 | | - break; |
15 | | - len--; |
16 | | - } |
17 | | - |
18 | | - return len; |
19 | | -} |
20 | | - |
21 | | -/* |
22 | | - * Remove empty lines from the beginning and end |
23 | | - * and also trailing spaces from every line. |
24 | | - * |
25 | | - * Turn multiple consecutive empty lines between paragraphs |
26 | | - * into just one empty line. |
27 | | - * |
28 | | - * If the input has only empty lines and spaces, |
29 | | - * no output will be produced. |
30 | | - * |
31 | | - * If last line does not have a newline at the end, one is added. |
32 | | - * |
33 | | - * Enable skip_comments to skip every line starting with comment |
34 | | - * character. |
35 | | - */ |
36 | | -void stripspace(struct strbuf *sb, int skip_comments) |
37 | | -{ |
38 | | - int empties = 0; |
39 | | - size_t i, j, len, newlen; |
40 | | - char *eol; |
41 | | - |
42 | | - /* We may have to add a newline. */ |
43 | | - strbuf_grow(sb, 1); |
44 | | - |
45 | | - for (i = j = 0; i < sb->len; i += len, j += newlen) { |
46 | | - eol = memchr(sb->buf + i, '\n', sb->len - i); |
47 | | - len = eol ? eol - (sb->buf + i) + 1 : sb->len - i; |
48 | | - |
49 | | - if (skip_comments && len && sb->buf[i] == comment_line_char) { |
50 | | - newlen = 0; |
51 | | - continue; |
52 | | - } |
53 | | - newlen = cleanup(sb->buf + i, len); |
54 | | - |
55 | | - /* Not just an empty line? */ |
56 | | - if (newlen) { |
57 | | - if (empties > 0 && j > 0) |
58 | | - sb->buf[j++] = '\n'; |
59 | | - empties = 0; |
60 | | - memmove(sb->buf + j, sb->buf + i, newlen); |
61 | | - sb->buf[newlen + j++] = '\n'; |
62 | | - } else { |
63 | | - empties++; |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - strbuf_setlen(sb, j); |
68 | | -} |
| 3 | +#include "strbuf.h" |
69 | 4 |
|
70 | 5 | static void comment_lines(struct strbuf *buf) |
71 | 6 | { |
@@ -111,7 +46,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix) |
111 | 46 | die_errno("could not read the input"); |
112 | 47 |
|
113 | 48 | if (mode == STRIP_SPACE) |
114 | | - stripspace(&buf, strip_comments); |
| 49 | + strbuf_stripspace(&buf, strip_comments); |
115 | 50 | else |
116 | 51 | comment_lines(&buf); |
117 | 52 |
|
|
0 commit comments