Skip to content

Instantly share code, notes, and snippets.

@Leandros
Last active October 14, 2018 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leandros/96f727171b7f80126f9f00694082493c to your computer and use it in GitHub Desktop.
Save Leandros/96f727171b7f80126f9f00694082493c to your computer and use it in GitHub Desktop.
/*
* Compile with re2c:
* $ re2c main.cpp -o main.re2c.cpp --nested-ifs --bit-vectors --utf-8 --no-debug-info
* $ g++ -std=c++17 -O3 -o test_re2c main.re2c.cpp
*/
#include <iostream>
#include <fstream>
bool
match_re2c(char const *YYCURSOR)
{
char const *YYMARKER;
/*!re2c
re2c:define:YYCTYPE = char;
re2c:yyfill:enable = 0;
PATTERN = "ABCD"|"DEFGH"|"EFGHI"|"A"{4,};
END = "\x00";
* { return false; }
PATTERN END { return true; }
*/
}
bool
match(std::string const &line)
{
return match_re2c(line.c_str());
}
int main(int argc, char **argv)
{
std::ifstream stream{argv[1], std::ifstream::in};
std::string line;
while (std::getline(stream, line)) {
if (match(line)) {
std::cout << line << '\n';
}
}
}
#include <iostream>
#include <fstream>
#include "compile-time-regular-expressions/include/ctre.hpp"
int main(int argc, char **argv)
{
using namespace ctre::literals;
auto constexpr re = "ABCD|DEFGH|EFGHI|A{4,}"_ctre;
std::ifstream stream{argv[1], std::ifstream::in};
std::string line;
while (std::getline(stream, line)) {
if (re.match(line)) {
std::cout << line << '\n';
}
}
}
% time ./testctre words2
./testctre words2 4.88s user 0.10s system 99% cpu 6.429 total
% time ./testre2c words2
./testre2c words2 4.77s user 0.09s system 99% cpu 4.866 total
% ls -lah words2 # /usr/share/dict/words duplicated a dozen times
-rw-r--r-- 1 leandros staff 500M Oct 14 12:30 words2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment