-
Notifications
You must be signed in to change notification settings - Fork 6
/
ftest.cpp
246 lines (231 loc) · 6.59 KB
/
ftest.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// test FuzzyMatcher
//
// ftest
// ftest max_error[idsb]
// ftest regex text [max_error[idsb]]
//
// where:
// 0 <= max_error <= 255 is the maximum edit distance^*, defaults to 1
// idsb is one or more optional characters i, d, or s to limit edits to
// insert, delete, and/or substitute, and b to limit matching to ASCII/binary
//
// *) Levenshstein distance: char insertion, substitution, deletion
//
// Build after installing RE/flex:
// c++ -o ftest ftest.cpp -lreflex
//
// Examples:
// ./ftest
// ./ftest 2ids
// ./ftest 'abcd' 'axbcd'
// ./ftest 'abcd' 'axcd'
// ./ftest 'abcd' 'acd'
// ./ftest 'a😀cd' 'axcd'
// ./ftest 'abcd' 'a😀cd'
// ./ftest 'aλcd' 'a😀cd'
// ./ftest 'αλcd' 'α😀cd'
// ./ftest 'aλβd' 'a😀βd'
// ./ftest 'acd' 'a😀cd'
// ./ftest 'αcd' 'α😀cd'
// ./ftest 'aβd' 'a😀βd'
// ./ftest 'αβd' 'α😀βd'
// ./ftest 'a😀cd' 'acd'
// ./ftest 'α😀cd' 'αcd'
// ./ftest 'a😀βd' 'aβd'
// ./ftest 'α😀βd' 'αβd'
// ./ftest 'a\d+z' 'az'
// ./ftest 'a\d+z' 'a9'
// ./ftest 'abcd' 'aabcd'
// ./ftest 'abcd' 'abbcd'
// ./ftest 'ab_cd' 'abcd'
// ./ftest 'ab_cd' 'ab-cd'
// ./ftest 'ab_cd' 'abCd' 2
// ./ftest 'ab_cd' 'ab_ab_cd' 2
// #define DEBUG // enable debugging to stderr
#include "fuzzymatcher.h"
int main(int argc, char **argv)
{
if (argc > 2)
{
try
{
std::string regex = reflex::Matcher::convert(argv[1], reflex::convert_flag::unicode);
reflex::Pattern reflex_pattern(regex, "mr");
const char *text = argv[2];
uint8_t max = 1;
uint16_t flags = 0;
if (argc > 3)
{
char *rest;
unsigned long n = strtoul(argv[3], &rest, 10);
if (n > 255)
{
fprintf(stderr, "max_error too large\n\n");
exit(EXIT_FAILURE);
}
max = static_cast<uint8_t>(n);
if (*rest != '\0')
{
if (strchr(rest, 'i') != NULL)
flags |= reflex::FuzzyMatcher::INS;
if (strchr(rest, 'd') != NULL)
flags |= reflex::FuzzyMatcher::DEL;
if (strchr(rest, 's') != NULL)
flags |= reflex::FuzzyMatcher::SUB;
if (strchr(rest, 'b') != NULL)
flags |= reflex::FuzzyMatcher::BIN;
}
}
reflex::FuzzyMatcher matcher(reflex_pattern, max | flags, text);
if (matcher.matches())
printf("matches(): %u edits\n", matcher.edits());
matcher.input(text);
/* scan() may not work well with fuzzy matching, because of possible "gaps" between matches caused by fuzzy matches
while (matcher.scan())
printf("scan(): '%s' at %zu (%u edits)\n", matcher.text(), matcher.columno(), matcher.edits());
matcher.input(text);
*/
while (matcher.find())
printf("find(): %u edits: '%s' at %zu\n", matcher.edits(), matcher.text(), matcher.columno());
matcher.input(text);
while (matcher.split())
printf("split(): %u edits: '%s' at %zu\n", matcher.edits(), matcher.text(), matcher.columno());
}
catch (const reflex::regex_error& e)
{
std::cerr << e.what();
}
}
else
{
uint8_t max = 1;
uint16_t flags = 0;
if (argc > 1)
{
char *rest;
unsigned long n = strtoul(argv[1], &rest, 10);
if (n > 255)
{
fprintf(stderr, "max_error too large\n\n");
exit(EXIT_FAILURE);
}
max = static_cast<uint8_t>(n);
if (*rest != '\0')
{
if (strchr(rest, 'i') != NULL)
flags |= reflex::FuzzyMatcher::INS;
if (strchr(rest, 'd') != NULL)
flags |= reflex::FuzzyMatcher::DEL;
if (strchr(rest, 's') != NULL)
flags |= reflex::FuzzyMatcher::SUB;
if (strchr(rest, 'b') != NULL)
flags |= reflex::FuzzyMatcher::BIN;
}
}
printf("PATTERN/TEXT DISTANCE=%u TESTING\n", max);
const char *regex_texts[] = {
"abcd", "abcd",
"abcd+", "abcd",
"abcd", "ab_cd",
"ab_cd", "ab-cd",
"ab_cd", "abcd",
"abcd", "abcd_",
"abcd_", "abcd-",
"abcd_", "abcd",
"ab_*cd", "ab_cd",
"ab_+cd", "ab-cd",
"ab_+cd", "abcd",
"abcd_*", "abcd_",
"abcd_+", "abcd-",
"abcd_+", "abcd",
"^abcd$", "ab_cd",
"^ab_cd$", "ab-cd",
"^ab_cd$", "abcd",
"^abcd$", "abcd_",
"^abcd_$", "abcd-",
"^abcd_$", "abcd",
"\\<abcd\\>", "ab_cd",
"\\<ab_cd\\>", "ab-cd",
"\\<ab_cd\\>", "abcd",
"\\<abcd\\>", "abcd_",
"\\<abcd_\\>", "abcd-",
"\\<abcd_\\>", "abcd",
"abcd", "abbcd",
"abcd", "axcd",
"abcd", "acd",
"abcd", "abcdd",
"abcd", "abcx",
"abcd", "abc",
"abcd", "aabcd",
"αβγδ", "αββγδ",
"αβγδ", "αωγδ",
"αβγδ", "αγδ",
"αβγδ", "αβγδδ",
"αβγδ", "αβγω",
"αβγδ", "αβγ",
"αβγδ", "ααβγδ",
"aβcδ", "aββcδ",
"aβcδ", "aωcδ",
"aβcδ", "acδ",
"aβcδ", "aβcδδ",
"aβcδ", "aβcω",
"aβcδ", "aβc",
"aβcδ", "aaβcδ",
"αργσ", "αρργσ",
"αργσ", "αβγσ",
"αργσ", "αγσ",
"αργσ", "αργσσ",
"αργσ", "αργω",
"αργσ", "αργ",
"αργσ", "ααργσ",
"abcd", "abβcd",
"abcd", "aωcd",
"abcd", "abcdδ",
"abcd", "abcω",
"abcd", "aαbcd",
"aβcd", "aβbcd",
"aβcd", "abβcd",
"aβcd", "axcd",
"aβcd", "acd",
".bcd", "abβcd",
".bcd", "aωcd",
".bcd", "abcdδ",
".bcd", "abcω",
".bcd", "aαbcd",
".βcd", "aβbcd",
".βcd", "abβcd",
".βcd", "axcd",
".βcd", "acd",
NULL, NULL };
int it = 0;
int hits = 0;
while (regex_texts[it] != NULL)
{
try
{
std::string regex = reflex::Matcher::convert(regex_texts[it++], reflex::convert_flag::unicode);
reflex::Pattern reflex_pattern(regex, "mr");
const char *text = regex_texts[it++];
reflex::FuzzyMatcher matcher(regex, max | flags, text);
printf("'%s'/'%s'\n", regex.c_str(), text);
if (matcher.matches())
{
printf("\tmatches(): %u edits\n", matcher.edits());
++hits;
}
matcher.input(text);
while (matcher.find())
{
printf("\tfind(): %u edits: '%s' at %zu\n", matcher.edits(), matcher.text(), matcher.columno());
++hits;
}
}
catch (const reflex::regex_error& e)
{
std::cerr << e.what();
}
}
printf("FUZZY HITS = %.3g%%\n", 100.*hits/it);
}
return 0;
}