diff options
author | Nick Mathewson <nickm@torproject.org> | 2004-10-14 19:51:47 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2004-10-14 19:51:47 +0000 |
commit | c5964d67389aea3c7c740e8021a4b31a5c120dd7 (patch) | |
tree | ddd71ba15a3d0a9dc7dc2ee17e08bf16f007ce14 /src/common | |
parent | 9e8e006c1eb3c2c79188ca55322c7a49686d37dc (diff) | |
download | tor-c5964d67389aea3c7c740e8021a4b31a5c120dd7.tar tor-c5964d67389aea3c7c740e8021a4b31a5c120dd7.tar.gz |
Basic string-join functionality
svn:r2521
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/util.c | 26 | ||||
-rw-r--r-- | src/common/util.h | 1 |
2 files changed, 27 insertions, 0 deletions
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 { \ |