1 #include <ccan/tal/str/str.h>
2 #include <ccan/tal/str/str.c>
3 #include <ccan/tap/tap.h>
5 static bool find_parent(tal_t *child, tal_t *parent)
9 for (i = child; i; i = tal_parent(i))
16 int main(int argc, char *argv[])
18 void *ctx = tal_strdup(NULL, "toplevel");
20 /* If it accesses this, it will crash. */
21 char **invalid = (char **)1L;
24 /* Simple matching. */
25 ok1(strreg(ctx, "hello world!", "hello") == true);
26 ok1(strreg(ctx, "hello world!", "hi") == false);
28 /* No parentheses means we don't use any extra args. */
29 ok1(strreg(ctx, "hello world!", "hello", invalid) == true);
30 ok1(strreg(ctx, "hello world!", "hi", invalid) == false);
32 ok1(strreg(ctx, "hello world!", "[a-z]+", invalid) == true);
33 ok1(strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true);
35 ok1(streq(a, "hello"));
36 /* Allocated off ctx */
37 ok1(find_parent(a, ctx));
40 ok1(strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)",
41 &a, &b, invalid) == true);
42 ok1(streq(a, "hello"));
43 ok1(streq(b, "world"));
44 ok1(find_parent(a, ctx));
45 ok1(find_parent(b, ctx));
49 /* * after parentheses returns last match. */
50 ok1(strreg(ctx, "hello world!", "([a-z])* ([a-z]+)",
51 &a, &b, invalid) == true);
53 ok1(streq(b, "world"));
57 /* Nested parentheses are ordered by open brace. */
58 ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
59 &a, &b, invalid) == true);
60 ok1(streq(a, "hello world"));
61 ok1(streq(b, "hello"));
65 /* Nested parentheses are ordered by open brace. */
66 ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
67 &a, &b, invalid) == true);
68 ok1(streq(a, "hello world"));
69 ok1(streq(b, "hello"));
73 /* NULL means we're not interested. */
74 ok1(strreg(ctx, "hello world!", "((hello|goodbye) world)",
75 &a, NULL, invalid) == true);
76 ok1(streq(a, "hello world"));
82 /* NULL arg with take means always fail. */
83 ok1(strreg(ctx, take(NULL), "((hello|goodbye) world)",
84 &b, NULL, invalid) == false);
87 a = tal_strdup(ctx, "hello world!");
88 ok1(strreg(ctx, take(a), "([a-z]+)", &b, invalid) == true);
89 ok1(streq(b, "hello"));
90 ok1(tal_parent(b) == ctx);
92 ok1(tal_first(ctx) == NULL);
95 a = tal_strdup(ctx, "([a-z]+)");
96 ok1(strreg(ctx, "hello world!", take(a), &b, invalid) == true);
97 ok1(streq(b, "hello"));
98 ok1(tal_parent(b) == ctx);
100 ok1(tal_first(ctx) == NULL);
103 a = tal_strdup(ctx, "([a-z]+)");
104 ok1(strreg(ctx, take(tal_strdup(ctx, "hello world!")),
105 take(a), &b, invalid) == true);
106 ok1(streq(b, "hello"));
107 ok1(tal_parent(b) == ctx);
109 ok1(tal_first(ctx) == NULL);
111 /* ... even if we fail to match. */
112 a = tal_strdup(ctx, "([a-z]+)");
113 ok1(strreg(ctx, take(tal_strdup(ctx, "HELLO WORLD!")),
114 take(a), &b, invalid) == false);
115 ok1(tal_first(ctx) == NULL);
118 return exit_status();