-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex.cpp
More file actions
62 lines (57 loc) · 1.19 KB
/
regex.cpp
File metadata and controls
62 lines (57 loc) · 1.19 KB
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
53
54
55
56
57
58
59
60
61
62
#include "regex.h"
#include <cstdio>
#include <cstring>
#include <cctype>
int match(char *regexp, char *text, Options opts)
{
if (regexp[0] == '^')
{
return matchhere(regexp + 1, text, opts);
}
do
{
if (matchhere(regexp, text, opts))
{
return 1;
}
} while (*text++ != '\0');
return 0;
}
int matchhere(char *regexp, char *text, Options opts)
{
if (regexp[0] == '\0')
{
return 1;
}
if (regexp[1] == '*')
{
return matchstar(regexp[0], regexp + 2, text, opts);
}
if (regexp[1] == '+')
{
if (regexp[0] == '.' || regexp[0] == *text) {
return matchstar(regexp[0], regexp + 2, text, opts);
}
return 0;
}
if (regexp[0] == '$' && regexp[1] == '\0')
{
return *text == '\0';
}
if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text || (opts.caseinsensitive && regexp[0] == tolower(*text))))
{
return matchhere(regexp + 1, text + 1, opts);
}
return 0;
}
int matchstar(int c, char *regexp, char *text, Options opts)
{
do
{
if (matchhere(regexp, text, opts))
{
return 1;
}
} while (*text != '\0' && (c == '.' || opts.caseinsensitive ? tolower(*text++) == c : *text++ == c ));
return 0;
}