Clean up stripspace a bit, use strbuf even more.
Signed-off-by: Kristian Høgsberg <krh@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Kristian Høgsberg committed
Sep 17, 2007 at 20:06 UTC
6d69b6f6ac27ab6f71a10da34b813ca25fd2a358
4 files changed
+28
-36
builtin-stripspace.c
+25
-31
@@ -8,17 +8,13 @@
8
*/
9
static size_t cleanup(char *line, size_t len)
10
{
11
- if (len) {
12
- if (line[len - 1] == '\n')
13
- len--;
14
-
15
- while (len) {
16
- unsigned char c = line[len - 1];
17
- if (!isspace(c))
18
- break;
19
- len--;
20
- }
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
@@ -34,42 +30,42 @@ static size_t cleanup(char *line, size_t len)
30
* If the input has only empty lines and spaces,
31
* no output will be produced.
32
*
37
- * If last line has a newline at the end, it will be removed.
33
+ * If last line does not have a newline at the end, one is added.
34
*
35
* Enable skip_comments to skip every line starting with "#".
36
*/
41
-size_t stripspace(char *buffer, size_t length, int skip_comments)
37
+void stripspace(struct strbuf *sb, int skip_comments)
38
{
43
- int empties = -1;
39
+ int empties = 0;
40
size_t i, j, len, newlen;
41
char *eol;
42
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;
43
+ /* We may have to add a newline. */
44
+ strbuf_grow(sb, 1);
45
51
- if (skip_comments && len && buffer[i] == '#') {
46
+ for (i = j = 0; i < sb->len; i += len, j += newlen) {
47
+ eol = memchr(sb->buf + i, '\n', sb->len - i);
48
+ len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
49
+
50
+ if (skip_comments && len && sb->buf[i] == '#') {
51
newlen = 0;
52
continue;
53
}
55
- newlen = cleanup(buffer + i, len);
54
+ newlen = cleanup(sb->buf + i, len);
55
56
/* Not just an empty line? */
57
if (newlen) {
59
- if (empties != -1)
60
- buffer[j++] = '\n';
61
- if (empties > 0)
62
- buffer[j++] = '\n';
58
+ if (empties > 0 && j > 0)
59
+ sb->buf[j++] = '\n';
60
empties = 0;
64
- memmove(buffer + j, buffer + i, newlen);
65
- continue;
61
+ memmove(sb->buf + j, sb->buf + i, newlen);
62
+ sb->buf[newlen + j++] = '\n';
63
+ } else {
64
+ empties++;
65
}
67
- if (empties < 0)
68
- continue;
69
- empties++;
66
}
67
72
- return j;
68
+ strbuf_setlen(sb, j);
69
}
70
71
int cmd_stripspace(int argc, const char **argv, const char *prefix)
@@ -85,9 +81,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
81
if (strbuf_read(&buf, 0, 1024) < 0)
82
die("could not read the input");
83
88
- strbuf_setlen(&buf, stripspace(buf.buf, buf.len, strip_comments));
89
- if (buf.len)
90
- strbuf_addch(&buf, '\n');
84
+ stripspace(&buf, strip_comments);
85
86
write_or_die(1, buf.buf, buf.len);
87
strbuf_release(&buf);
builtin-tag.c
+1
-4
@@ -291,14 +291,11 @@ static void create_tag(const unsigned char *object, const char *tag,
291
free(path);
292
}
293
294
- strbuf_setlen(buf, stripspace(buf->buf, buf->len, 1));
294
+ stripspace(buf, 1);
295
296
if (!message && !buf->len)
297
die("no tag message?");
298
299
- /* insert the header and add the '\n' if needed: */
300
- if (buf->len)
301
- strbuf_addch(buf, '\n');
299
strbuf_insert(buf, 0, header_buf, header_len);
300
301
if (sign && do_sign(buf) < 0)
builtin.h
-1
@@ -7,7 +7,6 @@ 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 size_t stripspace(char *buffer, size_t length, int skip_comments);
10
extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix);
11
extern void prune_packed_objects(int);
12
strbuf.h
+2
@@ -112,4 +112,6 @@ extern int strbuf_read_file(struct strbuf *sb, const char *path);
112
113
extern int strbuf_getline(struct strbuf *, FILE *, int);
114
115
+extern void stripspace(struct strbuf *buf, int skip_comments);
116
+
117
#endif /* STRBUF_H */