1 #include <ccan/tal/str/str.h>
2 #include <ccan/tal/str/str.c>
3 #include <ccan/tap/tap.h>
6 static bool find_parent(tal_t *child, tal_t *parent)
10 for (i = child; i; i = tal_parent(i))
17 int main(int argc, char *argv[])
19 void *ctx = tal_strdup(NULL, "toplevel");
21 /* If it accesses this, it will crash. */
22 char **invalid = (char **)1L;
25 /* Simple matching. */
26 ok1(tal_strreg(ctx, "hello world!", "hello") == true);
27 ok1(tal_strreg(ctx, "hello world!", "hi") == false);
29 /* No parentheses means we don't use any extra args. */
30 ok1(tal_strreg(ctx, "hello world!", "hello", invalid) == true);
31 ok1(tal_strreg(ctx, "hello world!", "hi", invalid) == false);
33 ok1(tal_strreg(ctx, "hello world!", "[a-z]+", invalid) == true);
34 ok1(tal_strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true);
36 ok1(streq(a, "hello"));
37 /* Allocated off ctx */
38 ok1(find_parent(a, ctx));
41 ok1(tal_strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)",
42 &a, &b, invalid) == true);
43 ok1(streq(a, "hello"));
44 ok1(streq(b, "world"));
45 ok1(find_parent(a, ctx));
46 ok1(find_parent(b, ctx));
50 /* * after parentheses returns last match. */
51 ok1(tal_strreg(ctx, "hello world!", "([a-z])* ([a-z]+)",
52 &a, &b, invalid) == true);
54 ok1(streq(b, "world"));
58 /* Nested parentheses are ordered by open brace. */
59 ok1(tal_strreg(ctx, "hello world!", "(([a-z]*) world)",
60 &a, &b, invalid) == true);
61 ok1(streq(a, "hello world"));
62 ok1(streq(b, "hello"));
66 /* Nested parentheses are ordered by open brace. */
67 ok1(tal_strreg(ctx, "hello world!", "(([a-z]*) world)",
68 &a, &b, invalid) == true);
69 ok1(streq(a, "hello world"));
70 ok1(streq(b, "hello"));
74 /* NULL means we're not interested. */
75 ok1(tal_strreg(ctx, "hello world!", "((hello|goodbye) world)",
76 &a, NULL, invalid) == true);
77 ok1(streq(a, "hello world"));
81 ok1(no_children(ctx));
83 /* NULL arg with take means always fail. */
84 ok1(tal_strreg(ctx, take(NULL), "((hello|goodbye) world)",
85 &b, NULL, invalid) == false);
88 a = tal_strdup(ctx, "hello world!");
89 ok1(tal_strreg(ctx, take(a), "([a-z]+)", &b, invalid) == true);
90 ok1(streq(b, "hello"));
91 ok1(tal_parent(b) == ctx);
93 ok1(no_children(ctx));
96 a = tal_strdup(ctx, "([a-z]+)");
97 ok1(tal_strreg(ctx, "hello world!", take(a), &b, invalid) == true);
98 ok1(streq(b, "hello"));
99 ok1(tal_parent(b) == ctx);
101 ok1(no_children(ctx));
104 a = tal_strdup(ctx, "([a-z]+)");
105 ok1(tal_strreg(ctx, take(tal_strdup(ctx, "hello world!")),
106 take(a), &b, invalid) == true);
107 ok1(streq(b, "hello"));
108 ok1(tal_parent(b) == ctx);
110 ok1(no_children(ctx));
112 /* ... even if we fail to match. */
113 a = tal_strdup(ctx, "([a-z]+)");
114 ok1(tal_strreg(ctx, take(tal_strdup(ctx, "HELLO WORLD!")),
115 take(a), &b, invalid) == false);
116 ok1(no_children(ctx));
119 /* Don't get fooled by \(! */
120 ok1(tal_strreg(ctx, "(hello) (world)!", "\\([a-z]*\\) \\([a-z]+\\)",
123 return exit_status();