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))
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));
39 ok1(tal_count(a) == strlen(a) + 1);
42 ok1(tal_strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)",
43 &a, &b, invalid) == true);
44 ok1(streq(a, "hello"));
45 ok1(streq(b, "world"));
46 ok1(tal_count(a) == strlen(a) + 1);
47 ok1(tal_count(b) == strlen(b) + 1);
48 ok1(find_parent(a, ctx));
49 ok1(find_parent(b, ctx));
53 /* * after parentheses returns last match. */
54 ok1(tal_strreg(ctx, "hello world!", "([a-z])* ([a-z]+)",
55 &a, &b, invalid) == true);
57 ok1(streq(b, "world"));
58 ok1(tal_count(a) == strlen(a) + 1);
59 ok1(tal_count(b) == strlen(b) + 1);
63 /* Nested parentheses are ordered by open brace. */
64 ok1(tal_strreg(ctx, "hello world!", "(([a-z]*) world)",
65 &a, &b, invalid) == true);
66 ok1(streq(a, "hello world"));
67 ok1(streq(b, "hello"));
68 ok1(tal_count(a) == strlen(a) + 1);
69 ok1(tal_count(b) == strlen(b) + 1);
73 /* Nested parentheses are ordered by open brace. */
74 ok1(tal_strreg(ctx, "hello world!", "(([a-z]*) world)",
75 &a, &b, invalid) == true);
76 ok1(streq(a, "hello world"));
77 ok1(streq(b, "hello"));
78 ok1(tal_count(a) == strlen(a) + 1);
79 ok1(tal_count(b) == strlen(b) + 1);
83 /* NULL means we're not interested. */
84 ok1(tal_strreg(ctx, "hello world!", "((hello|goodbye) world)",
85 &a, NULL, invalid) == true);
86 ok1(streq(a, "hello world"));
87 ok1(tal_count(a) == strlen(a) + 1);
91 ok1(no_children(ctx));
93 /* NULL arg with take means always fail. */
94 ok1(tal_strreg(ctx, take(NULL), "((hello|goodbye) world)",
95 &b, NULL, invalid) == false);
98 a = tal_strdup(ctx, "hello world!");
99 ok1(tal_strreg(ctx, take(a), "([a-z]+)", &b, invalid) == true);
100 ok1(streq(b, "hello"));
101 ok1(tal_count(b) == strlen(b) + 1);
102 ok1(tal_parent(b) == ctx);
104 ok1(no_children(ctx));
107 a = tal_strdup(ctx, "([a-z]+)");
108 ok1(tal_strreg(ctx, "hello world!", take(a), &b, invalid) == true);
109 ok1(streq(b, "hello"));
110 ok1(tal_count(b) == strlen(b) + 1);
111 ok1(tal_parent(b) == ctx);
113 ok1(no_children(ctx));
116 a = tal_strdup(ctx, "([a-z]+)");
117 ok1(tal_strreg(ctx, take(tal_strdup(ctx, "hello world!")),
118 take(a), &b, invalid) == true);
119 ok1(streq(b, "hello"));
120 ok1(tal_count(b) == strlen(b) + 1);
121 ok1(tal_parent(b) == ctx);
123 ok1(no_children(ctx));
125 /* ... even if we fail to match. */
126 a = tal_strdup(ctx, "([a-z]+)");
127 ok1(tal_strreg(ctx, take(tal_strdup(ctx, "HELLO WORLD!")),
128 take(a), &b, invalid) == false);
129 ok1(no_children(ctx));
132 /* Don't get fooled by \(! */
133 ok1(tal_strreg(ctx, "(hello) (world)!", "\\([a-z]*\\) \\([a-z]+\\)",
136 return exit_status();