aboutsummaryrefslogtreecommitdiff
path: root/builtin-stripspace.c
diff options
context:
space:
mode:
authorKristian Høgsberg <krh@redhat.com>2007-09-17 20:06:45 -0400
committerJunio C Hamano <gitster@pobox.com>2007-09-27 00:33:33 -0700
commit6d69b6f6ac27ab6f71a10da34b813ca25fd2a358 (patch)
tree9e581e5453aa5b43109e5c2eed57e892352e1ac4 /builtin-stripspace.c
parenta9390b9fcefb18c4ccdb521086a051bc9112e03d (diff)
downloadgit-6d69b6f6ac27ab6f71a10da34b813ca25fd2a358.tar.gz
git-6d69b6f6ac27ab6f71a10da34b813ca25fd2a358.tar.xz
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>
Diffstat (limited to 'builtin-stripspace.c')
-rw-r--r--builtin-stripspace.c56
1 files changed, 25 insertions, 31 deletions
diff --git a/builtin-stripspace.c b/builtin-stripspace.c
index 1ce284710..c0b21301b 100644
--- a/builtin-stripspace.c
+++ b/builtin-stripspace.c
@@ -8,17 +8,13 @@
*/
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;
}
@@ -34,42 +30,42 @@ static size_t cleanup(char *line, size_t 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)
@@ -85,9 +81,7 @@ 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);