summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: a9390b9)
raw | patch | inline | side by side (parent: a9390b9)
author | Kristian Høgsberg <krh@redhat.com> | |
Tue, 18 Sep 2007 00:06:45 +0000 (20:06 -0400) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Thu, 27 Sep 2007 07:33:33 +0000 (00:33 -0700) |
Signed-off-by: Kristian Høgsberg <krh@redhat.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-stripspace.c | patch | blob | history | |
builtin-tag.c | patch | blob | history | |
builtin.h | patch | blob | history | |
strbuf.h | patch | blob | history |
diff --git a/builtin-stripspace.c b/builtin-stripspace.c
index 1ce284710c77ef260d7f64f561e7acc0bed4a3ca..c0b21301ba4c126a49ed38b6983756b99a25aae0 100644 (file)
--- a/builtin-stripspace.c
+++ b/builtin-stripspace.c
*/
static size_t cleanup(char *line, size_t len)
{
- if (len) {
- if (line[len - 1] == '\n')
- len--;
-
- while (len) {
- unsigned char c = line[len - 1];
- if (!isspace(c))
- break;
- len--;
- }
+ while (len) {
+ unsigned char c = line[len - 1];
+ if (!isspace(c))
+ break;
+ len--;
}
+
return len;
}
* If the input has only empty lines and spaces,
* no output will be produced.
*
- * If last line has a newline at the end, it will be removed.
+ * If last line does not have a newline at the end, one is added.
*
* Enable skip_comments to skip every line starting with "#".
*/
-size_t stripspace(char *buffer, size_t length, int skip_comments)
+void stripspace(struct strbuf *sb, int skip_comments)
{
- int empties = -1;
+ int empties = 0;
size_t i, j, len, newlen;
char *eol;
- for (i = j = 0; i < length; i += len, j += newlen) {
- eol = memchr(buffer + i, '\n', length - i);
- len = eol ? eol - (buffer + i) + 1 : length - i;
+ /* We may have to add a newline. */
+ strbuf_grow(sb, 1);
- if (skip_comments && len && buffer[i] == '#') {
+ for (i = j = 0; i < sb->len; i += len, j += newlen) {
+ eol = memchr(sb->buf + i, '\n', sb->len - i);
+ len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
+
+ if (skip_comments && len && sb->buf[i] == '#') {
newlen = 0;
continue;
}
- newlen = cleanup(buffer + i, len);
+ newlen = cleanup(sb->buf + i, len);
/* Not just an empty line? */
if (newlen) {
- if (empties != -1)
- buffer[j++] = '\n';
- if (empties > 0)
- buffer[j++] = '\n';
+ if (empties > 0 && j > 0)
+ sb->buf[j++] = '\n';
empties = 0;
- memmove(buffer + j, buffer + i, newlen);
- continue;
+ memmove(sb->buf + j, sb->buf + i, newlen);
+ sb->buf[newlen + j++] = '\n';
+ } else {
+ empties++;
}
- if (empties < 0)
- continue;
- empties++;
}
- return j;
+ strbuf_setlen(sb, j);
}
int cmd_stripspace(int argc, const char **argv, const char *prefix)
if (strbuf_read(&buf, 0, 1024) < 0)
die("could not read the input");
- strbuf_setlen(&buf, stripspace(buf.buf, buf.len, strip_comments));
- if (buf.len)
- strbuf_addch(&buf, '\n');
+ stripspace(&buf, strip_comments);
write_or_die(1, buf.buf, buf.len);
strbuf_release(&buf);
diff --git a/builtin-tag.c b/builtin-tag.c
index fcbf9bbf18155b5e9b699bf12cd980561498992f..6132cac21868ee5a563278bd0480c82ed9ca74e9 100644 (file)
--- a/builtin-tag.c
+++ b/builtin-tag.c
free(path);
}
- strbuf_setlen(buf, stripspace(buf->buf, buf->len, 1));
+ stripspace(buf, 1);
if (!message && !buf->len)
die("no tag message?");
- /* insert the header and add the '\n' if needed: */
- if (buf->len)
- strbuf_addch(buf, '\n');
strbuf_insert(buf, 0, header_buf, header_len);
if (sign && do_sign(buf) < 0)
diff --git a/builtin.h b/builtin.h
index 03ee7bf780be93601f9190b733984ad82951952b..d6f2c76b86174e6353c3d6146368e3ff71406a22 100644 (file)
--- a/builtin.h
+++ b/builtin.h
extern const char git_usage_string[];
extern void help_unknown_cmd(const char *cmd);
-extern size_t stripspace(char *buffer, size_t length, int skip_comments);
extern int write_tree(unsigned char *sha1, int missing_ok, const char *prefix);
extern void prune_packed_objects(int);
diff --git a/strbuf.h b/strbuf.h
index d4d9e5663c58a9c7c019124194d95ef365e874f7..5657e3db1401dda1eea7d8be2bd36ec36502b187 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
extern int strbuf_getline(struct strbuf *, FILE *, int);
+extern void stripspace(struct strbuf *buf, int skip_comments);
+
#endif /* STRBUF_H */