-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.cpp
188 lines (179 loc) · 5.69 KB
/
main.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "pch.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <algorithm>
//////////////////////////////
bool _match1(const char *pat, const char *str) {
if (pat[0] == 0) return true;
if (pat[0] == '$') return str[0] == 0;
if (str[0] == 0) return false;
if (pat[1] == '*') {
const char *begin = str;
while ((pat[0] == '.' && *str) || pat[0] == *str) ++str;
do {
if (_match1(pat + 2, str)) return true;
} while(begin <= --str);
return false;
}
if ((pat[0] == '.' && *str) || pat[0] == *str) {
return _match1(pat + 1, str + 1);
}
return false;
}
bool find1(const char *pat, const char *str) {
if (pat[0] == '^') return _match1(pat + 1, str);
for (; *str; ++str) {
if (_match1(pat, str)) return true;
}
return false;
}
//////////////////////////////
// always consume pattern
bool _matchCharacter2(const char *&pat, const char *&str) {
bool succ;
if (pat[0] == '\\') {
pat += 2;
switch (pat[-1]) {
case 'd': succ = isdigit(*str); break;
case 'a': succ = isalpha(*str); break;
case 'w': succ = isalnum(*str); break;
default: succ = *str == pat[-1]; break;
}
} else if (pat[0] == '[') {
char buf[128] = {0};
while (*++pat != ']') {
if (*pat == '\\') buf[*++pat] = 1;
else buf[*pat] = 1;
}
++pat;
succ = buf[str[0]];
} else {
++pat;
succ = (pat[-1] == '.' && *str) || pat[-1] == *str;
}
if (succ) ++str;
return succ;
}
bool _testCharacter2(const char *pat, const char *str) {
return _matchCharacter2(pat, str);
}
bool _match2(const char *pat, const char *str) {
if (pat[0] == 0) return true;
if (pat[0] == '$') return str[0] == 0;
if (str[0] == 0) return false;
const char *rawPat = pat;
bool succ = _matchCharacter2(pat, str);
if (pat[0] == '?') {
if (pat[1] == '?') {
if (!succ) return _match2(pat + 2, str);
return _match2(pat + 2, str - 1) || _match2(pat + 2, str);
} else {
if (!succ) return _match2(pat + 1, str);
return _match2(pat + 1, str) || _match2(pat + 1, str - 1);
}
} else if (pat[0] == '*') {
if (pat[1] == '?') {
if (_match2(pat + 2, str - 1)) return true;
if (!succ) return false;
do {
if (_match2(pat + 2, str)) return true;
} while(_testCharacter2(rawPat, str++));
return false;
} else {
if (!succ) return _match2(pat + 1, str);
const char *begin = str - 1;
while (_testCharacter2(rawPat, str)) ++str;
do {
if (_match2(pat + 1, str)) return true;
} while(begin != --str);
return _match2(pat + 1, str);
}
} else if (pat[0] == '+') {
if (!succ) return false;
if (pat[1] == '?') {
do {
if (_match2(pat + 2, str)) return true;
} while (_testCharacter2(rawPat, str++));
return false;
} else {
const char *begin = str - 1;
while(_testCharacter2(rawPat, str)) ++str;
do {
if (_match2(pat + 1, str)) return true;
} while(begin != --str);
return false;
}
} else {
return succ && _match2(pat, str);
}
}
bool find2(const char *pat, const char *str) {
if (pat[0] == '^') return _match2(pat + 1, str);
for (; *str; ++str) {
if (_match2(pat, str)) return true;
}
return false;
}
//////////////////////////////
void test(const char *name, bool(*f)(const char*, const char*)) {
const char *testCase[][3] = {
"def^", "abcdef^", "",
"cde$", "abcdef", NULL,
"def$", "abcdef", "",
"^abcd", "abcdef", "",
"^bcd", "abcdef", NULL,
"a..f", "abcdef", NULL,
"a.*f", "abbbbf", "",
"^a*bb.*.9$", "aabb0099", "",
"^a*b.*.9$", "aabb0099", "",
"^e*abc$", "abc", "",
};
for (int i = 0; i < sizeof(testCase) / sizeof(testCase[0]); ++i) {
if (f(testCase[i][0], testCase[i][1]) != (testCase[i][2] != NULL)) {
printf("failed: %s- %d: %s , %s , %s", name, i, testCase[i][0], testCase[i][1], testCase[i][2] != NULL ? "true" : "false");
assert(0);
}
}
}
void test2(const char *name, bool(*f)(const char*, const char*)) {
const char *testCase[][3] = {
"w+\\.baidu\\.com$", "www.baidu.com", "",
"w+\\.baidu\\.com$", "www.baidu.com,", NULL,
"\\w\\a+\\d*abc", "ab1234abc", "",
"\\w\\a+\\d*abc", "ababc", "",
"\\w\\a+\\d*abc", "aabc", NULL,
"^a??bbb", "bbb", "",
"^a??bbb", "abbb", "",
"^a??bbb", "cbbb", NULL,
"[ab\\]d]*", "bbad]]a", "",
"[ab\\]d]*", "bbad]z]a", "",
"^[ab\\]d]*$", "bbad]z]a", NULL,
};
for (int i = 0; i < sizeof(testCase) / sizeof(testCase[0]); ++i) {
if (f(testCase[i][0], testCase[i][1]) != (testCase[i][2] != NULL)) {
printf("failed: %s- %d: %s , %s , %s", name, i, testCase[i][0], testCase[i][1], testCase[i][2] != NULL ? "true" : "false");
assert(0);
}
}
}
int main() {
test("find1", &find1);
test("find2", &find2);
test2("find2", &find2);
{
clock_t start = clock();
#ifdef NDEBUG
const int LOOP = 1000000;
#else
const int LOOP = 10000;
#endif
for (int i = 0; i < LOOP; ++i) {
test("find2", &find2);
test2("find2", &find2);
}
printf("%f sec\n", float(clock() - start) / CLOCKS_PER_SEC);
}
}