Last active
October 14, 2018 11:46
-
-
Save Leandros/96f727171b7f80126f9f00694082493c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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'; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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'; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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