Browse Source

str_talloc: make strjoin much more efficient.

Inspired by patch from Volker.
Rusty Russell 15 years ago
parent
commit
ed7aec77da
1 changed files with 8 additions and 2 deletions
  1. 8 2
      ccan/str_talloc/str_talloc.c

+ 8 - 2
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;
 }