1 |
// Matcher STL |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#ifndef _Matcher_hpp_ |
8 |
#define _Matcher_hpp_ |
9 |
|
10 |
#if defined(_MSC_VER) |
11 |
#if _MSC_VER > 1000 |
12 |
#pragma once |
13 |
#endif |
14 |
#endif |
15 |
|
16 |
#include <string> |
17 |
#include <vector> |
18 |
|
19 |
#include <pcre.h> |
20 |
|
21 |
class Matcher |
22 |
{ |
23 |
private: |
24 |
::pcre* expression; |
25 |
int options; |
26 |
std::vector<std::string> substrings; |
27 |
public: |
28 |
static int defaults; |
29 |
Matcher(int options = defaults) : expression(NULL), options(options) {} |
30 |
Matcher(const std::string& expression, int options = defaults) : expression(NULL), options(options) { (*this)(expression); } |
31 |
~Matcher() { if (expression != NULL) ::pcre_free(expression); } |
32 |
bool match(const std::string& stuff); |
33 |
std::vector<std::string>::size_type size() const { return substrings.size(); } |
34 |
Matcher& operator()(const std::string& expression); |
35 |
const std::string& operator[](std::vector<std::string>::size_type index) const { return substrings[index]; } |
36 |
operator std::string() const { return substrings[0]; } |
37 |
bool operator==(const std::string& stuff) { return match(stuff); } |
38 |
bool operator!=(const std::string& stuff) { return !match(stuff); } |
39 |
// friends: |
40 |
friend bool operator==(const std::string& stuff, Matcher& matcher) { return matcher == stuff; } |
41 |
friend bool operator!=(const std::string& stuff, Matcher& matcher) { return matcher != stuff; } |
42 |
}; |
43 |
|
44 |
#endif // _Matcher_hpp_ |