Commit 97d99004 authored by Rusty Russell's avatar Rusty Russell

tal/str: use tal/talloc backend #ifdef TAL_USE_TALLOC.

Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 3d5a1a32
......@@ -44,7 +44,11 @@ int main(int argc, char *argv[])
if (strcmp(argv[1], "depends") == 0) {
printf("ccan/str\n");
#ifdef TAL_USE_TALLOC
printf("ccan/tal/talloc\n");
#else
printf("ccan/tal\n");
#endif
printf("ccan/take\n");
return 0;
}
......
......@@ -11,7 +11,6 @@
#include <unistd.h>
#include <stdio.h>
#include <ccan/str/str.h>
#include <ccan/tal/tal.h>
#include <ccan/take/take.h>
char *tal_strdup(const tal_t *ctx, const char *p)
......
/* Licensed under BSD-MIT - see LICENSE file for details */
#ifndef CCAN_STR_TAL_H
#define CCAN_STR_TAL_H
#ifdef TAL_USE_TALLOC
#include <ccan/tal/talloc/talloc.h>
#else
#include <ccan/tal/tal.h>
#include <ccan/tal/tal.h>
#endif
#include <string.h>
#include <stdbool.h>
......
/* tal/talloc can't implement tal_first/tal_next. */
#ifdef TAL_USE_TALLOC
static inline bool no_children(const void *ctx)
{
return talloc_total_blocks(ctx) == 1;
}
static inline bool single_child(const void *ctx, const void *child)
{
return talloc_total_blocks(ctx) == 2 && tal_parent(child) == ctx;
}
#else
static inline bool no_children(const void *ctx)
{
return !tal_first(ctx);
}
static inline bool single_child(const void *ctx, const void *child)
{
return tal_first(ctx) == child && !tal_next(ctx, child);
}
#endif
#include <ccan/tal/str/str.h>
#include <ccan/tal/str/str.c>
#include <ccan/tap/tap.h>
#include "helper.h"
int main(void)
{
......@@ -21,7 +22,11 @@ int main(void)
ok1(tal_parent(c) == parent);
tal_free(c);
#ifdef TAL_USE_TALLOC
c = tal_talloc_typechk_(parent, char *);
#else
c = tal_typechk_(parent, char *);
#endif
c = tal_dup(parent, char, "hello", 6, 0);
ok1(strcmp(c, "hello") == 0);
ok1(strcmp(tal_name(c), "char[]") == 0);
......@@ -49,26 +54,26 @@ int main(void)
c = tal_strcat(parent, take(c), " again");
ok1(strcmp(c, "hello there again") == 0);
ok1(tal_parent(c) == parent);
ok1(tal_first(parent) == c && !tal_next(parent, c));
ok1(single_child(parent, c));
c = tal_strcat(parent, "And ", take(c));
ok1(strcmp(c, "And hello there again") == 0);
ok1(tal_parent(c) == parent);
ok1(tal_first(parent) == c && !tal_next(parent, c));
ok1(single_child(parent, c));
/* NULL pass through works... */
c = tal_strcat(parent, take(NULL), take(c));
ok1(!c);
ok1(!tal_first(parent));
ok1(no_children(parent));
c = tal_strcat(parent, take(tal_strdup(parent, "hi")),
take(NULL));
ok1(!c);
ok1(!tal_first(parent));
ok1(no_children(parent));
c = tal_strcat(parent, take(NULL), take(NULL));
ok1(!c);
ok1(!tal_first(parent));
ok1(no_children(parent));
/* Appending formatted strings. */
c = tal_strdup(parent, "hi");
......
#include <ccan/tal/str/str.h>
#include <ccan/tal/str/str.c>
#include <ccan/tap/tap.h>
#include "helper.h"
static bool find_parent(tal_t *child, tal_t *parent)
{
......@@ -77,7 +78,7 @@ int main(int argc, char *argv[])
tal_free(a);
/* No leaks! */
ok1(!tal_first(ctx));
ok1(no_children(ctx));
/* NULL arg with take means always fail. */
ok1(tal_strreg(ctx, take(NULL), "((hello|goodbye) world)",
......@@ -89,7 +90,7 @@ int main(int argc, char *argv[])
ok1(streq(b, "hello"));
ok1(tal_parent(b) == ctx);
tal_free(b);
ok1(tal_first(ctx) == NULL);
ok1(no_children(ctx));
/* Take regex. */
a = tal_strdup(ctx, "([a-z]+)");
......@@ -97,7 +98,7 @@ int main(int argc, char *argv[])
ok1(streq(b, "hello"));
ok1(tal_parent(b) == ctx);
tal_free(b);
ok1(tal_first(ctx) == NULL);
ok1(no_children(ctx));
/* Take both. */
a = tal_strdup(ctx, "([a-z]+)");
......@@ -106,13 +107,13 @@ int main(int argc, char *argv[])
ok1(streq(b, "hello"));
ok1(tal_parent(b) == ctx);
tal_free(b);
ok1(tal_first(ctx) == NULL);
ok1(no_children(ctx));
/* ... even if we fail to match. */
a = tal_strdup(ctx, "([a-z]+)");
ok1(tal_strreg(ctx, take(tal_strdup(ctx, "HELLO WORLD!")),
take(a), &b, invalid) == false);
ok1(tal_first(ctx) == NULL);
ok1(no_children(ctx));
tal_free(ctx);
return exit_status();
......
#include <ccan/tal/str/str.h>
#include <ccan/tal/str/str.c>
#include <ccan/tap/tap.h>
#include "helper.h"
int main(void)
{
......@@ -32,7 +33,7 @@ int main(void)
ok1(tal_parent(c) == parent);
/* No leftover allocations. */
tal_free(c);
ok1(tal_first(parent) == NULL);
ok1(no_children(parent));
tal_free(parent);
ok1(!taken_any());
......
......@@ -3,6 +3,7 @@
#include <stdio.h>
#include <ccan/tal/str/str.c>
#include <ccan/tap/tap.h>
#include "helper.h"
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
......@@ -85,7 +86,7 @@ int main(int argc, char *argv[])
ok1(tal_check(ctx, NULL));
tal_free(split);
/* Previous free should get rid of str */
ok1(!tal_first(ctx));
ok1(no_children(ctx));
/* tal_strsplit take delims */
str = tal_strdup(ctx, " ");
......@@ -98,7 +99,7 @@ int main(int argc, char *argv[])
ok1(tal_check(ctx, NULL));
tal_free(split);
/* str is gone... */
ok1(!tal_first(ctx));
ok1(no_children(ctx));
/* tal_strsplit takes both. */
split = tal_strsplit(ctx, take(tal_strdup(NULL, "hello world")),
......@@ -111,7 +112,7 @@ int main(int argc, char *argv[])
ok1(tal_check(ctx, NULL));
tal_free(split);
/* temp allocs are gone... */
ok1(!tal_first(ctx));
ok1(no_children(ctx));
/* tal_strjoin passthrough taken NULLs OK. */
ok1(tal_strjoin(ctx, take(NULL), "", STR_TRAIL) == NULL);
......@@ -125,9 +126,9 @@ int main(int argc, char *argv[])
ok1(!strcmp(str, "hello there world"));
ok1(tal_parent(str) == ctx);
/* split is gone... */
ok1(tal_first(ctx) == str && !tal_next(ctx, str));
ok1(single_child(ctx, str));
tal_free(str);
ok1(!tal_first(ctx));
ok1(no_children(ctx));
/* tal_strjoin take delim */
split = tal_strsplit(ctx, "hello world", " ", STR_EMPTY_OK);
......@@ -137,9 +138,9 @@ int main(int argc, char *argv[])
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));
ok1(single_child(ctx, str));
tal_free(str);
ok1(!tal_first(ctx));
ok1(no_children(ctx));
/* tal_strjoin take both. */
str = tal_strjoin(ctx, take(tal_strsplit(ctx, "hello world", " ",
......@@ -148,9 +149,9 @@ int main(int argc, char *argv[])
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));
ok1(single_child(ctx, str));
tal_free(str);
ok1(!tal_first(ctx));
ok1(no_children(ctx));
tal_free(ctx);
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