Skip to content

Commit 399ad55

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bb3788c commit 399ad55

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

strbuf.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,21 @@ extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *
491491
*/
492492
extern void strbuf_addstr_xml_quoted(struct strbuf *sb, const char *s);
493493

494+
/**
495+
* "Complete" the contents of `sb` by ensuring that either it ends with the
496+
* character `term`, or it is empty. This can be used, for example,
497+
* to ensure that text ends with a newline, but without creating an empty
498+
* blank line if there is no content in the first place.
499+
*/
500+
static inline void strbuf_complete(struct strbuf *sb, char term)
501+
{
502+
if (sb->len && sb->buf[sb->len - 1] != term)
503+
strbuf_addch(sb, term);
504+
}
505+
494506
static inline void strbuf_complete_line(struct strbuf *sb)
495507
{
496-
if (sb->len && sb->buf[sb->len - 1] != '\n')
497-
strbuf_addch(sb, '\n');
508+
strbuf_complete(sb, '\n');
498509
}
499510

500511
extern int strbuf_branchname(struct strbuf *sb, const char *name);

0 commit comments

Comments
 (0)