1 |
// Matcher |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Matcher.hpp" |
8 |
|
9 |
bool Matcher::match(const ext::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.GetData().Begin(), stuff.GetData().GetSize(), 0, 0, substrings, length)); |
21 |
|
22 |
if (count > 0) |
23 |
{ |
24 |
char* substring(new char[stuff.GetData().GetSize() + 1]); |
25 |
|
26 |
for (int index(0); index < count; ++index) |
27 |
{ |
28 |
pcre_copy_substring(stuff.GetData().Begin(), substrings, count, index, substring, stuff.GetData().GetSize() + 1); |
29 |
|
30 |
this->substrings.InsertLast(substring); |
31 |
} |
32 |
|
33 |
delete [] substring; |
34 |
delete [] substrings; |
35 |
|
36 |
return true; |
37 |
} |
38 |
|
39 |
delete [] substrings; |
40 |
} |
41 |
|
42 |
return false; |
43 |
} |
44 |
|
45 |
Matcher& Matcher::operator()(const ext::String& expression) |
46 |
{ |
47 |
substrings.Clear(); |
48 |
|
49 |
if (this->expression != NULL) pcre_free(this->expression); |
50 |
|
51 |
const char* error; |
52 |
int offset; |
53 |
|
54 |
this->expression = pcre_compile(expression.NullTerminate(), PCRE_MULTILINE, &error, &offset, NULL); |
55 |
|
56 |
return *this; |
57 |
} |