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
|
#ifndef CRASH_REGEX
#define CRASH_REGEX
#include <cassert>
#include <cstring>
#include <string>
#include <map>
#include <utility>
#include <stdexcept>
#include <cassert>
#include <sys/types.h>
#include <regex.h>
#ifndef CRASH_REGEX_CACHE_THRESHOLD
#define CRASH_REGEX_CACHE_THRESHOLD 128
#endif
using namespace std;
/**
Regex is a C++ wrapper around the POSIX Regex library.
13/00/01 Added cache. This speeds up general rx construction considerably.
14/08/00 Created.
*/
class Regex {
public :
struct exception : public runtime_error { exception(string const &what) : runtime_error(what) {} };
struct out_of_range : public exception { out_of_range(string const &what) : exception(what) {} };
struct no_match : public exception { no_match(string const &what) : exception(what) {} };
Regex();
Regex(char const *regex);
Regex(Regex const ©);
~Regex();
Regex &operator = (Regex const ©);
Regex &operator = (char const *regex);
string const &source() const { return inrx; }
/*
Regex regex("'([^']*)'");
string out;
out = regex.transform("'alec thomas'", "(\\1)");
// outputs: (alec thomas)
*/
string transform(string const &in, string const &mask);
int match(char const *str);
int operator == (char const *str) { return match(str); }
int matchStart(char const *str);
int operator <= (char const *str) { return matchStart(str); }
int substrings() {
for (int i = 0; i < 50; i++)
if (matches[i].rm_so == -1) return i;
return 50;
}
int subStart(unsigned index) {
assert(index < 50 && matches[index].rm_so != -1);
return matches[index].rm_so;
}
int subEnd(unsigned index) {
assert(index < 50 && matches[index].rm_so != -1);
return matches[index].rm_so;
}
private :
string inrx;
regex_t regex;
regmatch_t matches[50];
struct Cache {
Cache() : hits(0), instances(0) {}
regex_t rx;
int hits, instances;
};
friend struct Cache;
// static map<string, Cache> cache;
};
#endif
|