Skip to content

Commit 59db786

Browse files
ungpsdscho
authored andcommitted
strbuf.c: add strbuf_join_argv()
Implement `strbuf_join_argv()` to join arguments into a strbuf. Signed-off-by: Paul-Sebastian Ungureanu <[email protected]>
1 parent ab992eb commit 59db786

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

strbuf.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,21 @@ void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
259259
strbuf_setlen(sb, sb->len + sb2->len);
260260
}
261261

262+
const char *strbuf_join_argv(struct strbuf *buf,
263+
int argc, const char **argv, char delim)
264+
{
265+
if (!argc)
266+
return buf->buf;
267+
268+
strbuf_addstr(buf, *argv);
269+
while (--argc) {
270+
strbuf_addch(buf, delim);
271+
strbuf_addstr(buf, *(++argv));
272+
}
273+
274+
return buf->buf;
275+
}
276+
262277
void strbuf_addchars(struct strbuf *sb, int c, size_t n)
263278
{
264279
strbuf_grow(sb, n);

strbuf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)
284284
*/
285285
extern void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2);
286286

287+
/**
288+
* Join the arguments into a buffer. `delim` is put between every
289+
* two arguments.
290+
*/
291+
extern const char *strbuf_join_argv(struct strbuf *buf, int argc,
292+
const char **argv, char delim);
293+
287294
/**
288295
* This function can be used to expand a format string containing
289296
* placeholders. To that end, it parses the string and calls the specified

0 commit comments

Comments
 (0)