1 |
douglas |
317 |
// Matcher |
2 |
Douglas Thrift |
113 |
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include "Matcher.hpp" |
8 |
|
|
|
9 |
douglas |
319 |
#ifdef _WIN32 |
10 |
|
|
#pragma warning(disable:4267) |
11 |
|
|
#endif |
12 |
|
|
|
13 |
Douglas Thrift |
266 |
bool Matcher::match(const ext::String& stuff) |
14 |
Douglas Thrift |
115 |
{ |
15 |
douglas |
305 |
substrings.Clear(); |
16 |
Douglas Thrift |
119 |
|
17 |
|
|
if (expression != NULL) |
18 |
Douglas Thrift |
115 |
{ |
19 |
|
|
int length; |
20 |
Douglas Thrift |
113 |
|
21 |
Douglas Thrift |
115 |
pcre_fullinfo(expression, NULL, PCRE_INFO_CAPTURECOUNT, &length); |
22 |
|
|
|
23 |
Douglas Thrift |
266 |
int* substrings(new int[(++length *= 3)]); |
24 |
douglas |
305 |
int count(pcre_exec(expression, NULL, stuff.GetData().Begin(), stuff.GetData().GetSize(), 0, 0, substrings, length)); |
25 |
Douglas Thrift |
119 |
|
26 |
Douglas Thrift |
115 |
if (count > 0) |
27 |
|
|
{ |
28 |
Douglas Thrift |
266 |
char* substring(new char[stuff.GetData().GetSize() + 1]); |
29 |
Douglas Thrift |
115 |
|
30 |
Douglas Thrift |
266 |
for (int index(0); index < count; ++index) |
31 |
Douglas Thrift |
115 |
{ |
32 |
douglas |
305 |
pcre_copy_substring(stuff.GetData().Begin(), substrings, count, index, substring, stuff.GetData().GetSize() + 1); |
33 |
Douglas Thrift |
115 |
|
34 |
douglas |
305 |
this->substrings.InsertLast(substring); |
35 |
Douglas Thrift |
115 |
} |
36 |
|
|
|
37 |
|
|
delete [] substring; |
38 |
|
|
delete [] substrings; |
39 |
Douglas Thrift |
266 |
|
40 |
Douglas Thrift |
115 |
return true; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
delete [] substrings; |
44 |
|
|
} |
45 |
|
|
|
46 |
Douglas Thrift |
113 |
return false; |
47 |
|
|
} |
48 |
Douglas Thrift |
114 |
|
49 |
Douglas Thrift |
266 |
Matcher& Matcher::operator()(const ext::String& expression) |
50 |
Douglas Thrift |
114 |
{ |
51 |
douglas |
305 |
substrings.Clear(); |
52 |
Douglas Thrift |
119 |
|
53 |
Douglas Thrift |
266 |
if (this->expression != NULL) pcre_free(this->expression); |
54 |
Douglas Thrift |
114 |
|
55 |
Douglas Thrift |
116 |
const char* error; |
56 |
|
|
int offset; |
57 |
Douglas Thrift |
115 |
|
58 |
douglas |
305 |
this->expression = pcre_compile(expression.NullTerminate(), PCRE_MULTILINE, &error, &offset, NULL); |
59 |
Douglas Thrift |
116 |
|
60 |
Douglas Thrift |
114 |
return *this; |
61 |
|
|
} |