strbuf.c: add `strbuf_join_argv()`
Implement `strbuf_join_argv()` to join arguments into a strbuf. Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Paul-Sebastian Ungureanu committed
Feb 25, 2019 at 23:16 UTC
e71c4a88f65ca1d325f52b19bf79c3660eab50f7
2 files changed
+22
strbuf.c
+15
@@ -268,6 +268,21 @@ void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
268
strbuf_setlen(sb, sb->len + sb2->len);
269
}
270
271
+const char *strbuf_join_argv(struct strbuf *buf,
272
+ int argc, const char **argv, char delim)
273
+{
274
+ if (!argc)
275
+ return buf->buf;
276
+
277
+ strbuf_addstr(buf, *argv);
278
+ while (--argc) {
279
+ strbuf_addch(buf, delim);
280
+ strbuf_addstr(buf, *(++argv));
281
+ }
282
+
283
+ return buf->buf;
284
+}
285
+
286
void strbuf_addchars(struct strbuf *sb, int c, size_t n)
287
{
288
strbuf_grow(sb, n);
strbuf.h
+7
@@ -288,6 +288,13 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)
288
*/
289
void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2);
290
291
+/**
292
+ * Join the arguments into a buffer. `delim` is put between every
293
+ * two arguments.
294
+ */
295
+const char *strbuf_join_argv(struct strbuf *buf, int argc,
296
+ const char **argv, char delim);
297
+
298
/**
299
* This function can be used to expand a format string containing
300
* placeholders. To that end, it parses the string and calls the specified