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;
}
#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>
--- /dev/null
+/* 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)
{
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);
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)
{
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)",
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]+)");
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]+)");
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)
{
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());
#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]))
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, " ");
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")),
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);
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);
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", " ",
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();