From: "Kristian Høgsberg" <krh@redhat.com>
To: git@vger.kernel.org
Cc: "Kristian Høgsberg" <krh@redhat.com>
Subject: [PATCH 4/7] Clean up stripspace a bit, use strbuf even more.
Date: Mon, 17 Sep 2007 20:06:45 -0400 [thread overview]
Message-ID: <11900740153845-git-send-email-krh@redhat.com> (raw)
In-Reply-To: <11900740142347-git-send-email-krh@redhat.com>
Signed-off-by: Kristian Høgsberg <krh@redhat.com>
---
builtin-stripspace.c | 56 ++++++++++++++++++++++---------------------------
builtin-tag.c | 5 +---
builtin.h | 1 -
strbuf.h | 2 +
4 files changed, 28 insertions(+), 36 deletions(-)
diff --git a/builtin-stripspace.c b/builtin-stripspace.c
index c4cf2f0..a5e7518 100644
--- a/builtin-stripspace.c
+++ b/builtin-stripspace.c
@@ -9,17 +9,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;
}
@@ -35,42 +31,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)
@@ -86,9 +82,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);
diff --git a/builtin-tag.c b/builtin-tag.c
index 9f29342..68c9a20 100644
--- a/builtin-tag.c
+++ b/builtin-tag.c
@@ -297,14 +297,11 @@ static void create_tag(const unsigned char *object, const char *tag,
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 03ee7bf..d6f2c76 100644
--- a/builtin.h
+++ b/builtin.h
@@ -7,7 +7,6 @@ extern const char git_version_string[];
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 21fc111..5960637 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -98,4 +98,6 @@ extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
extern void read_line(struct strbuf *, FILE *, int);
+extern void stripspace(struct strbuf *buf, int skip_comments);
+
#endif /* STRBUF_H */
--
1.5.3.1.993.gbf388-dirty
next prev parent reply other threads:[~2007-09-18 0:07 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-09-18 0:06 [PATCH 1/7] Enable wt-status output to a given FILE pointer Kristian Høgsberg
2007-09-18 0:06 ` [PATCH 2/7] Enable wt-status to run against non-standard index file Kristian Høgsberg
2007-09-18 0:06 ` [PATCH 3/7] Introduce entry point for launching add--interactive Kristian Høgsberg
2007-09-18 0:06 ` Kristian Høgsberg [this message]
2007-09-18 0:06 ` [PATCH 5/7] Add strbuf_read_file() Kristian Høgsberg
2007-09-18 0:06 ` [PATCH 6/7] Export rerere() and launch_editor() Kristian Høgsberg
2007-09-18 0:06 ` [PATCH 7/7] Implement git commit as a builtin command Kristian Høgsberg
2007-09-18 13:58 ` Johannes Schindelin
2007-09-18 15:07 ` Kristian Høgsberg
2007-09-20 1:27 ` Junio C Hamano
2007-09-21 17:18 ` Kristian Høgsberg
2007-09-21 19:32 ` Junio C Hamano
2007-09-24 20:27 ` Kristian Høgsberg
2007-09-18 13:14 ` [PATCH 6/7] Export rerere() and launch_editor() Johannes Schindelin
2007-09-19 23:52 ` Junio C Hamano
2007-09-21 18:01 ` Kristian Høgsberg
2007-09-18 13:12 ` [PATCH 4/7] Clean up stripspace a bit, use strbuf even more Johannes Schindelin
2007-09-18 13:52 ` Kristian Høgsberg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=11900740153845-git-send-email-krh@redhat.com \
--to=krh@redhat.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).