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
|
#pragma once
class pattern_match
{
public:
static pattern_match succeeded(const string &s, int start = 0, int end = 0)
{
return pattern_match(true, s, start, end);
}
static pattern_match failed(const string &s = string())
{
return pattern_match(false, s, -1, -1);
}
operator bool () const
{
return matched;
}
string annotate_string(const string &color) const;
const string &matched_text() const
{
return text;
}
private:
pattern_match(bool _matched, const string &_text, int _start, int _end)
: matched(_matched), text(_text), start(_start), end(_end)
{
}
bool matched;
string text;
int start;
int end;
};
class base_pattern
{
public:
virtual ~base_pattern() { }
virtual bool valid() const = 0;
virtual bool matches(const string &s) const = 0;
virtual pattern_match match_location(const string &s) const = 0;
virtual const string &tostring() const = 0;
};
class text_pattern : public base_pattern
{
public:
text_pattern(const string &s, bool icase = false)
: pattern(s), compiled_pattern(nullptr),
isvalid(true), ignore_case(icase)
{
}
text_pattern()
: pattern(), compiled_pattern(nullptr),
isvalid(false), ignore_case(false)
{
}
text_pattern(const text_pattern &tp)
: base_pattern(tp),
pattern(tp.pattern),
compiled_pattern(nullptr),
isvalid(tp.isvalid),
ignore_case(tp.ignore_case)
{
}
~text_pattern();
const text_pattern &operator= (const text_pattern &tp);
const text_pattern &operator= (const string &spattern);
bool operator== (const text_pattern &tp) const;
bool compile() const;
bool empty() const { return !pattern.length(); }
bool valid() const override
{
return isvalid
&& (compiled_pattern || (isvalid = compile()));
}
bool matches(const char *s, int length) const;
bool matches(const char *s) const
{
return matches(s, strlen(s));
}
bool matches(const string &s) const override
{
return matches(s.c_str(), s.length());
}
pattern_match match_location(const char *s, int length) const;
pattern_match match_location(const char *s) const
{
return match_location(s, strlen(s));
}
pattern_match match_location(const string &s) const override
{
return match_location(s.c_str(), s.length());
}
const string &tostring() const override
{
return pattern;
}
private:
string pattern;
mutable void *compiled_pattern;
mutable bool isvalid;
bool ignore_case;
};
class plaintext_pattern : public base_pattern
{
public:
plaintext_pattern(const string &s, bool icase = false)
: pattern(s), ignore_case(icase)
{
}
plaintext_pattern()
: pattern(), ignore_case(false)
{
}
const plaintext_pattern &operator= (const string &spattern);
bool operator== (const plaintext_pattern &tp) const;
bool empty() const { return !pattern.length(); }
bool valid() const override { return true; }
bool matches(const string &s) const override;
pattern_match match_location(const string &s) const override;
const string &tostring() const override
{
return pattern;
}
private:
string pattern;
bool ignore_case;
};
|