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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
/*
*
* Copyright (c) 2002-2003
* Dr 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 <iomanip>
#include <fstream>
#include <deque>
#include <sstream>
#include <stdexcept>
#include <iterator>
#include <boost/regex.hpp>
#include <boost/version.hpp>
#include "regex_comparison.hpp"
#ifdef BOOST_HAS_PCRE
#include "pcre.h" // for pcre version number
#endif
//
// globals:
//
bool time_boost = false;
bool time_localised_boost = false;
bool time_greta = false;
bool time_safe_greta = false;
bool time_posix = false;
bool time_pcre = false;
bool test_matches = false;
bool test_code = false;
bool test_html = false;
bool test_short_twain = false;
bool test_long_twain = false;
std::string html_template_file;
std::string html_out_file;
std::string html_contents;
std::list<results> result_list;
// the following let us compute averages:
double greta_total = 0;
double safe_greta_total = 0;
double boost_total = 0;
double locale_boost_total = 0;
double posix_total = 0;
double pcre_total = 0;
unsigned greta_test_count = 0;
unsigned safe_greta_test_count = 0;
unsigned boost_test_count = 0;
unsigned locale_boost_test_count = 0;
unsigned posix_test_count = 0;
unsigned pcre_test_count = 0;
int handle_argument(const std::string& what)
{
if(what == "-b")
time_boost = true;
else if(what == "-bl")
time_localised_boost = true;
#ifdef BOOST_HAS_GRETA
else if(what == "-g")
time_greta = true;
else if(what == "-gs")
time_safe_greta = true;
#endif
#ifdef BOOST_HAS_POSIX
else if(what == "-posix")
time_posix = true;
#endif
#ifdef BOOST_HAS_PCRE
else if(what == "-pcre")
time_pcre = true;
#endif
else if(what == "-all")
{
time_boost = true;
time_localised_boost = true;
#ifdef BOOST_HAS_GRETA
time_greta = true;
time_safe_greta = true;
#endif
#ifdef BOOST_HAS_POSIX
time_posix = true;
#endif
#ifdef BOOST_HAS_PCRE
time_pcre = true;
#endif
}
else if(what == "-test-matches")
test_matches = true;
else if(what == "-test-code")
test_code = true;
else if(what == "-test-html")
test_html = true;
else if(what == "-test-short-twain")
test_short_twain = true;
else if(what == "-test-long-twain")
test_long_twain = true;
else if(what == "-test-all")
{
test_matches = true;
test_code = true;
test_html = true;
test_short_twain = true;
test_long_twain = true;
}
else if((what == "-h") || (what == "--help"))
return show_usage();
else if((what[0] == '-') || (what[0] == '/'))
{
std::cerr << "Unknown argument: \"" << what << "\"" << std::endl;
return 1;
}
else if(html_template_file.size() == 0)
{
html_template_file = what;
load_file(html_contents, what.c_str());
}
else if(html_out_file.size() == 0)
html_out_file = what;
else
{
std::cerr << "Unexpected argument: \"" << what << "\"" << std::endl;
return 1;
}
return 0;
}
int show_usage()
{
std::cout <<
"Usage\n"
"regex_comparison [-h] [library options] [test options] [html_template html_output_file]\n"
" -h Show help\n\n"
" library options:\n"
" -b Apply tests to boost library\n"
" -bl Apply tests to boost library with C++ locale\n"
#ifdef BOOST_HAS_GRETA
" -g Apply tests to GRETA library\n"
" -gs Apply tests to GRETA library (in non-recursive mode)\n"
#endif
#ifdef BOOST_HAS_POSIX
" -posix Apply tests to POSIX library\n"
#endif
#ifdef BOOST_HAS_PCRE
" -pcre Apply tests to PCRE library\n"
#endif
" -all Apply tests to all libraries\n\n"
" test options:\n"
" -test-matches Test short matches\n"
" -test-code Test c++ code examples\n"
" -test-html Test c++ code examples\n"
" -test-short-twain Test short searches\n"
" -test-long-twain Test long searches\n"
" -test-all Test everthing\n";
return 1;
}
void load_file(std::string& text, const char* file)
{
std::deque<char> temp_copy;
std::ifstream is(file);
if(!is.good())
{
std::string msg("Unable to open file: \"");
msg.append(file);
msg.append("\"");
throw std::runtime_error(msg);
}
is.seekg(0, std::ios_base::end);
std::istream::pos_type pos = is.tellg();
is.seekg(0, std::ios_base::beg);
text.erase();
text.reserve(pos);
std::istreambuf_iterator<char> it(is);
std::copy(it, std::istreambuf_iterator<char>(), std::back_inserter(text));
}
void print_result(std::ostream& os, double time, double best)
{
static const char* suffixes[] = {"s", "ms", "us", "ns", "ps", };
if(time < 0)
{
os << "<td>NA</td>";
return;
}
double rel = time / best;
bool highlight = ((rel > 0) && (rel < 1.1));
unsigned suffix = 0;
while(time < 0)
{
time *= 1000;
++suffix;
}
os << "<td>";
if(highlight)
os << "<font color=\"#008000\">";
if(rel <= 1000)
os << std::setprecision(3) << rel;
else
os << (int)rel;
os << "<BR>(";
if(time <= 1000)
os << std::setprecision(3) << time;
else
os << (int)time;
os << suffixes[suffix] << ")";
if(highlight)
os << "</font>";
os << "</td>";
}
std::string html_quote(const std::string& in)
{
static const boost::regex e("(<)|(>)|(&)|(\")");
static const std::string format("(?1<)(?2>)(?3&)(?4")");
return regex_replace(in, e, format, boost::match_default | boost::format_all);
}
void output_html_results(bool show_description, const std::string& tagname)
{
std::stringstream os;
if(result_list.size())
{
//
// start by outputting the table header:
//
os << "<table border=\"1\" cellspacing=\"1\">\n";
os << "<tr><td><strong>Expression</strong></td>";
if(show_description)
os << "<td><strong>Text</strong></td>";
#if defined(BOOST_HAS_GRETA)
if(time_greta == true)
os << "<td><strong>GRETA</strong></td>";
if(time_safe_greta == true)
os << "<td><strong>GRETA<BR>(non-recursive mode)</strong></td>";
#endif
if(time_boost == true)
os << "<td><strong>Boost</strong></td>";
if(time_localised_boost == true)
os << "<td><strong>Boost + C++ locale</strong></td>";
#if defined(BOOST_HAS_POSIX)
if(time_posix == true)
os << "<td><strong>POSIX</strong></td>";
#endif
#ifdef BOOST_HAS_PCRE
if(time_pcre == true)
os << "<td><strong>PCRE</strong></td>";
#endif
os << "</tr>\n";
//
// Now enumerate through all the test results:
//
std::list<results>::const_iterator first, last;
first = result_list.begin();
last = result_list.end();
while(first != last)
{
os << "<tr><td><code>" << html_quote(first->expression) << "</code></td>";
if(show_description)
os << "<td>" << html_quote(first->description) << "</td>";
#if defined(BOOST_HAS_GRETA)
if(time_greta == true)
{
print_result(os, first->greta_time, first->factor);
if(first->greta_time > 0)
{
greta_total += first->greta_time / first->factor;
++greta_test_count;
}
}
if(time_safe_greta == true)
{
print_result(os, first->safe_greta_time, first->factor);
if(first->safe_greta_time > 0)
{
safe_greta_total += first->safe_greta_time / first->factor;
++safe_greta_test_count;
}
}
#endif
#if defined(BOOST_HAS_POSIX)
if(time_boost == true)
{
print_result(os, first->boost_time, first->factor);
if(first->boost_time > 0)
{
boost_total += first->boost_time / first->factor;
++boost_test_count;
}
}
if(time_localised_boost == true)
{
print_result(os, first->localised_boost_time, first->factor);
if(first->localised_boost_time > 0)
{
locale_boost_total += first->localised_boost_time / first->factor;
++locale_boost_test_count;
}
}
#endif
if(time_posix == true)
{
print_result(os, first->posix_time, first->factor);
if(first->posix_time > 0)
{
posix_total += first->posix_time / first->factor;
++posix_test_count;
}
}
#if defined(BOOST_HAS_PCRE)
if(time_pcre == true)
{
print_result(os, first->pcre_time, first->factor);
if(first->pcre_time > 0)
{
pcre_total += first->pcre_time / first->factor;
++pcre_test_count;
}
}
#endif
os << "</tr>\n";
++first;
}
os << "</table>\n";
result_list.clear();
}
else
{
os << "<P><I>Results not available...</I></P>\n";
}
std::string result = os.str();
std::string::size_type pos = html_contents.find(tagname);
if(pos != std::string::npos)
{
html_contents.replace(pos, tagname.size(), result);
}
}
std::string get_boost_version()
{
std::stringstream os;
os << (BOOST_VERSION / 100000) << '.' << ((BOOST_VERSION / 100) % 1000) << '.' << (BOOST_VERSION % 100);
return os.str();
}
std::string get_averages_table()
{
std::stringstream os;
//
// start by outputting the table header:
//
os << "<table border=\"1\" cellspacing=\"1\">\n";
os << "<tr>";
#if defined(BOOST_HAS_GRETA)
if(time_greta == true)
{
os << "<td><strong>GRETA</strong></td>";
}
if(time_safe_greta == true)
{
os << "<td><strong>GRETA<BR>(non-recursive mode)</strong></td>";
}
#endif
if(time_boost == true)
{
os << "<td><strong>Boost</strong></td>";
}
if(time_localised_boost == true)
{
os << "<td><strong>Boost + C++ locale</strong></td>";
}
#if defined(BOOST_HAS_POSIX)
if(time_posix == true)
{
os << "<td><strong>POSIX</strong></td>";
}
#endif
#ifdef BOOST_HAS_PCRE
if(time_pcre == true)
{
os << "<td><strong>PCRE</strong></td>";
}
#endif
os << "</tr>\n";
//
// Now enumerate through all averages:
//
os << "<tr>";
#if defined(BOOST_HAS_GRETA)
if(time_greta == true)
os << "<td>" << (greta_total / greta_test_count) << "</td>\n";
if(time_safe_greta == true)
os << "<td>" << (safe_greta_total / safe_greta_test_count) << "</td>\n";
#endif
#if defined(BOOST_HAS_POSIX)
if(time_boost == true)
os << "<td>" << (boost_total / boost_test_count) << "</td>\n";
if(time_localised_boost == true)
os << "<td>" << (locale_boost_total / locale_boost_test_count) << "</td>\n";
#endif
if(time_posix == true)
os << "<td>" << (posix_total / posix_test_count) << "</td>\n";
#if defined(BOOST_HAS_PCRE)
if(time_pcre == true)
os << "<td>" << (pcre_total / pcre_test_count) << "</td>\n";
#endif
os << "</tr>\n";
os << "</table>\n";
return os.str();
}
void output_final_html()
{
if(html_out_file.size())
{
//
// start with search and replace ops:
//
std::string::size_type pos;
pos = html_contents.find("%compiler%");
if(pos != std::string::npos)
{
html_contents.replace(pos, 10, BOOST_COMPILER);
}
pos = html_contents.find("%library%");
if(pos != std::string::npos)
{
html_contents.replace(pos, 9, BOOST_STDLIB);
}
pos = html_contents.find("%os%");
if(pos != std::string::npos)
{
html_contents.replace(pos, 4, BOOST_PLATFORM);
}
pos = html_contents.find("%boost%");
if(pos != std::string::npos)
{
html_contents.replace(pos, 7, get_boost_version());
}
pos = html_contents.find("%pcre%");
if(pos != std::string::npos)
{
#ifdef PCRE_MINOR
html_contents.replace(pos, 6, BOOST_STRINGIZE(PCRE_MAJOR.PCRE_MINOR));
#else
html_contents.replace(pos, 6, "N/A");
#endif
}
pos = html_contents.find("%averages%");
if(pos != std::string::npos)
{
html_contents.replace(pos, 10, get_averages_table());
}
//
// now right the output to file:
//
std::ofstream os(html_out_file.c_str());
os << html_contents;
}
else
{
std::cout << html_contents;
}
}
|