Commit e2da7d35 authored by Rusty Russell's avatar Rusty Russell

strjoin()

parent 779d8308
...@@ -31,3 +31,14 @@ char **strsplit(const void *ctx, const char *string, const char *delims, ...@@ -31,3 +31,14 @@ char **strsplit(const void *ctx, const char *string, const char *delims,
return lines; return lines;
} }
char *strjoin(const void *ctx, char *strings[], const char *delim)
{
unsigned int i;
char *ret = talloc_strdup(ctx, "");
for (i = 0; strings[i]; i++) {
ret = talloc_append_string(ret, strings[i]);
ret = talloc_append_string(ret, delim);
}
return ret;
}
...@@ -54,7 +54,8 @@ static inline bool strends(const char *str, const char *postfix) ...@@ -54,7 +54,8 @@ static inline bool strends(const char *str, const char *postfix)
* This function splits a single string into multiple strings. The * This function splits a single string into multiple strings. The
* original string is untouched: an array is allocated (using talloc) * original string is untouched: an array is allocated (using talloc)
* pointing to copies of each substring. Multiple delimiters result * pointing to copies of each substring. Multiple delimiters result
* in empty substrings. * in empty substrings. By definition, no delimiters will appear in
* the substrings.
* *
* The final char * in the array will be NULL, so you can use this or * The final char * in the array will be NULL, so you can use this or
* @nump to find the array length. * @nump to find the array length.
...@@ -76,4 +77,29 @@ static inline bool strends(const char *str, const char *postfix) ...@@ -76,4 +77,29 @@ static inline bool strends(const char *str, const char *postfix)
*/ */
char **strsplit(const void *ctx, const char *string, const char *delims, char **strsplit(const void *ctx, const char *string, const char *delims,
unsigned int *nump); unsigned int *nump);
/**
* strjoin - Join an array of substrings into one long string
* @ctx: the context to tallocate from (often NULL)
* @strings: the NULL-terminated array of strings to join
* @delim: the delimiter to insert between the strings
*
* This function joins an array of strings into a single string. The
* return value is allocated using talloc. Each string in @strings is
* followed by a copy of @delim.
*
* Example:
* // Append the string "--EOL" to each line.
* char *append_to_all_lines(const char *string)
* {
* char **lines, *ret;
* unsigned int i, num, newnum;
*
* lines = strsplit(NULL, string, "\n", NULL);
* ret = strjoin(NULL, lines, "-- EOL\n");
* talloc_free(lines);
* return ret;
* }
*/
char *strjoin(const void *ctx, char *strings[], const char *delim);
#endif /* CCAN_STRING_H */ #endif /* CCAN_STRING_H */
...@@ -7,7 +7,9 @@ ...@@ -7,7 +7,9 @@
/* FIXME: ccanize */ /* FIXME: ccanize */
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
static char *substrings[] = { "far", "bar", "baz", "b", "ba", "z", "ar" }; static char *substrings[] = { "far", "bar", "baz", "b", "ba", "z", "ar", NULL };
#define NUM_SUBSTRINGS (ARRAY_SIZE(substrings) - 1)
static char *strdup_rev(const char *s) static char *strdup_rev(const char *s)
{ {
...@@ -22,13 +24,13 @@ static char *strdup_rev(const char *s) ...@@ -22,13 +24,13 @@ static char *strdup_rev(const char *s)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
unsigned int i, j, n; unsigned int i, j, n;
char **split; char **split, *str;
void *ctx; void *ctx;
char *strings[ARRAY_SIZE(substrings) * ARRAY_SIZE(substrings)]; char *strings[NUM_SUBSTRINGS * NUM_SUBSTRINGS];
n = 0; n = 0;
for (i = 0; i < ARRAY_SIZE(substrings); i++) { for (i = 0; i < NUM_SUBSTRINGS; i++) {
for (j = 0; j < ARRAY_SIZE(substrings); j++) { for (j = 0; j < NUM_SUBSTRINGS; j++) {
strings[n] = malloc(strlen(substrings[i]) strings[n] = malloc(strlen(substrings[i])
+ strlen(substrings[j]) + 1); + strlen(substrings[j]) + 1);
sprintf(strings[n++], "%s%s", sprintf(strings[n++], "%s%s",
...@@ -36,7 +38,7 @@ int main(int argc, char *argv[]) ...@@ -36,7 +38,7 @@ int main(int argc, char *argv[])
} }
} }
plan_tests(n * n * 5 + 16); plan_tests(n * n * 5 + 19);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) { for (j = 0; j < n; j++) {
unsigned int k, identical = 0; unsigned int k, identical = 0;
...@@ -105,5 +107,15 @@ int main(int argc, char *argv[]) ...@@ -105,5 +107,15 @@ int main(int argc, char *argv[])
ok1(talloc_parent(split) == ctx); ok1(talloc_parent(split) == ctx);
talloc_free(ctx); talloc_free(ctx);
str = strjoin(NULL, substrings, ", ");
ok1(streq(str, "far, bar, baz, b, ba, z, ar, "));
ctx = str;
str = strjoin(ctx, substrings, "");
ok1(streq(str, "farbarbazbbazar"));
ok1(talloc_parent(str) == ctx);
talloc_free(ctx);
return exit_status(); return exit_status();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment