|
5 | 5 | #include "re.h"
|
6 | 6 |
|
7 | 7 | #ifdef RE_CSTRINGS
|
| 8 | + #define RE_MATCH re_match_cstr |
8 | 9 | #define RE_MATCH_START_ONLY re_match_start_only_cstr
|
9 | 10 | #define text_args(t) t
|
10 | 11 | #else
|
| 12 | + #define RE_MATCH re_match |
11 | 13 | #define RE_MATCH_START_ONLY re_match_start_only
|
12 | 14 | #define text_args(t) t, t + strlen(t)
|
13 | 15 | #endif
|
14 | 16 |
|
15 |
| -void test(const char *regex, const char *text, int expected_match_len) |
| 17 | +static void checkMatch(struct re_context *context, int match_result, re_length_t match_start, re_length_t expected_match_start, int expected_match_len) |
16 | 18 | {
|
17 |
| - printf("Pattern '%s' Text '%s'\n", regex, text); |
18 |
| - struct re_context context = {0}; |
19 |
| - |
20 |
| - if (RE_MATCH_START_ONLY(&context, regex, text_args(text))) { |
21 |
| - if (expected_match_len == -1) { |
22 |
| - printf("fail: expected not to match\n"); |
23 |
| - exit(1); |
24 |
| - } |
25 |
| - if ((re_length_t)expected_match_len != context.match_length) { |
26 |
| - printf("fail: expected match length %d but got %llu\n", expected_match_len, (unsigned long long)context.match_length); |
27 |
| - exit(1); |
| 19 | + if (match_result) { |
| 20 | + if (expected_match_start != match_start) { |
| 21 | + printf("fail: expected not to match to start at %llu but started at %llu\n", |
| 22 | + (unsigned long long)expected_match_start, (unsigned long long)match_start); |
| 23 | + exit(1); |
| 24 | + } |
| 25 | + if (expected_match_len == -1) { |
| 26 | + printf("fail: expected not to match\n"); |
| 27 | + exit(1); |
| 28 | + } |
| 29 | + if ((re_length_t)expected_match_len != context->match_length) { |
| 30 | + printf("fail: expected match length %d but got %llu\n", expected_match_len, (unsigned long long)context->match_length); |
| 31 | + exit(1); |
| 32 | + } |
| 33 | + } else { |
| 34 | + if (expected_match_len != -1) { |
| 35 | + printf("fail: expected a match\n"); |
| 36 | + exit(1); |
| 37 | + } |
28 | 38 | }
|
29 |
| - } else { |
30 |
| - if (expected_match_len != -1) { |
31 |
| - printf("fail: expected a match\n"); |
| 39 | + if (context->error) { |
| 40 | + printf("fail: unexpected error %s\n", re_error_name_table[context->error]); |
32 | 41 | exit(1);
|
33 | 42 | }
|
| 43 | +} |
| 44 | + |
| 45 | +void testExt(const char *regex, const char *text, re_length_t expected_match_start, int expected_match_len) |
| 46 | +{ |
| 47 | + printf("Pattern '%s' Text '%s'\n", regex, text); |
| 48 | + { |
| 49 | + struct re_context context = {0}; |
| 50 | + re_length_t match_start; |
| 51 | + int match_result = RE_MATCH(&context, regex, text_args(text), &match_start); |
| 52 | + checkMatch(&context, match_result, match_start, expected_match_start, expected_match_len); |
34 | 53 | }
|
35 |
| - if (context.error) { |
36 |
| - printf("fail: unexpected error %s\n", re_error_name_table[context.error]); |
37 |
| - exit(1); |
| 54 | + |
| 55 | + if (expected_match_start == 0) { |
| 56 | + struct re_context context = {0}; |
| 57 | + int match_result = RE_MATCH_START_ONLY(&context, regex, text_args(text)); |
| 58 | + checkMatch(&context, match_result, 0, 0, expected_match_len); |
38 | 59 | }
|
39 | 60 | }
|
40 | 61 |
|
| 62 | +void test(const char *regex, const char *text, int expected_match_len) |
| 63 | +{ |
| 64 | + testExt(regex, text, 0, expected_match_len); |
| 65 | +} |
| 66 | + |
41 | 67 | void testbad(const char *regex, const char *text, enum re_error error)
|
42 | 68 | {
|
43 | 69 | printf("BadPattern '%s' Text '%s'\n", regex, text);
|
@@ -194,4 +220,7 @@ int main()
|
194 | 220 |
|
195 | 221 | // https://github.com/kokke/tiny-regex-c/issues/53
|
196 | 222 | test(".*a.*a", "aa", 2);
|
| 223 | + |
| 224 | + // https://github.com/kokke/tiny-regex-c/issues/69 |
| 225 | + testExt("{\\w+}", "{{FW}_TEST", 1, 4); |
197 | 226 | }
|
0 commit comments