1 #include <ccan/tal/str/str.h>
2 #include <ccan/tal/str/str.c>
3 #include <ccan/tap/tap.h>
5 static unsigned int tal_total_blocks(tal_t *ctx)
10 for (i = tal_first(ctx); i; i = tal_next(ctx, i))
15 static bool find_parent(tal_t *child, tal_t *parent)
19 for (i = child; i; i = tal_parent(i))
26 int main(int argc, char *argv[])
28 void *ctx = tal_strdup(NULL, "toplevel");
29 unsigned int top_blocks = tal_total_blocks(ctx);
31 /* If it accesses this, it will crash. */
32 char **invalid = (char **)1L;
35 /* Simple matching. */
36 ok1(strreg(ctx, "hello world!", "hello") == true);
37 ok1(strreg(ctx, "hello world!", "hi") == false);
39 /* No parentheses means we don't use any extra args. */
40 ok1(strreg(ctx, "hello world!", "hello", invalid) == true);
41 ok1(strreg(ctx, "hello world!", "hi", invalid) == false);
43 ok1(strreg(ctx, "hello world!", "[a-z]+", invalid) == true);
44 ok1(strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true);
46 ok1(streq(a, "hello"));
47 /* Allocated off ctx */
48 ok1(find_parent(a, ctx));
51 ok1(strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)",
52 &a, &b, invalid) == true);
53 ok1(streq(a, "hello"));
54 ok1(streq(b, "world"));
55 ok1(find_parent(a, ctx));
56 ok1(find_parent(b, ctx));
60 /* * after parentheses returns last match. */
61 ok1(strreg(ctx, "hello world!", "([a-z])* ([a-z]+)",
62 &a, &b, invalid) == true);
64 ok1(streq(b, "world"));
68 /* Nested parentheses are ordered by open brace. */
69 ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
70 &a, &b, invalid) == true);
71 ok1(streq(a, "hello world"));
72 ok1(streq(b, "hello"));
76 /* Nested parentheses are ordered by open brace. */
77 ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
78 &a, &b, invalid) == true);
79 ok1(streq(a, "hello world"));
80 ok1(streq(b, "hello"));
84 /* NULL means we're not interested. */
85 ok1(strreg(ctx, "hello world!", "((hello|goodbye) world)",
86 &a, NULL, invalid) == true);
87 ok1(streq(a, "hello world"));
91 ok1(tal_total_blocks(ctx) == top_blocks);