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>
This commit is contained in:
Kristian Høgsberg 2007-09-17 20:06:45 -04:00 committed by Junio C Hamano
parent a9390b9fce
commit 6d69b6f6ac
4 changed files with 28 additions and 36 deletions

View File

@ -8,17 +8,13 @@
*/ */
static size_t cleanup(char *line, size_t len) static size_t cleanup(char *line, size_t len)
{ {
if (len) { while (len) {
if (line[len - 1] == '\n') unsigned char c = line[len - 1];
len--; if (!isspace(c))
break;
while (len) { len--;
unsigned char c = line[len - 1];
if (!isspace(c))
break;
len--;
}
} }
return 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, * If the input has only empty lines and spaces,
* no output will be produced. * 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 "#". * 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; size_t i, j, len, newlen;
char *eol; char *eol;
for (i = j = 0; i < length; i += len, j += newlen) { /* We may have to add a newline. */
eol = memchr(buffer + i, '\n', length - i); strbuf_grow(sb, 1);
len = eol ? eol - (buffer + i) + 1 : length - i;
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; newlen = 0;
continue; continue;
} }
newlen = cleanup(buffer + i, len); newlen = cleanup(sb->buf + i, len);
/* Not just an empty line? */ /* Not just an empty line? */
if (newlen) { if (newlen) {
if (empties != -1) if (empties > 0 && j > 0)
buffer[j++] = '\n'; sb->buf[j++] = '\n';
if (empties > 0)
buffer[j++] = '\n';
empties = 0; empties = 0;
memmove(buffer + j, buffer + i, newlen); memmove(sb->buf + j, sb->buf + i, newlen);
continue; 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) 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) if (strbuf_read(&buf, 0, 1024) < 0)
die("could not read the input"); die("could not read the input");
strbuf_setlen(&buf, stripspace(buf.buf, buf.len, strip_comments)); stripspace(&buf, strip_comments);
if (buf.len)
strbuf_addch(&buf, '\n');
write_or_die(1, buf.buf, buf.len); write_or_die(1, buf.buf, buf.len);
strbuf_release(&buf); strbuf_release(&buf);

View File

@ -291,14 +291,11 @@ static void create_tag(const unsigned char *object, const char *tag,
free(path); free(path);
} }
strbuf_setlen(buf, stripspace(buf->buf, buf->len, 1)); stripspace(buf, 1);
if (!message && !buf->len) if (!message && !buf->len)
die("no tag message?"); 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); strbuf_insert(buf, 0, header_buf, header_len);
if (sign && do_sign(buf) < 0) if (sign && do_sign(buf) < 0)

View File

@ -7,7 +7,6 @@ extern const char git_version_string[];
extern const char git_usage_string[]; extern const char git_usage_string[];
extern void help_unknown_cmd(const char *cmd); 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 int write_tree(unsigned char *sha1, int missing_ok, const char *prefix);
extern void prune_packed_objects(int); extern void prune_packed_objects(int);

View File

@ -112,4 +112,6 @@ extern int strbuf_read_file(struct strbuf *sb, const char *path);
extern int strbuf_getline(struct strbuf *, FILE *, int); extern int strbuf_getline(struct strbuf *, FILE *, int);
extern void stripspace(struct strbuf *buf, int skip_comments);
#endif /* STRBUF_H */ #endif /* STRBUF_H */