From e71c4a88f65ca1d325f52b19bf79c3660eab50f7 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu Date: Mon, 25 Feb 2019 23:16:06 +0000 Subject: strbuf.c: add `strbuf_join_argv()` Implement `strbuf_join_argv()` to join arguments into a strbuf. Signed-off-by: Paul-Sebastian Ungureanu Signed-off-by: Thomas Gummerer Signed-off-by: Junio C Hamano --- strbuf.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'strbuf.c') diff --git a/strbuf.c b/strbuf.c index f6a6cf78b9..82e90f1dfe 100644 --- a/strbuf.c +++ b/strbuf.c @@ -268,6 +268,21 @@ void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2) strbuf_setlen(sb, sb->len + sb2->len); } +const char *strbuf_join_argv(struct strbuf *buf, + int argc, const char **argv, char delim) +{ + if (!argc) + return buf->buf; + + strbuf_addstr(buf, *argv); + while (--argc) { + strbuf_addch(buf, delim); + strbuf_addstr(buf, *(++argv)); + } + + return buf->buf; +} + void strbuf_addchars(struct strbuf *sb, int c, size_t n) { strbuf_grow(sb, n); -- cgit v1.2.3 From 5ef264dbdbc48b44ad5fc37e7542f3dc70e3e8c5 Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu Date: Mon, 25 Feb 2019 23:16:07 +0000 Subject: strbuf.c: add `strbuf_insertf()` and `strbuf_vinsertf()` Implement `strbuf_insertf()` and `strbuf_vinsertf()` to insert data using a printf format string. Original-idea-by: Johannes Schindelin Signed-off-by: Paul-Sebastian Ungureanu Helped-by: Johannes Sixt Signed-off-by: Thomas Gummerer Signed-off-by: Junio C Hamano --- strbuf.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'strbuf.c') diff --git a/strbuf.c b/strbuf.c index 82e90f1dfe..87ecf7f975 100644 --- a/strbuf.c +++ b/strbuf.c @@ -249,6 +249,42 @@ void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len) strbuf_splice(sb, pos, 0, data, len); } +void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt, va_list ap) +{ + int len, len2; + char save; + va_list cp; + + if (pos > sb->len) + die("`pos' is too far after the end of the buffer"); + va_copy(cp, ap); + len = vsnprintf(sb->buf + sb->len, 0, fmt, cp); + va_end(cp); + if (len < 0) + BUG("your vsnprintf is broken (returned %d)", len); + if (!len) + return; /* nothing to do */ + if (unsigned_add_overflows(sb->len, len)) + die("you want to use way too much memory"); + strbuf_grow(sb, len); + memmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos); + /* vsnprintf() will append a NUL, overwriting one of our characters */ + save = sb->buf[pos + len]; + len2 = vsnprintf(sb->buf + pos, len + 1, fmt, ap); + sb->buf[pos + len] = save; + if (len2 != len) + BUG("your vsnprintf is broken (returns inconsistent lengths)"); + strbuf_setlen(sb, sb->len + len); +} + +void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + strbuf_vinsertf(sb, pos, fmt, ap); + va_end(ap); +} + void strbuf_remove(struct strbuf *sb, size_t pos, size_t len) { strbuf_splice(sb, pos, len, "", 0); -- cgit v1.2.3