summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2011-04-19 20:23:59 +0930
committerRusty Russell <rusty@rustcorp.com.au>2011-04-19 20:30:08 +0930
commited7aec77da970556c0281e2c7573bf00d1390ff1 (patch)
treed9ea1fc8faef8454c2713c42e66576385d5a7e16
parent377395cfa60d0773aab50c977f71b06596ebbc52 (diff)
str_talloc: make strjoin much more efficient.
Inspired by patch from Volker.
-rw-r--r--ccan/str_talloc/str_talloc.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/ccan/str_talloc/str_talloc.c b/ccan/str_talloc/str_talloc.c
index 3bcb1f2a..88e02ef2 100644
--- a/ccan/str_talloc/str_talloc.c
+++ b/ccan/str_talloc/str_talloc.c
@@ -38,11 +38,17 @@ char *strjoin(const void *ctx, char *strings[], const char *delim)
{
unsigned int i;
char *ret = talloc_strdup(ctx, "");
+ size_t totlen = 0, dlen = strlen(delim);
for (i = 0; strings[i]; i++) {
- ret = talloc_append_string(ret, strings[i]);
- ret = talloc_append_string(ret, delim);
+ size_t len = strlen(strings[i]);
+ ret = talloc_realloc(ctx, ret, char, totlen + len + dlen + 1);
+ memcpy(ret + totlen, strings[i], len);
+ totlen += len;
+ memcpy(ret + totlen, delim, dlen);
+ totlen += dlen;
}
+ ret[totlen] = '\0';
return ret;
}