Skip to content

Commit 3422bdc

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]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aaede5c commit 3422bdc

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
@@ -268,6 +268,21 @@ void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
268268
strbuf_setlen(sb, sb->len + sb2->len);
269269
}
270270

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+
271286
void strbuf_addchars(struct strbuf *sb, int c, size_t n)
272287
{
273288
strbuf_grow(sb, n);

strbuf.h

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

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+
291298
/**
292299
* This function can be used to expand a format string containing
293300
* placeholders. To that end, it parses the string and calls the specified

0 commit comments

Comments
 (0)