Skip to content
Navigation Menu
{{ message }}
forked from boostorg/regex
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
323 lines (298 loc) · 11.4 KB
/
Copy pathmain.cpp
File metadata and controls
323 lines (298 loc) · 11.4 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
*
* Copyright (c) 2002
* John Maddock
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
#include <iostream>
#include <fstream>
#include <iterator>
#include <cassert>
#include <boost/test/execution_monitor.hpp>
#include "regex_comparison.hpp"
void test_match(const std::string& re, const std::string& text, const std::string& description, bool icase)
{
double time;
results r(re, description);
std::cout << "Testing: \"" << re << "\" against \"" << description << "\"" << std::endl;
#ifdef BOOST_HAS_GRETA
if(time_greta == true)
{
time = g::time_match(re, text, icase);
r.greta_time = time;
std::cout << "\tGRETA regex: " << time << "s\n";
}
if(time_safe_greta == true)
{
time = gs::time_match(re, text, icase);
r.safe_greta_time = time;
std::cout << "\tSafe GRETA regex: " << time << "s\n";
}
#endif
if(time_boost == true)
{
time = b::time_match(re, text, icase);
r.boost_time = time;
std::cout << "\tBoost regex: " << time << "s\n";
}
if(time_localised_boost == true)
{
time = bl::time_match(re, text, icase);
r.localised_boost_time = time;
std::cout << "\tBoost regex (C++ locale): " << time << "s\n";
}
#ifdef BOOST_HAS_POSIX
if(time_posix == true)
{
time = posix::time_match(re, text, icase);
r.posix_time = time;
std::cout << "\tPOSIX regex: " << time << "s\n";
}
#endif
#ifdef BOOST_HAS_PCRE
if(time_pcre == true)
{
time = pcr::time_match(re, text, icase);
r.pcre_time = time;
std::cout << "\tPCRE regex: " << time << "s\n";
}
#endif
#ifdef BOOST_HAS_PCRE_JIT
if(time_pcre_jit == true)
{
time = pcrj::time_match(re, text, icase);
r.pcre_jit_time = time;
std::cout << "\tPCRE JIT regex: " << time << "s\n";
}
#endif
#ifdef BOOST_HAS_XPRESSIVE
if(time_xpressive == true)
{
time = dxpr::time_match(re, text, icase);
r.xpressive_time = time;
std::cout << "\txpressive regex: " << time << "s\n";
}
#endif
#ifdef BOOST_HAS_RE2
if(time_re2 == true)
{
time = gre2::time_match(re, text, icase);
r.re2_time = time;
std::cout << "\tRE2 regex: " << time << "s\n";
}
#endif
#ifndef BOOST_NO_CXX11_HDR_REGEX
if(time_std == true)
{
time = stdr::time_match(re, text, icase);
r.std_time = time;
std::cout << "\tstd::regex: " << time << "s\n";
}
#endif
r.finalise();
result_list.push_back(r);
}
void test_find_all(const std::string& re, const std::string& text, const std::string& description, bool icase)
{
std::cout << "Testing: " << re << std::endl;
double time;
results r(re, description);
#ifdef BOOST_HAS_GRETA
if(time_greta == true)
{
time = g::time_find_all(re, text, icase);
r.greta_time = time;
std::cout << "\tGRETA regex: " << time << "s\n";
}
if(time_safe_greta == true)
{
time = gs::time_find_all(re, text, icase);
r.safe_greta_time = time;
std::cout << "\tSafe GRETA regex: " << time << "s\n";
}
#endif
if(time_boost == true)
{
time = b::time_find_all(re, text, icase);
r.boost_time = time;
std::cout << "\tBoost regex: " << time << "s\n";
}
if(time_localised_boost == true)
{
time = bl::time_find_all(re, text, icase);
r.localised_boost_time = time;
std::cout << "\tBoost regex (C++ locale): " << time << "s\n";
}
#ifdef BOOST_HAS_POSIX
if(time_posix == true)
{
time = posix::time_find_all(re, text, icase);
r.posix_time = time;
std::cout << "\tPOSIX regex: " << time << "s\n";
}
#endif
#ifdef BOOST_HAS_PCRE
if(time_pcre == true)
{
time = pcr::time_find_all(re, text, icase);
r.pcre_time = time;
std::cout << "\tPCRE regex: " << time << "s\n";
}
#endif
#ifdef BOOST_HAS_PCRE_JIT
if(time_pcre_jit == true)
{
time = pcrj::time_find_all(re, text, icase);
r.pcre_jit_time = time;
std::cout << "\tPCRE JIT regex: " << time << "s\n";
}
#endif
#ifdef BOOST_HAS_XPRESSIVE
if(time_xpressive == true)
{
time = dxpr::time_find_all(re, text, icase);
r.xpressive_time = time;
std::cout << "\txpressive regex: " << time << "s\n";
}
#endif
#ifdef BOOST_HAS_RE2
if(time_re2 == true)
{
time = gre2::time_find_all(re, text, icase);
r.re2_time = time;
std::cout << "\tRE2 regex: " << time << "s\n";
}
#endif
#ifndef BOOST_NO_CXX11_HDR_REGEX
if(time_std == true)
{
time = stdr::time_find_all(re, text, icase);
r.std_time = time;
std::cout << "\tstd::regex: " << time << "s\n";
}
#endif
r.finalise();
result_list.push_back(r);
}
int cpp_main(int argc, char * argv[])
{
// start by processing the command line args:
if(argc < 2)
return show_usage();
int result = 0;
for(int c = 1; c < argc; ++c)
{
result += handle_argument(argv[c]);
}
if(result)
return result;
if(test_matches)
{
// start with a simple test, this is basically a measure of the minimal overhead
// involved in calling a regex matcher:
test_match("abc", "abc");
// these are from the regex docs:
test_match("^([0-9]+)(\\-| |$)(.*)$", "100- this is a line of ftp response which contains a message string");
test_match("([[:digit:]]{4}[- ]){3}[[:digit:]]{3,4}", "1234-5678-1234-456");
// these are from http://www.regxlib.com/
test_match("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", "john@johnmaddock.co.uk");
test_match("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", "foo12@foo.edu");
test_match("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", "bob.smith@foo.tv");
test_match("^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$", "EH10 2QQ");
test_match("^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$", "G1 1AA");
test_match("^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$", "SW1 1ZZ");
test_match("^[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}$", "4/1/2001");
test_match("^[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}$", "12/12/2001");
test_match("^[-+]?[[:digit:]]*\\.?[[:digit:]]*$", "123");
test_match("^[-+]?[[:digit:]]*\\.?[[:digit:]]*$", "+3.14159");
test_match("^[-+]?[[:digit:]]*\\.?[[:digit:]]*$", "-3.14159");
}
output_html_results(true, "%short_matches%");
std::string file_contents;
if(test_code)
{
load_file(file_contents, "../../../boost/crc.hpp");
const char* highlight_expression = // preprocessor directives: index 1
"(^[ \t]*#(?:[^\\\\\\n]|\\\\[^\\n_[:punct:][:alnum:]]*[\\n[:punct:][:word:]])*)|"
// comment: index 2
"(//[^\\n]*|/\\*.*?\\*/)|"
// literals: index 3
"\\<([+-]?(?:(?:0x[[:xdigit:]]+)|(?:(?:[[:digit:]]*\\.)?[[:digit:]]+(?:[eE][+-]?[[:digit:]]+)?))u?(?:(?:int(?:8|16|32|64))|L)?)\\>|"
// string literals: index 4
"('(?:[^\\\\']|\\\\.)*'|\"(?:[^\\\\\"]|\\\\.)*\")|"
// keywords: index 5
"\\<(__asm|__cdecl|__declspec|__export|__far16|__fastcall|__fortran|__import"
"|__pascal|__rtti|__stdcall|_asm|_cdecl|__except|_export|_far16|_fastcall"
"|__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto|bool"
"|break|case|catch|cdecl|char|class|const|const_cast|continue|default|delete"
"|do|double|dynamic_cast|else|enum|explicit|extern|false|float|for|friend|goto"
"|if|inline|int|long|mutable|namespace|new|operator|pascal|private|protected"
"|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast"
"|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned"
"|using|virtual|void|volatile|wchar_t|while)\\>"
;
const char* class_expression = "^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
"(class|struct)[[:space:]]*(\\<\\w+\\>([ \t]*\\([^)]*\\))?"
"[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?"
"(\\{|:[^;\\{()]*\\{)";
const char* include_expression = "^[ \t]*#[ \t]*include[ \t]+(\"[^\"]+\"|<[^>]+>)";
const char* boost_include_expression = "^[ \t]*#[ \t]*include[ \t]+(\"boost/[^\"]+\"|<boost/[^>]+>)";
bool time_posix_orig = time_posix;
time_posix = false;
test_find_all(class_expression, file_contents);
test_find_all(highlight_expression, file_contents);
test_find_all(include_expression, file_contents);
test_find_all(boost_include_expression, file_contents);
time_posix = time_posix_orig;
}
output_html_results(false, "%code_search%");
if(test_html)
{
load_file(file_contents, "../../../libs/libraries.htm");
test_find_all("beman|john|dave", file_contents, true);
test_find_all("<a[^>]+href=(\"[^\"]*\"|[^[:space:]]+)[^>]*>", file_contents, true);
test_find_all("<img[^>]+src=(\"[^\"]*\"|[^[:space:]]+)[^>]*>", file_contents, true);
bool time_posix_orig = time_posix;
time_posix = false;
// POSIX-Extended unspport Non greedy repeats
test_find_all("<p>.*?</p>", file_contents, true);
test_find_all("<h[12345678][^>]*>.*?</h[12345678]>", file_contents, true);
test_find_all("<font[^>]+face=(\"[^\"]*\"|[^[:space:]]+)[^>]*>.*?</font>", file_contents, true);
time_posix = time_posix_orig;
}
output_html_results(false, "%html_search%");
if(test_short_twain)
{
load_file(file_contents, "short_twain.txt");
test_find_all("Twain", file_contents);
test_find_all("Huck[[:alpha:]]+", file_contents);
test_find_all("[[:alpha:]]+ing", file_contents);
bool time_posix_orig = time_posix;
time_posix = false;
test_find_all("^[^\n]*?Twain", file_contents);
time_posix = time_posix_orig;
test_find_all("Tom|Sawyer|Huckleberry|Finn", file_contents);
test_find_all("(Tom|Sawyer|Huckleberry|Finn).{0,30}river|river.{0,30}(Tom|Sawyer|Huckleberry|Finn)", file_contents);
}
output_html_results(false, "%short_twain_search%");
if(test_long_twain)
{
load_file(file_contents, "mtent12.txt");
test_find_all("Twain", file_contents);
test_find_all("Huck[[:alpha:]]+", file_contents);
test_find_all("[[:alpha:]]+ing", file_contents);
bool time_posix_orig = time_posix;
time_posix = false;
test_find_all("^[^\n]*?Twain", file_contents); // POSIX-Extended: the escape character is not "special" inside a character class declaration
time_posix = time_posix_orig;
test_find_all("Tom|Sawyer|Huckleberry|Finn", file_contents);
test_find_all("(Tom|Sawyer|Huckleberry|Finn).{0,30}river|river.{0,30}(Tom|Sawyer|Huckleberry|Finn)", file_contents);
}
output_html_results(false, "%long_twain_search%");
output_final_html();
return 0;
}
You can’t perform that action at this time.
