-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
52 lines (41 loc) · 1.33 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "re.h"
int main(int argc, char **argv)
{
re_pat *re;
char buffer[512];
re = re_compile("this (is|was) fun");
struct { char const *s; bool e; } tests[] = {
{ "this is fun", true }
, { "this was fun", true }
, { "this fun", false }
, { "thi was is fun", false }
};
struct re_result m;
for (size_t i = 0; i < sizeof tests / sizeof tests[0]; ++i) {
if (re_match(re, tests[i].s, &m) != tests[i].e) {
fprintf(stderr, "Test failed: `%s`\n", tests[i].s);
}
}
re_free(re);
if (argc <= 1) {
return 0;
}
re = re_compile(argv[1]);
if (re == NULL) {
fprintf(stderr, "Failed to compile regular expression: `%s`\n", argv[1]);
return 1;
}
while (fgets(buffer, sizeof buffer, stdin)) {
buffer[strcspn(buffer, "\n")] = 0;
if (re_match(re, buffer, &m)) {
printf("Match: `%.*s`\n", (int) (m.end - m.start), m.start);
} else {
puts("No match!");
}
}
re_free(re);
return 0;
}