Commit f103f73f authored by Rusty Russell's avatar Rusty Russell

tal/str: accept take() for arguments.

Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent d873aaec
...@@ -45,6 +45,7 @@ int main(int argc, char *argv[]) ...@@ -45,6 +45,7 @@ int main(int argc, char *argv[])
if (strcmp(argv[1], "depends") == 0) { if (strcmp(argv[1], "depends") == 0) {
printf("ccan/str\n"); printf("ccan/str\n");
printf("ccan/tal\n"); printf("ccan/tal\n");
printf("ccan/take\n");
return 0; return 0;
} }
......
...@@ -9,98 +9,149 @@ ...@@ -9,98 +9,149 @@
#include <regex.h> #include <regex.h>
#include <stdarg.h> #include <stdarg.h>
#include <unistd.h> #include <unistd.h>
#include <ccan/tal/tal.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <ccan/tal/tal.h>
#include <ccan/take/take.h>
char **strsplit(const void *ctx, const char *string, const char *delims, char **strsplit(const tal_t *ctx,
enum strsplit flags) const char *string, const char *delims, enum strsplit flags)
{ {
char **lines = NULL; char **parts, *str;
size_t max = 64, num = 0; size_t max = 64, num = 0;
lines = tal_arr(ctx, char *, max+1); parts = tal_arr(ctx, char *, max + 1);
if (unlikely(!parts)) {
if (taken(string))
tal_free(string);
if (taken(delims))
tal_free(delims);
return NULL;
}
str = tal_strdup(parts, string);
if (unlikely(!str))
goto fail;
if (unlikely(!delims) && is_taken(delims))
goto fail;
if (flags == STR_NO_EMPTY) if (flags == STR_NO_EMPTY)
string += strspn(string, delims); str += strspn(str, delims);
while (*string != '\0') { while (*str != '\0') {
size_t len = strcspn(string, delims), dlen; size_t len = strcspn(str, delims), dlen;
lines[num] = tal_arr(lines, char, len + 1); parts[num] = str;
memcpy(lines[num], string, len); dlen = strspn(str + len, delims);
lines[num][len] = '\0'; parts[num][len] = '\0';
string += len;
dlen = strspn(string, delims);
if (flags == STR_EMPTY_OK && dlen) if (flags == STR_EMPTY_OK && dlen)
dlen = 1; dlen = 1;
string += dlen; str += len + dlen;
if (++num == max) if (++num == max && !tal_resize(&parts, max*=2 + 1))
tal_resize(&lines, max*=2 + 1); goto fail;
} }
lines[num] = NULL; parts[num] = NULL;
return lines; if (taken(delims))
tal_free(delims);
return parts;
fail:
tal_free(parts);
if (taken(delims))
tal_free(delims);
return NULL;
} }
char *strjoin(const void *ctx, char *strings[], const char *delim, char *strjoin(const tal_t *ctx,
enum strjoin flags) char *strings[], const char *delim, enum strjoin flags)
{ {
unsigned int i; unsigned int i;
char *ret = tal_strdup(ctx, ""); char *ret = NULL;
size_t totlen = 0, dlen = strlen(delim); size_t totlen = 0, dlen;
if (unlikely(!strings) && is_taken(strings))
goto fail;
if (unlikely(!delim) && is_taken(delim))
goto fail;
dlen = strlen(delim);
ret = tal_arr(ctx, char, dlen*2+1);
if (!ret)
goto fail;
ret[0] = '\0';
for (i = 0; strings[i]; i++) { for (i = 0; strings[i]; i++) {
size_t len = strlen(strings[i]); size_t len = strlen(strings[i]);
if (flags == STR_NO_TRAIL && !strings[i+1]) if (flags == STR_NO_TRAIL && !strings[i+1])
dlen = 0; dlen = 0;
tal_resize(&ret, totlen + len + dlen + 1); if (!tal_resize(&ret, totlen + len + dlen + 1))
goto fail;
memcpy(ret + totlen, strings[i], len); memcpy(ret + totlen, strings[i], len);
totlen += len; totlen += len;
memcpy(ret + totlen, delim, dlen); memcpy(ret + totlen, delim, dlen);
totlen += dlen; totlen += dlen;
} }
ret[totlen] = '\0'; ret[totlen] = '\0';
out:
if (taken(strings))
tal_free(strings);
if (taken(delim))
tal_free(delim);
return ret; return ret;
fail:
ret = tal_free(ret);
goto out;
} }
bool strreg(const void *ctx, const char *string, const char *regex, ...) bool strreg(const tal_t *ctx, const char *string, const char *regex, ...)
{ {
size_t nmatch = 1 + strcount(regex, "("); size_t nmatch = 1 + strcount(regex, "(");
regmatch_t *matches = tal_arr(ctx, regmatch_t, nmatch); regmatch_t matches[nmatch];
regex_t r; regex_t r;
bool ret; bool ret = false;
unsigned int i;
if (!matches || regcomp(&r, regex, REG_EXTENDED) != 0) va_list ap;
return false;
if (unlikely(!regex) && is_taken(regex))
if (regexec(&r, string, nmatch, matches, 0) == 0) { goto fail_no_re;
unsigned int i;
va_list ap; if (regcomp(&r, regex, REG_EXTENDED) != 0)
goto fail_no_re;
ret = true;
va_start(ap, regex); if (unlikely(!string) && is_taken(string))
for (i = 1; i < nmatch; i++) { goto fail;
char **arg;
arg = va_arg(ap, char **); if (regexec(&r, string, nmatch, matches, 0) != 0)
if (arg) { goto fail;
/* eg. ([a-z])? can give "no match". */
if (matches[i].rm_so == -1) ret = true;
*arg = NULL; va_start(ap, regex);
else { for (i = 1; i < nmatch; i++) {
*arg = tal_strndup(ctx, char **arg = va_arg(ap, char **);
string + matches[i].rm_so, if (arg) {
matches[i].rm_eo /* eg. ([a-z])? can give "no match". */
- matches[i].rm_so); if (matches[i].rm_so == -1)
if (!*arg) { *arg = NULL;
ret = false; else {
break; *arg = tal_strndup(ctx,
} string + matches[i].rm_so,
matches[i].rm_eo
- matches[i].rm_so);
/* FIXME: If we fail, we set some and leak! */
if (!*arg) {
ret = false;
break;
} }
} }
} }
va_end(ap); }
} else va_end(ap);
ret = false; fail:
tal_free(matches);
regfree(&r); regfree(&r);
fail_no_re:
if (taken(regex))
tal_free(regex);
if (taken(string))
tal_free(string);
return ret; return ret;
} }
...@@ -13,16 +13,18 @@ enum strsplit { ...@@ -13,16 +13,18 @@ enum strsplit {
/** /**
* strsplit - Split string into an array of substrings * strsplit - Split string into an array of substrings
* @ctx: the parent to tal from (often NULL) * @ctx: the context to tal from (often NULL).
* @string: the string to split * @string: the string to split (can be take()).
* @delims: delimiters where lines should be split. * @delims: delimiters where lines should be split (can be take()).
* @flags: whether to include empty substrings. * @flags: whether to include empty substrings.
* *
* This function splits a single string into multiple strings. The * This function splits a single string into multiple strings.
* original string is untouched: an array is allocated (using tal) *
* pointing to copies of each substring. Multiple delimiters result * If @string is take(), the returned array will point into the
* in empty substrings. By definition, no delimiters will appear in * mangled @string.
* the substrings. *
* Multiple delimiters result in empty substrings. By definition, no
* delimiters will appear in the substrings.
* *
* The final char * in the array will be NULL. * The final char * in the array will be NULL.
* *
...@@ -43,8 +45,8 @@ enum strsplit { ...@@ -43,8 +45,8 @@ enum strsplit {
* return long_lines; * return long_lines;
* } * }
*/ */
char **strsplit(const void *ctx, const char *string, const char *delims, char **strsplit(const tal_t *ctx,
enum strsplit flags); const char *string, const char *delims, enum strsplit flags);
enum strjoin { enum strjoin {
STR_TRAIL, STR_TRAIL,
...@@ -53,9 +55,9 @@ enum strjoin { ...@@ -53,9 +55,9 @@ enum strjoin {
/** /**
* strjoin - Join an array of substrings into one long string * strjoin - Join an array of substrings into one long string
* @ctx: the context to tal from (often NULL) * @ctx: the context to tal from (often NULL).
* @strings: the NULL-terminated array of strings to join * @strings: the NULL-terminated array of strings to join (can be take())
* @delim: the delimiter to insert between the strings * @delim: the delimiter to insert between the strings (can be take())
* @flags: whether to add a delimieter to the end * @flags: whether to add a delimieter to the end
* *
* This function joins an array of strings into a single string. The * This function joins an array of strings into a single string. The
...@@ -80,8 +82,8 @@ char *strjoin(const void *ctx, char *strings[], const char *delim, ...@@ -80,8 +82,8 @@ char *strjoin(const void *ctx, char *strings[], const char *delim,
/** /**
* strreg - match and extract from a string via (extended) regular expressions. * strreg - match and extract from a string via (extended) regular expressions.
* @ctx: the context to tal from (often NULL) * @ctx: the context to tal from (often NULL)
* @string: the string to try to match. * @string: the string to try to match (can be take())
* @regex: the regular expression to match. * @regex: the regular expression to match (can be take())
* ...: pointers to strings to allocate for subexpressions. * ...: pointers to strings to allocate for subexpressions.
* *
* Returns true if we matched, in which case any parenthesized * Returns true if we matched, in which case any parenthesized
......
...@@ -2,16 +2,6 @@ ...@@ -2,16 +2,6 @@
#include <ccan/tal/str/str.c> #include <ccan/tal/str/str.c>
#include <ccan/tap/tap.h> #include <ccan/tap/tap.h>
static unsigned int tal_total_blocks(tal_t *ctx)
{
unsigned int num = 1;
tal_t *i;
for (i = tal_first(ctx); i; i = tal_next(ctx, i))
num++;
return num;
}
static bool find_parent(tal_t *child, tal_t *parent) static bool find_parent(tal_t *child, tal_t *parent)
{ {
tal_t *i; tal_t *i;
...@@ -26,12 +16,11 @@ static bool find_parent(tal_t *child, tal_t *parent) ...@@ -26,12 +16,11 @@ static bool find_parent(tal_t *child, tal_t *parent)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
void *ctx = tal_strdup(NULL, "toplevel"); void *ctx = tal_strdup(NULL, "toplevel");
unsigned int top_blocks = tal_total_blocks(ctx);
char *a, *b; char *a, *b;
/* If it accesses this, it will crash. */ /* If it accesses this, it will crash. */
char **invalid = (char **)1L; char **invalid = (char **)1L;
plan_tests(25); plan_tests(40);
/* Simple matching. */ /* Simple matching. */
ok1(strreg(ctx, "hello world!", "hello") == true); ok1(strreg(ctx, "hello world!", "hello") == true);
ok1(strreg(ctx, "hello world!", "hi") == false); ok1(strreg(ctx, "hello world!", "hi") == false);
...@@ -88,7 +77,42 @@ int main(int argc, char *argv[]) ...@@ -88,7 +77,42 @@ int main(int argc, char *argv[])
tal_free(a); tal_free(a);
/* No leaks! */ /* No leaks! */
ok1(tal_total_blocks(ctx) == top_blocks); ok1(!tal_first(ctx));
/* NULL arg with take means always fail. */
ok1(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(streq(b, "hello"));
ok1(tal_parent(b) == ctx);
tal_free(b);
ok1(tal_first(ctx) == NULL);
/* Take regex. */
a = tal_strdup(ctx, "([a-z]+)");
ok1(strreg(ctx, "hello world!", take(a), &b, invalid) == true);
ok1(streq(b, "hello"));
ok1(tal_parent(b) == ctx);
tal_free(b);
ok1(tal_first(ctx) == NULL);
/* Take both. */
a = tal_strdup(ctx, "([a-z]+)");
ok1(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);
ok1(tal_first(ctx) == NULL);
/* ... 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_first(ctx) == NULL);
tal_free(ctx); tal_free(ctx);
return exit_status(); return exit_status();
......
...@@ -14,7 +14,7 @@ int main(int argc, char *argv[]) ...@@ -14,7 +14,7 @@ int main(int argc, char *argv[])
char **split, *str; char **split, *str;
void *ctx; void *ctx;
plan_tests(24); plan_tests(65);
split = strsplit(NULL, "hello world", " ", STR_EMPTY_OK); split = strsplit(NULL, "hello world", " ", STR_EMPTY_OK);
ok1(!strcmp(split[0], "hello")); ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], "")); ok1(!strcmp(split[1], ""));
...@@ -61,5 +61,93 @@ int main(int argc, char *argv[]) ...@@ -61,5 +61,93 @@ int main(int argc, char *argv[])
ok1(tal_parent(str) == ctx); ok1(tal_parent(str) == ctx);
tal_free(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);
/* 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);
ok1(tal_parent(split) == ctx);
ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], "world"));
ok1(split[2] == NULL);
ok1(tal_check(split, NULL));
ok1(tal_check(ctx, NULL));
tal_free(split);
/* Previous free should get rid of str */
ok1(!tal_first(ctx));
/* strsplit take delims */
str = tal_strdup(ctx, " ");
split = strsplit(ctx, "hello world", take(str), STR_EMPTY_OK);
ok1(tal_parent(split) == ctx);
ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], "world"));
ok1(split[2] == NULL);
ok1(tal_check(split, NULL));
ok1(tal_check(ctx, NULL));
tal_free(split);
/* 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);
ok1(tal_parent(split) == ctx);
ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], "world"));
ok1(split[2] == NULL);
ok1(tal_check(split, NULL));
ok1(tal_check(ctx, NULL));
tal_free(split);
/* 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);
/* strjoin take strings[] */
split = strsplit(ctx, "hello world", " ", STR_EMPTY_OK);
str = strjoin(ctx, take(split), " there ", STR_NO_TRAIL);
ok1(!strcmp(str, "hello there world"));
ok1(tal_parent(str) == ctx);
/* split is gone... */
ok1(tal_first(ctx) == str && !tal_next(ctx, str));
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);
ok1(!strcmp(str, "hello there world"));
ok1(tal_parent(str) == ctx);
tal_free(split);
/* tmp alloc is gone, str is only remainder. */
ok1(tal_first(ctx) == str && !tal_next(ctx, str));
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);
ok1(!strcmp(str, "hello there world"));
ok1(tal_parent(str) == ctx);
/* tmp allocs are gone, str is only remainder. */
ok1(tal_first(ctx) == str && !tal_next(ctx, str));
tal_free(str);
ok1(!tal_first(ctx));
tal_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