aboutsummaryrefslogtreecommitdiff
path: root/strbuf.h
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2015-09-24 17:05:43 -0400
committerJunio C Hamano <gitster@pobox.com>2015-09-25 10:18:18 -0700
commit399ad553ce87fca77a9bc5a0e734a361a9e8a5a3 (patch)
treea095f0c903c94fba7a506970f91ce68d87b2fc05 /strbuf.h
parentbb3788cebb814aa003941abcf484da872aa61412 (diff)
downloadgit-399ad553ce87fca77a9bc5a0e734a361a9e8a5a3.tar.gz
git-399ad553ce87fca77a9bc5a0e734a361a9e8a5a3.tar.xz
strbuf: make strbuf_complete_line more generic
The strbuf_complete_line function makes sure that a buffer ends in a newline. But we may want to do this for any character (e.g., "/" on the end of a path). Let's factor out a generic version, and keep strbuf_complete_line as a thin wrapper. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'strbuf.h')
-rw-r--r--strbuf.h15
1 files changed, 13 insertions, 2 deletions
diff --git a/strbuf.h b/strbuf.h
index aef279465..43f27c3a6 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -491,10 +491,21 @@ extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *
*/
extern void strbuf_addstr_xml_quoted(struct strbuf *sb, const char *s);
+/**
+ * "Complete" the contents of `sb` by ensuring that either it ends with the
+ * character `term`, or it is empty. This can be used, for example,
+ * to ensure that text ends with a newline, but without creating an empty
+ * blank line if there is no content in the first place.
+ */
+static inline void strbuf_complete(struct strbuf *sb, char term)
+{
+ if (sb->len && sb->buf[sb->len - 1] != term)
+ strbuf_addch(sb, term);
+}
+
static inline void strbuf_complete_line(struct strbuf *sb)
{
- if (sb->len && sb->buf[sb->len - 1] != '\n')
- strbuf_addch(sb, '\n');
+ strbuf_complete(sb, '\n');
}
extern int strbuf_branchname(struct strbuf *sb, const char *name);