1 |
// Feeping Creaturism |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Matcher.hpp" |
8 |
|
9 |
bool Matcher::match(const std::string& stuff) |
10 |
{ |
11 |
substrings.clear(); |
12 |
|
13 |
if (expression != NULL) |
14 |
{ |
15 |
int length; |
16 |
|
17 |
pcre_fullinfo(expression, NULL, PCRE_INFO_CAPTURECOUNT, &length); |
18 |
|
19 |
int* substrings(new int[(++length *= 3)]); |
20 |
int count(pcre_exec(expression, NULL, stuff.c_str(), stuff.length(), 0, |
21 |
0, substrings, length)); |
22 |
|
23 |
if (count > 0) |
24 |
{ |
25 |
char* substring(new char[stuff.length() + 1]); |
26 |
|
27 |
for (int index(0); index < count; ++index) |
28 |
{ |
29 |
pcre_copy_substring(stuff.c_str(), substrings, count, index, |
30 |
substring, stuff.length() + 1); |
31 |
|
32 |
this->substrings.push_back(substring); |
33 |
} |
34 |
|
35 |
delete [] substring; |
36 |
delete [] substrings; |
37 |
|
38 |
return true; |
39 |
} |
40 |
|
41 |
delete [] substrings; |
42 |
} |
43 |
|
44 |
return false; |
45 |
} |
46 |
|
47 |
Matcher& Matcher::operator()(const std::string& expression) |
48 |
{ |
49 |
substrings.clear(); |
50 |
|
51 |
if (this->expression != NULL) pcre_free(this->expression); |
52 |
|
53 |
const char* error; |
54 |
int offset; |
55 |
|
56 |
this->expression = pcre_compile(expression.c_str(), 0, &error, &offset, |
57 |
NULL); |
58 |
|
59 |
return *this; |
60 |
} |