Commit 15ed4f45 authored by Rusty Russell's avatar Rusty Russell

tal/str: use tal_ prefix.

It's short, and much clearer.
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 737dcc0f
......@@ -525,7 +525,7 @@ bool path_is_dir(const char *path)
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]) {
......
......@@ -24,10 +24,10 @@
* 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);
......
......@@ -13,8 +13,8 @@
#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;
......@@ -65,8 +65,8 @@ fail:
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;
......@@ -108,7 +108,7 @@ fail:
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];
......
......@@ -12,7 +12,7 @@ enum strsplit {
};
/**
* 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()).
......@@ -38,7 +38,7 @@ enum strsplit {
* 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++;
......@@ -46,8 +46,8 @@ enum strsplit {
* 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,
......@@ -55,7 +55,7 @@ enum strjoin {
};
/**
* 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())
......@@ -71,17 +71,17 @@ enum strjoin {
* {
* 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())
......@@ -109,14 +109,15 @@ char *strjoin(const void *ctx, char *strings[], const char *delim,
* 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 */
......@@ -22,23 +22,23 @@ int main(int argc, char *argv[])
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));
......@@ -47,32 +47,32 @@ int main(int argc, char *argv[])
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);
......@@ -80,12 +80,12 @@ int main(int argc, char *argv[])
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);
......@@ -93,7 +93,7 @@ int main(int argc, char *argv[])
/* 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);
......@@ -101,8 +101,8 @@ int main(int argc, char *argv[])
/* 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);
......@@ -110,8 +110,8 @@ int main(int argc, char *argv[])
/* ... 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);
......
......@@ -15,7 +15,7 @@ int main(int argc, char *argv[])
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"));
......@@ -23,21 +23,21 @@ int main(int argc, char *argv[])
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], ""));
......@@ -47,36 +47,36 @@ int main(int argc, char *argv[])
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"));
......@@ -87,9 +87,9 @@ int main(int argc, char *argv[])
/* 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"));
......@@ -100,9 +100,9 @@ int main(int argc, char *argv[])
/* 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"));
......@@ -113,15 +113,15 @@ int main(int argc, char *argv[])
/* 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... */
......@@ -129,10 +129,10 @@ int main(int argc, char *argv[])
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);
......@@ -141,10 +141,10 @@ int main(int argc, char *argv[])
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. */
......
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