From c5964d67389aea3c7c740e8021a4b31a5c120dd7 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 14 Oct 2004 19:51:47 +0000 Subject: Basic string-join functionality svn:r2521 --- src/common/util.c | 26 ++++++++++++++++++++++++++ src/common/util.h | 1 + 2 files changed, 27 insertions(+) (limited to 'src/common') diff --git a/src/common/util.c b/src/common/util.c index 5a5fbddda..f99e29df2 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -609,6 +609,32 @@ int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, return n; } +char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate) +{ + int i; + size_t n = 0, jlen; + char *r = NULL, *dst, *src; + + tor_assert(sl && join); + jlen = strlen(join); + for (i = 0; i < sl->num_used; ++i) { + n += strlen(sl->list[i]); + n += jlen; + } + if (!terminate) n -= jlen; + dst = r = tor_malloc(n+1); + for (i = 0; i < sl->num_used; ) { + for (src = sl->list[i]; *src; ) + *dst++ = *src++; + if (++i < sl->num_used || terminate) { + memcpy(dst, join, jlen); + dst += jlen; + } + } + *dst = '\0'; + return r; +} + /* Splay-tree implementation of string-to-void* map */ struct strmap_entry_t { diff --git a/src/common/util.h b/src/common/util.h index 5961ca33e..2155a3a06 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -145,6 +145,7 @@ int smartlist_len(const smartlist_t *sl); #define SPLIT_IGNORE_BLANK 0x02 int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max); +char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate); #define SMARTLIST_FOREACH(sl, type, var, cmd) \ do { \ -- cgit v1.2.3