1 |
douglas |
3 |
// Charlemagne Package Manager |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
/* Menes - C++ High-Level Utility Library |
8 |
|
|
* Copyright (C) 2003-2005 Jay Freeman (saurik) |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
/* |
12 |
|
|
* Redistribution and use in source and binary |
13 |
|
|
* forms, with or without modification, are permitted |
14 |
|
|
* provided that the following conditions are met: |
15 |
|
|
* |
16 |
|
|
* 1. Redistributions of source code must retain the |
17 |
|
|
* above copyright notice, this list of conditions |
18 |
|
|
* and the following disclaimer. |
19 |
|
|
* 2. Redistributions in binary form must reproduce the |
20 |
|
|
* above copyright notice, this list of conditions |
21 |
|
|
* and the following disclaimer in the documentation |
22 |
|
|
* and/or other materials provided with the |
23 |
|
|
* distribution. |
24 |
|
|
* 3. The name of the author may not be used to endorse |
25 |
|
|
* or promote products derived from this software |
26 |
|
|
* without specific prior written permission. |
27 |
|
|
* |
28 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' |
29 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, |
30 |
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
31 |
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
32 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
33 |
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
34 |
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
35 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
36 |
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
37 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
38 |
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
39 |
|
|
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
40 |
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
41 |
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
42 |
|
|
*/ |
43 |
|
|
|
44 |
|
|
#include <cassert> |
45 |
|
|
#include <sstream> |
46 |
|
|
|
47 |
|
|
#include "regex.hpp" |
48 |
douglas |
25 |
#include "truck.hpp" |
49 |
douglas |
3 |
|
50 |
|
|
namespace Pcre |
51 |
|
|
{ |
52 |
|
|
|
53 |
|
|
Error::Error(int code) |
54 |
|
|
{ |
55 |
|
|
std::ostringstream message; |
56 |
|
|
|
57 |
douglas |
25 |
message << _B("Pcre[#") << code << _B("] "); |
58 |
douglas |
3 |
|
59 |
|
|
switch (code) |
60 |
|
|
{ |
61 |
|
|
case PCRE_ERROR_NOMATCH: |
62 |
douglas |
25 |
message << _B("PCRE_ERROR_NOMATCH"); |
63 |
douglas |
3 |
|
64 |
|
|
break; |
65 |
|
|
|
66 |
|
|
case PCRE_ERROR_NULL: |
67 |
douglas |
25 |
message << _B("PCRE_ERROR_NULL"); |
68 |
douglas |
3 |
|
69 |
|
|
break; |
70 |
|
|
|
71 |
|
|
case PCRE_ERROR_BADOPTION: |
72 |
douglas |
25 |
message << _B("PCRE_ERROR_BADOPTION"); |
73 |
douglas |
3 |
|
74 |
|
|
break; |
75 |
|
|
|
76 |
|
|
case PCRE_ERROR_BADMAGIC: |
77 |
douglas |
25 |
message << _B("PCRE_ERROR_BADMAGIC"); |
78 |
douglas |
3 |
|
79 |
|
|
break; |
80 |
|
|
|
81 |
|
|
case PCRE_ERROR_UNKNOWN_NODE: |
82 |
douglas |
25 |
message << _B("PCRE_ERROR_UNKNOWN_NODE"); |
83 |
douglas |
3 |
|
84 |
|
|
break; |
85 |
|
|
|
86 |
|
|
case PCRE_ERROR_NOMEMORY: |
87 |
douglas |
25 |
message << _B("PCRE_ERROR_NOMEMORY"); |
88 |
douglas |
3 |
|
89 |
|
|
break; |
90 |
|
|
|
91 |
|
|
case PCRE_ERROR_NOSUBSTRING: |
92 |
douglas |
25 |
message << _B("PCRE_ERROR_NOSUBSTRING"); |
93 |
douglas |
3 |
|
94 |
|
|
break; |
95 |
|
|
|
96 |
|
|
case PCRE_ERROR_MATCHLIMIT: |
97 |
douglas |
25 |
message << _B("PCRE_ERROR_MATCHLIMIT"); |
98 |
douglas |
3 |
|
99 |
|
|
break; |
100 |
|
|
|
101 |
|
|
case PCRE_ERROR_CALLOUT: |
102 |
douglas |
25 |
message << _B("PCRE_ERROR_CALLOUT"); |
103 |
douglas |
3 |
|
104 |
|
|
break; |
105 |
|
|
case PCRE_ERROR_BADUTF8: |
106 |
douglas |
25 |
message << _B("PCRE_ERROR_BADUTF8"); |
107 |
douglas |
3 |
|
108 |
|
|
break; |
109 |
|
|
|
110 |
|
|
case PCRE_ERROR_BADUTF8_OFFSET: |
111 |
douglas |
25 |
message << _B("PCRE_ERROR_BADUTF8_OFFSET"); |
112 |
douglas |
3 |
|
113 |
|
|
break; |
114 |
|
|
|
115 |
|
|
default: |
116 |
douglas |
25 |
message << _B("Unknown Error"); |
117 |
douglas |
3 |
} |
118 |
|
|
|
119 |
|
|
this->message = message.str(); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
RegEx::Match::Match(const std::string &data, std::vector<int> &substrings) : data(data) |
123 |
|
|
{ |
124 |
|
|
std::swap(this->substrings, substrings); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
RegEx::Match::Match() {} |
128 |
|
|
|
129 |
|
|
RegEx::Match::operator bool() const |
130 |
|
|
{ |
131 |
|
|
return !substrings.empty(); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
std::string RegEx::Match::operator [](size_t substring) const |
135 |
|
|
{ |
136 |
|
|
size_t index(substring * 2); |
137 |
|
|
|
138 |
|
|
if (index >= substrings.size()) |
139 |
|
|
return std::string(); |
140 |
|
|
|
141 |
|
|
return std::string(data.begin() + substrings[index], data.begin() + substrings[index + 1]); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
RegEx::RegEx(const std::string &pattern) : code(NULL), study(NULL), capture(-1) |
145 |
|
|
{ |
146 |
|
|
const char *error; |
147 |
|
|
int offset; |
148 |
|
|
|
149 |
|
|
code = ::pcre_compile(pattern.c_str(), PCRE_DOTALL | PCRE_DOLLAR_ENDONLY, &error, &offset, NULL); |
150 |
|
|
|
151 |
|
|
int err(::pcre_fullinfo(code, study, PCRE_INFO_CAPTURECOUNT, &capture)); |
152 |
|
|
|
153 |
|
|
if (err != 0) |
154 |
|
|
{ |
155 |
douglas |
25 |
assert(err < 0); |
156 |
douglas |
3 |
|
157 |
|
|
throw Error(err); |
158 |
|
|
} |
159 |
|
|
|
160 |
douglas |
25 |
assert(capture >= 0); |
161 |
douglas |
3 |
} |
162 |
|
|
|
163 |
|
|
RegEx::~RegEx() |
164 |
|
|
{ |
165 |
|
|
::pcre_free(code); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
RegEx::Match RegEx::operator ()(const std::string &data) const |
169 |
|
|
{ |
170 |
|
|
std::vector<int> substrings((capture + 1) * 3); |
171 |
|
|
int err(::pcre_exec(code, study, data.c_str(), data.size(), 0, 0, &substrings.front(), substrings.size())); |
172 |
|
|
|
173 |
|
|
_assert(err != 0); |
174 |
|
|
|
175 |
|
|
if (err == PCRE_ERROR_NOMATCH) |
176 |
|
|
err = 0; |
177 |
|
|
|
178 |
|
|
if (err < 0) |
179 |
|
|
throw Error(err); |
180 |
|
|
|
181 |
|
|
substrings.resize(err * 2); |
182 |
|
|
|
183 |
|
|
return Match(data, substrings); |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
} |