It's short, and much clearer.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
char **path_split(const tal_t *ctx, const char *path)
{
bool empty = path && !path[0];
- char **ret = strsplit(ctx, path, PATH_SEP_STR, STR_NO_EMPTY);
+ char **ret = tal_strsplit(ctx, path, PATH_SEP_STR, STR_NO_EMPTY);
/* Handle the "/" case */
if (ret && !empty && !ret[0]) {
* textfile = grab_file(NULL, argv[1], NULL);
* if (!textfile)
* err(1, "Failed reading %s", argv[1]);
- * lines = strsplit(textfile, textfile, "\n", STR_EMPTY_OK);
+ * lines = tal_strsplit(textfile, textfile, "\n", STR_EMPTY_OK);
*
* // Join them back together with two linefeeds.
- * printf("%s", strjoin(textfile, lines, "\n\n", STR_TRAIL));
+ * printf("%s", tal_strjoin(textfile, lines, "\n\n", STR_TRAIL));
*
* // Free everything, just because we can.
* tal_free(textfile);
#include <ccan/tal/tal.h>
#include <ccan/take/take.h>
-char **strsplit(const tal_t *ctx,
- const char *string, const char *delims, enum strsplit flags)
+char **tal_strsplit(const tal_t *ctx,
+ const char *string, const char *delims, enum strsplit flags)
{
char **parts, *str;
size_t max = 64, num = 0;
return NULL;
}
-char *strjoin(const tal_t *ctx,
- char *strings[], const char *delim, enum strjoin flags)
+char *tal_strjoin(const tal_t *ctx,
+ char *strings[], const char *delim, enum strjoin flags)
{
unsigned int i;
char *ret = NULL;
goto out;
}
-bool strreg(const tal_t *ctx, const char *string, const char *regex, ...)
+bool tal_strreg(const tal_t *ctx, const char *string, const char *regex, ...)
{
size_t nmatch = 1 + strcount(regex, "(");
regmatch_t matches[nmatch];
};
/**
- * strsplit - Split string into an array of substrings
+ * tal_strsplit - Split string into an array of substrings
* @ctx: the context to tal from (often NULL).
* @string: the string to split (can be take()).
* @delims: delimiters where lines should be split (can be take()).
* unsigned int i, long_lines = 0;
*
* // Can only fail on out-of-memory.
- * lines = strsplit(NULL, string, "\n", STR_NO_EMPTY);
+ * lines = tal_strsplit(NULL, string, "\n", STR_NO_EMPTY);
* for (i = 0; lines[i] != NULL; i++)
* if (strlen(lines[i]) > 80)
* long_lines++;
* return long_lines;
* }
*/
-char **strsplit(const tal_t *ctx,
- const char *string, const char *delims, enum strsplit flags);
+char **tal_strsplit(const tal_t *ctx,
+ const char *string, const char *delims, enum strsplit flag);
enum strjoin {
STR_TRAIL,
};
/**
- * strjoin - Join an array of substrings into one long string
+ * tal_strjoin - Join an array of substrings into one long string
* @ctx: the context to tal from (often NULL).
* @strings: the NULL-terminated array of strings to join (can be take())
* @delim: the delimiter to insert between the strings (can be take())
* {
* char **lines, *ret;
*
- * lines = strsplit(NULL, string, "\n", STR_EMPTY_OK);
- * ret = strjoin(NULL, lines, "-- EOL\n", STR_TRAIL);
+ * lines = tal_strsplit(NULL, string, "\n", STR_EMPTY_OK);
+ * ret = tal_strjoin(NULL, lines, "-- EOL\n", STR_TRAIL);
* tal_free(lines);
* return ret;
* }
*/
-char *strjoin(const void *ctx, char *strings[], const char *delim,
- enum strjoin flags);
+char *tal_strjoin(const void *ctx, char *strings[], const char *delim,
+ enum strjoin flags);
/**
- * strreg - match and extract from a string via (extended) regular expressions.
+ * tal_strreg - match/extract from a string via (extended) regular expressions.
* @ctx: the context to tal from (often NULL)
* @string: the string to try to match (can be take())
* @regex: the regular expression to match (can be take())
* char *person, *input;
*
* // Join args and trim trailing space.
- * input = strjoin(NULL, argv+1, " ", STR_NO_TRAIL);
- * if (strreg(NULL, input, "[Mm]y (first )?name is ([A-Za-z ]+)",
- * NULL, &person))
+ * input = tal_strjoin(NULL, argv+1, " ", STR_NO_TRAIL);
+ * if (tal_strreg(NULL, input,
+ * "[Mm]y (first )?name is ([A-Za-z ]+)",
+ * NULL, &person))
* printf("Hello %s!\n", person);
* else
* printf("Hello there!\n");
* return 0;
* }
*/
-bool strreg(const void *ctx, const char *string, const char *regex, ...);
+bool tal_strreg(const void *ctx, const char *string, const char *regex, ...);
#endif /* CCAN_STR_TAL_H */
plan_tests(40);
/* Simple matching. */
- ok1(strreg(ctx, "hello world!", "hello") == true);
- ok1(strreg(ctx, "hello world!", "hi") == false);
+ ok1(tal_strreg(ctx, "hello world!", "hello") == true);
+ ok1(tal_strreg(ctx, "hello world!", "hi") == false);
/* No parentheses means we don't use any extra args. */
- ok1(strreg(ctx, "hello world!", "hello", invalid) == true);
- ok1(strreg(ctx, "hello world!", "hi", invalid) == false);
+ ok1(tal_strreg(ctx, "hello world!", "hello", invalid) == true);
+ ok1(tal_strreg(ctx, "hello world!", "hi", invalid) == false);
- ok1(strreg(ctx, "hello world!", "[a-z]+", invalid) == true);
- ok1(strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true);
+ ok1(tal_strreg(ctx, "hello world!", "[a-z]+", invalid) == true);
+ ok1(tal_strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true);
/* Found string */
ok1(streq(a, "hello"));
/* Allocated off ctx */
ok1(find_parent(a, ctx));
tal_free(a);
- ok1(strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)",
- &a, &b, invalid) == true);
+ ok1(tal_strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)",
+ &a, &b, invalid) == true);
ok1(streq(a, "hello"));
ok1(streq(b, "world"));
ok1(find_parent(a, ctx));
tal_free(b);
/* * after parentheses returns last match. */
- ok1(strreg(ctx, "hello world!", "([a-z])* ([a-z]+)",
- &a, &b, invalid) == true);
+ ok1(tal_strreg(ctx, "hello world!", "([a-z])* ([a-z]+)",
+ &a, &b, invalid) == true);
ok1(streq(a, "o"));
ok1(streq(b, "world"));
tal_free(a);
tal_free(b);
/* Nested parentheses are ordered by open brace. */
- ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
- &a, &b, invalid) == true);
+ ok1(tal_strreg(ctx, "hello world!", "(([a-z]*) world)",
+ &a, &b, invalid) == true);
ok1(streq(a, "hello world"));
ok1(streq(b, "hello"));
tal_free(a);
tal_free(b);
/* Nested parentheses are ordered by open brace. */
- ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
- &a, &b, invalid) == true);
+ ok1(tal_strreg(ctx, "hello world!", "(([a-z]*) world)",
+ &a, &b, invalid) == true);
ok1(streq(a, "hello world"));
ok1(streq(b, "hello"));
tal_free(a);
tal_free(b);
/* NULL means we're not interested. */
- ok1(strreg(ctx, "hello world!", "((hello|goodbye) world)",
- &a, NULL, invalid) == true);
+ ok1(tal_strreg(ctx, "hello world!", "((hello|goodbye) world)",
+ &a, NULL, invalid) == true);
ok1(streq(a, "hello world"));
tal_free(a);
ok1(!tal_first(ctx));
/* NULL arg with take means always fail. */
- ok1(strreg(ctx, take(NULL), "((hello|goodbye) world)",
- &b, NULL, invalid) == false);
+ ok1(tal_strreg(ctx, take(NULL), "((hello|goodbye) world)",
+ &b, NULL, invalid) == false);
/* Take string. */
a = tal_strdup(ctx, "hello world!");
- ok1(strreg(ctx, take(a), "([a-z]+)", &b, invalid) == true);
+ ok1(tal_strreg(ctx, take(a), "([a-z]+)", &b, invalid) == true);
ok1(streq(b, "hello"));
ok1(tal_parent(b) == ctx);
tal_free(b);
/* Take regex. */
a = tal_strdup(ctx, "([a-z]+)");
- ok1(strreg(ctx, "hello world!", take(a), &b, invalid) == true);
+ ok1(tal_strreg(ctx, "hello world!", take(a), &b, invalid) == true);
ok1(streq(b, "hello"));
ok1(tal_parent(b) == ctx);
tal_free(b);
/* Take both. */
a = tal_strdup(ctx, "([a-z]+)");
- ok1(strreg(ctx, take(tal_strdup(ctx, "hello world!")),
- take(a), &b, invalid) == true);
+ ok1(tal_strreg(ctx, take(tal_strdup(ctx, "hello world!")),
+ take(a), &b, invalid) == true);
ok1(streq(b, "hello"));
ok1(tal_parent(b) == ctx);
tal_free(b);
/* ... even if we fail to match. */
a = tal_strdup(ctx, "([a-z]+)");
- ok1(strreg(ctx, take(tal_strdup(ctx, "HELLO WORLD!")),
- take(a), &b, invalid) == false);
+ ok1(tal_strreg(ctx, take(tal_strdup(ctx, "HELLO WORLD!")),
+ take(a), &b, invalid) == false);
ok1(tal_first(ctx) == NULL);
tal_free(ctx);
void *ctx;
plan_tests(69);
- split = strsplit(NULL, "hello world", " ", STR_EMPTY_OK);
+ split = tal_strsplit(NULL, "hello world", " ", STR_EMPTY_OK);
ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], ""));
ok1(!strcmp(split[2], "world"));
ok1(tal_count(split) == 4);
tal_free(split);
- split = strsplit(NULL, "hello world", " ", STR_NO_EMPTY);
+ split = tal_strsplit(NULL, "hello world", " ", STR_NO_EMPTY);
ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], "world"));
ok1(split[2] == NULL);
ok1(tal_count(split) == 3);
tal_free(split);
- split = strsplit(NULL, " hello world", " ", STR_NO_EMPTY);
+ split = tal_strsplit(NULL, " hello world", " ", STR_NO_EMPTY);
ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], "world"));
ok1(split[2] == NULL);
ok1(tal_count(split) == 3);
tal_free(split);
- split = strsplit(NULL, "hello world", "o ", STR_EMPTY_OK);
+ split = tal_strsplit(NULL, "hello world", "o ", STR_EMPTY_OK);
ok1(!strcmp(split[0], "hell"));
ok1(!strcmp(split[1], ""));
ok1(!strcmp(split[2], ""));
ok1(tal_count(split) == 6);
ctx = split;
- split = strsplit(ctx, "hello world", "o ", STR_EMPTY_OK);
+ split = tal_strsplit(ctx, "hello world", "o ", STR_EMPTY_OK);
ok1(tal_parent(split) == ctx);
tal_free(ctx);
- str = strjoin(NULL, (char **)substrings, ", ", STR_TRAIL);
+ str = tal_strjoin(NULL, (char **)substrings, ", ", STR_TRAIL);
ok1(!strcmp(str, "far, bar, baz, b, ba, z, ar, "));
ctx = str;
- str = strjoin(ctx, (char **)substrings, "", STR_TRAIL);
+ str = tal_strjoin(ctx, (char **)substrings, "", STR_TRAIL);
ok1(!strcmp(str, "farbarbazbbazar"));
ok1(tal_parent(str) == ctx);
- str = strjoin(ctx, (char **)substrings, ", ", STR_NO_TRAIL);
+ str = tal_strjoin(ctx, (char **)substrings, ", ", STR_NO_TRAIL);
ok1(tal_parent(str) == ctx);
ok1(!strcmp(str, "far, bar, baz, b, ba, z, ar"));
- str = strjoin(ctx, (char **)substrings, "", STR_NO_TRAIL);
+ str = tal_strjoin(ctx, (char **)substrings, "", STR_NO_TRAIL);
ok1(!strcmp(str, "farbarbazbbazar"));
ok1(tal_parent(str) == ctx);
tal_free(ctx);
ctx = tal_strdup(NULL, "context");
/* Pass through NULLs from take. */
- ok1(strsplit(NULL, take(NULL), " ", STR_EMPTY_OK) == NULL);
- ok1(strsplit(NULL, "foo", take(NULL), STR_EMPTY_OK) == NULL);
+ ok1(tal_strsplit(NULL, take(NULL), " ", STR_EMPTY_OK) == NULL);
+ ok1(tal_strsplit(NULL, "foo", take(NULL), STR_EMPTY_OK) == NULL);
- /* strsplit take string. It reallocs it to same size, but
+ /* tal_strsplit take string. It reallocs it to same size, but
* that sometimes causes a move, so we can't directly check
* that split[0] == str. */
str = tal_strdup(ctx, "hello world");
ok1(tal_check(ctx, NULL));
ok1(tal_check(str, NULL));
- split = strsplit(ctx, take(str), " ", STR_EMPTY_OK);
+ split = tal_strsplit(ctx, take(str), " ", STR_EMPTY_OK);
ok1(tal_parent(split) == ctx);
ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], "world"));
/* Previous free should get rid of str */
ok1(!tal_first(ctx));
- /* strsplit take delims */
+ /* tal_strsplit take delims */
str = tal_strdup(ctx, " ");
- split = strsplit(ctx, "hello world", take(str), STR_EMPTY_OK);
+ split = tal_strsplit(ctx, "hello world", take(str), STR_EMPTY_OK);
ok1(tal_parent(split) == ctx);
ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], "world"));
/* str is gone... */
ok1(!tal_first(ctx));
- /* strsplit takes both. */
- split = strsplit(ctx, take(tal_strdup(NULL, "hello world")),
- take(tal_strdup(NULL, " ")), STR_EMPTY_OK);
+ /* tal_strsplit takes both. */
+ split = tal_strsplit(ctx, take(tal_strdup(NULL, "hello world")),
+ take(tal_strdup(NULL, " ")), STR_EMPTY_OK);
ok1(tal_parent(split) == ctx);
ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], "world"));
/* temp allocs are gone... */
ok1(!tal_first(ctx));
- /* strjoin passthrough taken NULLs OK. */
- ok1(strjoin(ctx, take(NULL), "", STR_TRAIL) == NULL);
- ok1(strjoin(ctx, take(NULL), "", STR_NO_TRAIL) == NULL);
- ok1(strjoin(ctx, split, take(NULL), STR_TRAIL) == NULL);
- ok1(strjoin(ctx, split, take(NULL), STR_NO_TRAIL) == NULL);
+ /* tal_strjoin passthrough taken NULLs OK. */
+ ok1(tal_strjoin(ctx, take(NULL), "", STR_TRAIL) == NULL);
+ ok1(tal_strjoin(ctx, take(NULL), "", STR_NO_TRAIL) == NULL);
+ ok1(tal_strjoin(ctx, split, take(NULL), STR_TRAIL) == NULL);
+ ok1(tal_strjoin(ctx, split, take(NULL), STR_NO_TRAIL) == NULL);
- /* strjoin take strings[] */
- split = strsplit(ctx, "hello world", " ", STR_EMPTY_OK);
- str = strjoin(ctx, take(split), " there ", STR_NO_TRAIL);
+ /* tal_strjoin take strings[] */
+ split = tal_strsplit(ctx, "hello world", " ", STR_EMPTY_OK);
+ str = tal_strjoin(ctx, take(split), " there ", STR_NO_TRAIL);
ok1(!strcmp(str, "hello there world"));
ok1(tal_parent(str) == ctx);
/* split is gone... */
tal_free(str);
ok1(!tal_first(ctx));
- /* strjoin take delim */
- split = strsplit(ctx, "hello world", " ", STR_EMPTY_OK);
- str = strjoin(ctx, split, take(tal_strdup(ctx, " there ")),
- STR_NO_TRAIL);
+ /* tal_strjoin take delim */
+ split = tal_strsplit(ctx, "hello world", " ", STR_EMPTY_OK);
+ str = tal_strjoin(ctx, split, take(tal_strdup(ctx, " there ")),
+ STR_NO_TRAIL);
ok1(!strcmp(str, "hello there world"));
ok1(tal_parent(str) == ctx);
tal_free(split);
tal_free(str);
ok1(!tal_first(ctx));
- /* strjoin take both. */
- str = strjoin(ctx, take(strsplit(ctx, "hello world", " ",
- STR_EMPTY_OK)),
- take(tal_strdup(ctx, " there ")), STR_NO_TRAIL);
+ /* tal_strjoin take both. */
+ str = tal_strjoin(ctx, take(tal_strsplit(ctx, "hello world", " ",
+ STR_EMPTY_OK)),
+ take(tal_strdup(ctx, " there ")), STR_NO_TRAIL);
ok1(!strcmp(str, "hello there world"));
ok1(tal_parent(str) == ctx);
/* tmp allocs are gone, str is only remainder. */