1 |
// Copyright (c) 2005, Google Inc.
|
2 |
// All rights reserved.
|
3 |
//
|
4 |
// Redistribution and use in source and binary forms, with or without
|
5 |
// modification, are permitted provided that the following conditions are
|
6 |
// met:
|
7 |
//
|
8 |
// * Redistributions of source code must retain the above copyright
|
9 |
// notice, this list of conditions and the following disclaimer.
|
10 |
// * Redistributions in binary form must reproduce the above
|
11 |
// copyright notice, this list of conditions and the following disclaimer
|
12 |
// in the documentation and/or other materials provided with the
|
13 |
// distribution.
|
14 |
// * Neither the name of Google Inc. nor the names of its
|
15 |
// contributors may be used to endorse or promote products derived from
|
16 |
// this software without specific prior written permission.
|
17 |
//
|
18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29 |
//
|
30 |
// Author: Sanjay Ghemawat
|
31 |
|
32 |
#include <stdlib.h>
|
33 |
#include <stdio.h>
|
34 |
#include <ctype.h>
|
35 |
#include <limits.h> /* for SHRT_MIN, USHRT_MAX, etc */
|
36 |
#include <assert.h>
|
37 |
#include <errno.h>
|
38 |
#include <string>
|
39 |
#include "config.h"
|
40 |
// We need this to compile the proper dll on windows/msys. This is copied
|
41 |
// from pcre_internal.h. It would probably be better just to include that.
|
42 |
#define PCRE_DEFINITION /* Win32 __declspec(export) trigger for .dll */
|
43 |
#include "pcre.h"
|
44 |
#include "pcre_stringpiece.h"
|
45 |
#include "pcrecpp.h"
|
46 |
|
47 |
|
48 |
namespace pcrecpp {
|
49 |
|
50 |
// Maximum number of args we can set
|
51 |
static const int kMaxArgs = 16;
|
52 |
static const int kVecSize = (1 + kMaxArgs) * 3; // results + PCRE workspace
|
53 |
|
54 |
// Special object that stands-in for no argument
|
55 |
Arg no_arg((void*)NULL);
|
56 |
|
57 |
// If a regular expression has no error, its error_ field points here
|
58 |
static const string empty_string;
|
59 |
|
60 |
// If the user doesn't ask for any options, we just use this one
|
61 |
static RE_Options default_options;
|
62 |
|
63 |
void RE::Init(const char* pat, const RE_Options* options) {
|
64 |
pattern_ = pat;
|
65 |
if (options == NULL) {
|
66 |
options_ = default_options;
|
67 |
} else {
|
68 |
options_ = *options;
|
69 |
}
|
70 |
error_ = &empty_string;
|
71 |
re_full_ = NULL;
|
72 |
re_partial_ = NULL;
|
73 |
|
74 |
re_partial_ = Compile(UNANCHORED);
|
75 |
if (re_partial_ != NULL) {
|
76 |
// Check for complicated patterns. The following change is
|
77 |
// conservative in that it may treat some "simple" patterns
|
78 |
// as "complex" (e.g., if the vertical bar is in a character
|
79 |
// class or is escaped). But it seems good enough.
|
80 |
if (strchr(pat, '|') == NULL) {
|
81 |
// Simple pattern: we can use position-based checks to perform
|
82 |
// fully anchored matches
|
83 |
re_full_ = re_partial_;
|
84 |
} else {
|
85 |
// We need a special pattern for anchored matches
|
86 |
re_full_ = Compile(ANCHOR_BOTH);
|
87 |
}
|
88 |
}
|
89 |
}
|
90 |
|
91 |
RE::~RE() {
|
92 |
if (re_full_ != NULL && re_full_ != re_partial_) (*pcre_free)(re_full_);
|
93 |
if (re_partial_ != NULL) (*pcre_free)(re_partial_);
|
94 |
if (error_ != &empty_string) delete error_;
|
95 |
}
|
96 |
|
97 |
pcre* RE::Compile(Anchor anchor) {
|
98 |
// First, convert RE_Options into pcre options
|
99 |
int pcre_options = 0;
|
100 |
if (options_.utf8())
|
101 |
pcre_options |= PCRE_UTF8;
|
102 |
|
103 |
// Special treatment for anchoring. This is needed because at
|
104 |
// runtime pcre only provides an option for anchoring at the
|
105 |
// beginning of a string (unless you use offset).
|
106 |
//
|
107 |
// There are three types of anchoring we want:
|
108 |
// UNANCHORED Compile the original pattern, and use
|
109 |
// a pcre unanchored match.
|
110 |
// ANCHOR_START Compile the original pattern, and use
|
111 |
// a pcre anchored match.
|
112 |
// ANCHOR_BOTH Tack a "\z" to the end of the original pattern
|
113 |
// and use a pcre anchored match.
|
114 |
|
115 |
const char* compile_error;
|
116 |
int eoffset;
|
117 |
pcre* re;
|
118 |
if (anchor != ANCHOR_BOTH) {
|
119 |
re = pcre_compile(pattern_.c_str(), pcre_options,
|
120 |
&compile_error, &eoffset, NULL);
|
121 |
} else {
|
122 |
// Tack a '\z' at the end of RE. Parenthesize it first so that
|
123 |
// the '\z' applies to all top-level alternatives in the regexp.
|
124 |
string wrapped = "(?:"; // A non-counting grouping operator
|
125 |
wrapped += pattern_;
|
126 |
wrapped += ")\\z";
|
127 |
re = pcre_compile(wrapped.c_str(), pcre_options,
|
128 |
&compile_error, &eoffset, NULL);
|
129 |
}
|
130 |
if (re == NULL) {
|
131 |
if (error_ == &empty_string) error_ = new string(compile_error);
|
132 |
}
|
133 |
return re;
|
134 |
}
|
135 |
|
136 |
/***** Matching interfaces *****/
|
137 |
|
138 |
bool RE::FullMatch(const StringPiece& text,
|
139 |
const Arg& ptr1,
|
140 |
const Arg& ptr2,
|
141 |
const Arg& ptr3,
|
142 |
const Arg& ptr4,
|
143 |
const Arg& ptr5,
|
144 |
const Arg& ptr6,
|
145 |
const Arg& ptr7,
|
146 |
const Arg& ptr8,
|
147 |
const Arg& ptr9,
|
148 |
const Arg& ptr10,
|
149 |
const Arg& ptr11,
|
150 |
const Arg& ptr12,
|
151 |
const Arg& ptr13,
|
152 |
const Arg& ptr14,
|
153 |
const Arg& ptr15,
|
154 |
const Arg& ptr16) const {
|
155 |
const Arg* args[kMaxArgs];
|
156 |
int n = 0;
|
157 |
if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
|
158 |
if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
|
159 |
if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
|
160 |
if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
|
161 |
if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
|
162 |
if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
|
163 |
if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
|
164 |
if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
|
165 |
if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
|
166 |
if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
|
167 |
if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
|
168 |
if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
|
169 |
if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
|
170 |
if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
|
171 |
if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
|
172 |
if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
|
173 |
done:
|
174 |
|
175 |
int consumed;
|
176 |
int vec[kVecSize];
|
177 |
return DoMatchImpl(text, ANCHOR_BOTH, &consumed, args, n, vec, kVecSize);
|
178 |
}
|
179 |
|
180 |
bool RE::PartialMatch(const StringPiece& text,
|
181 |
const Arg& ptr1,
|
182 |
const Arg& ptr2,
|
183 |
const Arg& ptr3,
|
184 |
const Arg& ptr4,
|
185 |
const Arg& ptr5,
|
186 |
const Arg& ptr6,
|
187 |
const Arg& ptr7,
|
188 |
const Arg& ptr8,
|
189 |
const Arg& ptr9,
|
190 |
const Arg& ptr10,
|
191 |
const Arg& ptr11,
|
192 |
const Arg& ptr12,
|
193 |
const Arg& ptr13,
|
194 |
const Arg& ptr14,
|
195 |
const Arg& ptr15,
|
196 |
const Arg& ptr16) const {
|
197 |
const Arg* args[kMaxArgs];
|
198 |
int n = 0;
|
199 |
if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
|
200 |
if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
|
201 |
if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
|
202 |
if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
|
203 |
if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
|
204 |
if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
|
205 |
if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
|
206 |
if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
|
207 |
if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
|
208 |
if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
|
209 |
if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
|
210 |
if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
|
211 |
if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
|
212 |
if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
|
213 |
if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
|
214 |
if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
|
215 |
done:
|
216 |
|
217 |
int consumed;
|
218 |
int vec[kVecSize];
|
219 |
return DoMatchImpl(text, UNANCHORED, &consumed, args, n, vec, kVecSize);
|
220 |
}
|
221 |
|
222 |
bool RE::Consume(StringPiece* input,
|
223 |
const Arg& ptr1,
|
224 |
const Arg& ptr2,
|
225 |
const Arg& ptr3,
|
226 |
const Arg& ptr4,
|
227 |
const Arg& ptr5,
|
228 |
const Arg& ptr6,
|
229 |
const Arg& ptr7,
|
230 |
const Arg& ptr8,
|
231 |
const Arg& ptr9,
|
232 |
const Arg& ptr10,
|
233 |
const Arg& ptr11,
|
234 |
const Arg& ptr12,
|
235 |
const Arg& ptr13,
|
236 |
const Arg& ptr14,
|
237 |
const Arg& ptr15,
|
238 |
const Arg& ptr16) const {
|
239 |
const Arg* args[kMaxArgs];
|
240 |
int n = 0;
|
241 |
if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
|
242 |
if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
|
243 |
if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
|
244 |
if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
|
245 |
if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
|
246 |
if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
|
247 |
if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
|
248 |
if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
|
249 |
if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
|
250 |
if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
|
251 |
if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
|
252 |
if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
|
253 |
if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
|
254 |
if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
|
255 |
if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
|
256 |
if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
|
257 |
done:
|
258 |
|
259 |
int consumed;
|
260 |
int vec[kVecSize];
|
261 |
if (DoMatchImpl(*input, ANCHOR_START, &consumed,
|
262 |
args, n, vec, kVecSize)) {
|
263 |
input->remove_prefix(consumed);
|
264 |
return true;
|
265 |
} else {
|
266 |
return false;
|
267 |
}
|
268 |
}
|
269 |
|
270 |
bool RE::FindAndConsume(StringPiece* input,
|
271 |
const Arg& ptr1,
|
272 |
const Arg& ptr2,
|
273 |
const Arg& ptr3,
|
274 |
const Arg& ptr4,
|
275 |
const Arg& ptr5,
|
276 |
const Arg& ptr6,
|
277 |
const Arg& ptr7,
|
278 |
const Arg& ptr8,
|
279 |
const Arg& ptr9,
|
280 |
const Arg& ptr10,
|
281 |
const Arg& ptr11,
|
282 |
const Arg& ptr12,
|
283 |
const Arg& ptr13,
|
284 |
const Arg& ptr14,
|
285 |
const Arg& ptr15,
|
286 |
const Arg& ptr16) const {
|
287 |
const Arg* args[kMaxArgs];
|
288 |
int n = 0;
|
289 |
if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
|
290 |
if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
|
291 |
if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
|
292 |
if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
|
293 |
if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
|
294 |
if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
|
295 |
if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
|
296 |
if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
|
297 |
if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
|
298 |
if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
|
299 |
if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
|
300 |
if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
|
301 |
if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
|
302 |
if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
|
303 |
if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
|
304 |
if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
|
305 |
done:
|
306 |
|
307 |
int consumed;
|
308 |
int vec[kVecSize];
|
309 |
if (DoMatchImpl(*input, UNANCHORED, &consumed,
|
310 |
args, n, vec, kVecSize)) {
|
311 |
input->remove_prefix(consumed);
|
312 |
return true;
|
313 |
} else {
|
314 |
return false;
|
315 |
}
|
316 |
}
|
317 |
|
318 |
bool RE::Replace(const StringPiece& rewrite,
|
319 |
string *str) const {
|
320 |
int vec[kVecSize];
|
321 |
int matches = TryMatch(*str, 0, UNANCHORED, vec, kVecSize);
|
322 |
if (matches == 0)
|
323 |
return false;
|
324 |
|
325 |
string s;
|
326 |
if (!Rewrite(&s, rewrite, *str, vec, matches))
|
327 |
return false;
|
328 |
|
329 |
assert(vec[0] >= 0);
|
330 |
assert(vec[1] >= 0);
|
331 |
str->replace(vec[0], vec[1] - vec[0], s);
|
332 |
return true;
|
333 |
}
|
334 |
|
335 |
int RE::GlobalReplace(const StringPiece& rewrite,
|
336 |
string *str) const {
|
337 |
int count = 0;
|
338 |
int vec[kVecSize];
|
339 |
string out;
|
340 |
int start = 0;
|
341 |
int lastend = -1;
|
342 |
|
343 |
for (; start <= static_cast<int>(str->length()); count++) {
|
344 |
int matches = TryMatch(*str, start, UNANCHORED, vec, kVecSize);
|
345 |
if (matches <= 0)
|
346 |
break;
|
347 |
int matchstart = vec[0], matchend = vec[1];
|
348 |
assert(matchstart >= start);
|
349 |
assert(matchend >= matchstart);
|
350 |
if (matchstart == matchend && matchstart == lastend) {
|
351 |
// advance one character if we matched an empty string at the same
|
352 |
// place as the last match occurred
|
353 |
if (start < static_cast<int>(str->length()))
|
354 |
out.push_back((*str)[start]);
|
355 |
start++;
|
356 |
} else {
|
357 |
out.append(*str, start, matchstart - start);
|
358 |
Rewrite(&out, rewrite, *str, vec, matches);
|
359 |
start = matchend;
|
360 |
lastend = matchend;
|
361 |
count++;
|
362 |
}
|
363 |
}
|
364 |
|
365 |
if (count == 0)
|
366 |
return 0;
|
367 |
|
368 |
if (start < static_cast<int>(str->length()))
|
369 |
out.append(*str, start, str->length() - start);
|
370 |
swap(out, *str);
|
371 |
return count;
|
372 |
}
|
373 |
|
374 |
bool RE::Extract(const StringPiece& rewrite,
|
375 |
const StringPiece& text,
|
376 |
string *out) const {
|
377 |
int vec[kVecSize];
|
378 |
int matches = TryMatch(text, 0, UNANCHORED, vec, kVecSize);
|
379 |
if (matches == 0)
|
380 |
return false;
|
381 |
out->clear();
|
382 |
return Rewrite(out, rewrite, text, vec, matches);
|
383 |
}
|
384 |
|
385 |
/***** Actual matching and rewriting code *****/
|
386 |
|
387 |
int RE::TryMatch(const StringPiece& text,
|
388 |
int startpos,
|
389 |
Anchor anchor,
|
390 |
int *vec,
|
391 |
int vecsize) const {
|
392 |
pcre* re = (anchor == ANCHOR_BOTH) ? re_full_ : re_partial_;
|
393 |
if (re == NULL) {
|
394 |
//fprintf(stderr, "Matching against invalid re: %s\n", error_->c_str());
|
395 |
return 0;
|
396 |
}
|
397 |
|
398 |
pcre_extra extra = { 0 };
|
399 |
if (options_.match_limit() > 0) {
|
400 |
extra.flags = PCRE_EXTRA_MATCH_LIMIT;
|
401 |
extra.match_limit = options_.match_limit();
|
402 |
}
|
403 |
int rc = pcre_exec(re, // The regular expression object
|
404 |
&extra,
|
405 |
text.data(),
|
406 |
text.size(),
|
407 |
startpos,
|
408 |
(anchor == UNANCHORED) ? 0 : PCRE_ANCHORED,
|
409 |
vec,
|
410 |
vecsize);
|
411 |
|
412 |
// Handle errors
|
413 |
if (rc == PCRE_ERROR_NOMATCH) {
|
414 |
return 0;
|
415 |
} else if (rc < 0) {
|
416 |
//fprintf(stderr, "Unexpected return code: %d when matching '%s'\n",
|
417 |
// re, pattern_.c_str());
|
418 |
return 0;
|
419 |
} else if (rc == 0) {
|
420 |
// pcre_exec() returns 0 as a special case when the number of
|
421 |
// capturing subpatterns exceeds the size of the vector.
|
422 |
// When this happens, there is a match and the output vector
|
423 |
// is filled, but we miss out on the positions of the extra subpatterns.
|
424 |
rc = vecsize / 2;
|
425 |
}
|
426 |
|
427 |
if ((anchor == ANCHOR_BOTH) && (re_full_ == re_partial_)) {
|
428 |
// We need an extra check to make sure that the match extended
|
429 |
// to the end of the input string
|
430 |
assert(vec[0] == 0); // PCRE_ANCHORED forces starting match
|
431 |
if (vec[1] != text.size()) return 0; // Did not get ending match
|
432 |
}
|
433 |
|
434 |
return rc;
|
435 |
}
|
436 |
|
437 |
bool RE::DoMatchImpl(const StringPiece& text,
|
438 |
Anchor anchor,
|
439 |
int* consumed,
|
440 |
const Arg* const* args,
|
441 |
int n,
|
442 |
int* vec,
|
443 |
int vecsize) const {
|
444 |
assert((1 + n) * 3 <= vecsize); // results + PCRE workspace
|
445 |
int matches = TryMatch(text, 0, anchor, vec, vecsize);
|
446 |
assert(matches >= 0); // TryMatch never returns negatives
|
447 |
if (matches == 0)
|
448 |
return false;
|
449 |
|
450 |
*consumed = vec[1];
|
451 |
|
452 |
if (args == NULL) {
|
453 |
// We are not interested in results
|
454 |
return true;
|
455 |
}
|
456 |
|
457 |
// If we got here, we must have matched the whole pattern.
|
458 |
// We do not need (can not do) any more checks on the value of 'matches' here
|
459 |
// -- see the comment for TryMatch.
|
460 |
for (int i = 0; i < n; i++) {
|
461 |
const int start = vec[2*(i+1)];
|
462 |
const int limit = vec[2*(i+1)+1];
|
463 |
if (!args[i]->Parse(text.data() + start, limit-start)) {
|
464 |
// TODO: Should we indicate what the error was?
|
465 |
return false;
|
466 |
}
|
467 |
}
|
468 |
|
469 |
return true;
|
470 |
}
|
471 |
|
472 |
bool RE::DoMatch(const StringPiece& text,
|
473 |
Anchor anchor,
|
474 |
int* consumed,
|
475 |
const Arg* const args[],
|
476 |
int n) const {
|
477 |
assert(n >= 0);
|
478 |
size_t const vecsize = (1 + n) * 3; // results + PCRE workspace
|
479 |
// (as for kVecSize)
|
480 |
int space[21]; // use stack allocation for small vecsize (common case)
|
481 |
int* vec = vecsize <= 21 ? space : new int[vecsize];
|
482 |
bool retval = DoMatchImpl(text, anchor, consumed, args, n, vec, vecsize);
|
483 |
if (vec != space) delete [] vec;
|
484 |
return retval;
|
485 |
}
|
486 |
|
487 |
bool RE::Rewrite(string *out, const StringPiece &rewrite,
|
488 |
const StringPiece &text, int *vec, int veclen) const {
|
489 |
for (const char *s = rewrite.data(), *end = s + rewrite.size();
|
490 |
s < end; s++) {
|
491 |
int c = *s;
|
492 |
if (c == '\\') {
|
493 |
c = *++s;
|
494 |
if (isdigit(c)) {
|
495 |
int n = (c - '0');
|
496 |
if (n >= veclen) {
|
497 |
//fprintf(stderr, requested group %d in regexp %.*s\n",
|
498 |
// n, rewrite.size(), rewrite.data());
|
499 |
return false;
|
500 |
}
|
501 |
int start = vec[2 * n];
|
502 |
if (start >= 0)
|
503 |
out->append(text.data() + start, vec[2 * n + 1] - start);
|
504 |
} else if (c == '\\') {
|
505 |
out->push_back('\\');
|
506 |
} else {
|
507 |
//fprintf(stderr, "invalid rewrite pattern: %.*s\n",
|
508 |
// rewrite.size(), rewrite.data());
|
509 |
return false;
|
510 |
}
|
511 |
} else {
|
512 |
out->push_back(c);
|
513 |
}
|
514 |
}
|
515 |
return true;
|
516 |
}
|
517 |
|
518 |
// Return the number of capturing subpatterns, or -1 if the
|
519 |
// regexp wasn't valid on construction.
|
520 |
int RE::NumberOfCapturingGroups() {
|
521 |
if (re_partial_ == NULL) return -1;
|
522 |
|
523 |
int result;
|
524 |
int pcre_retval = pcre_fullinfo(re_partial_, // The regular expression object
|
525 |
NULL, // We did not study the pattern
|
526 |
PCRE_INFO_CAPTURECOUNT,
|
527 |
&result);
|
528 |
assert(pcre_retval == 0);
|
529 |
return result;
|
530 |
}
|
531 |
|
532 |
/***** Parsers for various types *****/
|
533 |
|
534 |
bool Arg::parse_null(const char* str, int n, void* dest) {
|
535 |
// We fail if somebody asked us to store into a non-NULL void* pointer
|
536 |
return (dest == NULL);
|
537 |
}
|
538 |
|
539 |
bool Arg::parse_string(const char* str, int n, void* dest) {
|
540 |
reinterpret_cast<string*>(dest)->assign(str, n);
|
541 |
return true;
|
542 |
}
|
543 |
|
544 |
bool Arg::parse_stringpiece(const char* str, int n, void* dest) {
|
545 |
reinterpret_cast<StringPiece*>(dest)->set(str, n);
|
546 |
return true;
|
547 |
}
|
548 |
|
549 |
bool Arg::parse_char(const char* str, int n, void* dest) {
|
550 |
if (n != 1) return false;
|
551 |
*(reinterpret_cast<char*>(dest)) = str[0];
|
552 |
return true;
|
553 |
}
|
554 |
|
555 |
bool Arg::parse_uchar(const char* str, int n, void* dest) {
|
556 |
if (n != 1) return false;
|
557 |
*(reinterpret_cast<unsigned char*>(dest)) = str[0];
|
558 |
return true;
|
559 |
}
|
560 |
|
561 |
// Largest number spec that we are willing to parse
|
562 |
static const int kMaxNumberLength = 32;
|
563 |
|
564 |
// REQUIRES "buf" must have length at least kMaxNumberLength+1
|
565 |
// REQUIRES "n > 0"
|
566 |
// Copies "str" into "buf" and null-terminates if necessary.
|
567 |
// Returns one of:
|
568 |
// a. "str" if no termination is needed
|
569 |
// b. "buf" if the string was copied and null-terminated
|
570 |
// c. "" if the input was invalid and has no hope of being parsed
|
571 |
static const char* TerminateNumber(char* buf, const char* str, int n) {
|
572 |
if ((n > 0) && isspace(*str)) {
|
573 |
// We are less forgiving than the strtoxxx() routines and do not
|
574 |
// allow leading spaces.
|
575 |
return "";
|
576 |
}
|
577 |
|
578 |
// See if the character right after the input text may potentially
|
579 |
// look like a digit.
|
580 |
if (isdigit(str[n]) ||
|
581 |
((str[n] >= 'a') && (str[n] <= 'f')) ||
|
582 |
((str[n] >= 'A') && (str[n] <= 'F'))) {
|
583 |
if (n > kMaxNumberLength) return ""; // Input too big to be a valid number
|
584 |
memcpy(buf, str, n);
|
585 |
buf[n] = '\0';
|
586 |
return buf;
|
587 |
} else {
|
588 |
// We can parse right out of the supplied string, so return it.
|
589 |
return str;
|
590 |
}
|
591 |
}
|
592 |
|
593 |
bool Arg::parse_long_radix(const char* str,
|
594 |
int n,
|
595 |
void* dest,
|
596 |
int radix) {
|
597 |
if (n == 0) return false;
|
598 |
char buf[kMaxNumberLength+1];
|
599 |
str = TerminateNumber(buf, str, n);
|
600 |
char* end;
|
601 |
errno = 0;
|
602 |
long r = strtol(str, &end, radix);
|
603 |
if (end != str + n) return false; // Leftover junk
|
604 |
if (errno) return false;
|
605 |
*(reinterpret_cast<long*>(dest)) = r;
|
606 |
return true;
|
607 |
}
|
608 |
|
609 |
bool Arg::parse_ulong_radix(const char* str,
|
610 |
int n,
|
611 |
void* dest,
|
612 |
int radix) {
|
613 |
if (n == 0) return false;
|
614 |
char buf[kMaxNumberLength+1];
|
615 |
str = TerminateNumber(buf, str, n);
|
616 |
char* end;
|
617 |
errno = 0;
|
618 |
unsigned long r = strtoul(str, &end, radix);
|
619 |
if (end != str + n) return false; // Leftover junk
|
620 |
if (errno) return false;
|
621 |
*(reinterpret_cast<unsigned long*>(dest)) = r;
|
622 |
return true;
|
623 |
}
|
624 |
|
625 |
bool Arg::parse_short_radix(const char* str,
|
626 |
int n,
|
627 |
void* dest,
|
628 |
int radix) {
|
629 |
long r;
|
630 |
if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
|
631 |
if (r < SHRT_MIN || r > SHRT_MAX) return false; // Out of range
|
632 |
*(reinterpret_cast<short*>(dest)) = r;
|
633 |
return true;
|
634 |
}
|
635 |
|
636 |
bool Arg::parse_ushort_radix(const char* str,
|
637 |
int n,
|
638 |
void* dest,
|
639 |
int radix) {
|
640 |
unsigned long r;
|
641 |
if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
|
642 |
if (r > USHRT_MAX) return false; // Out of range
|
643 |
*(reinterpret_cast<unsigned short*>(dest)) = r;
|
644 |
return true;
|
645 |
}
|
646 |
|
647 |
bool Arg::parse_int_radix(const char* str,
|
648 |
int n,
|
649 |
void* dest,
|
650 |
int radix) {
|
651 |
long r;
|
652 |
if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
|
653 |
if (r < INT_MIN || r > INT_MAX) return false; // Out of range
|
654 |
*(reinterpret_cast<int*>(dest)) = r;
|
655 |
return true;
|
656 |
}
|
657 |
|
658 |
bool Arg::parse_uint_radix(const char* str,
|
659 |
int n,
|
660 |
void* dest,
|
661 |
int radix) {
|
662 |
unsigned long r;
|
663 |
if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
|
664 |
if (r > UINT_MAX) return false; // Out of range
|
665 |
*(reinterpret_cast<unsigned int*>(dest)) = r;
|
666 |
return true;
|
667 |
}
|
668 |
|
669 |
bool Arg::parse_longlong_radix(const char* str,
|
670 |
int n,
|
671 |
void* dest,
|
672 |
int radix) {
|
673 |
#ifndef HAVE_LONG_LONG
|
674 |
return false;
|
675 |
#else
|
676 |
if (n == 0) return false;
|
677 |
char buf[kMaxNumberLength+1];
|
678 |
str = TerminateNumber(buf, str, n);
|
679 |
char* end;
|
680 |
errno = 0;
|
681 |
#if defined HAVE_STRTOQ
|
682 |
long long r = strtoq(str, &end, radix);
|
683 |
#elif defined HAVE_STRTOLL
|
684 |
long long r = strtoll(str, &end, radix);
|
685 |
#else
|
686 |
#error parse_longlong_radix: cannot convert input to a long-long
|
687 |
#endif
|
688 |
if (end != str + n) return false; // Leftover junk
|
689 |
if (errno) return false;
|
690 |
*(reinterpret_cast<long long*>(dest)) = r;
|
691 |
return true;
|
692 |
#endif /* HAVE_LONG_LONG */
|
693 |
}
|
694 |
|
695 |
bool Arg::parse_ulonglong_radix(const char* str,
|
696 |
int n,
|
697 |
void* dest,
|
698 |
int radix) {
|
699 |
#ifndef HAVE_UNSIGNED_LONG_LONG
|
700 |
return false;
|
701 |
#else
|
702 |
if (n == 0) return false;
|
703 |
char buf[kMaxNumberLength+1];
|
704 |
str = TerminateNumber(buf, str, n);
|
705 |
char* end;
|
706 |
errno = 0;
|
707 |
#if defined HAVE_STRTOQ
|
708 |
unsigned long long r = strtouq(str, &end, radix);
|
709 |
#elif defined HAVE_STRTOLL
|
710 |
unsigned long long r = strtoull(str, &end, radix);
|
711 |
#else
|
712 |
#error parse_ulonglong_radix: cannot convert input to a long-long
|
713 |
#endif
|
714 |
if (end != str + n) return false; // Leftover junk
|
715 |
if (errno) return false;
|
716 |
*(reinterpret_cast<unsigned long long*>(dest)) = r;
|
717 |
return true;
|
718 |
#endif /* HAVE_UNSIGNED_LONG_LONG */
|
719 |
}
|
720 |
|
721 |
bool Arg::parse_double(const char* str, int n, void* dest) {
|
722 |
if (n == 0) return false;
|
723 |
static const int kMaxLength = 200;
|
724 |
char buf[kMaxLength];
|
725 |
if (n >= kMaxLength) return false;
|
726 |
memcpy(buf, str, n);
|
727 |
buf[n] = '\0';
|
728 |
errno = 0;
|
729 |
char* end;
|
730 |
double r = strtod(buf, &end);
|
731 |
if (end != buf + n) return false; // Leftover junk
|
732 |
if (errno) return false;
|
733 |
*(reinterpret_cast<double*>(dest)) = r;
|
734 |
return true;
|
735 |
}
|
736 |
|
737 |
bool Arg::parse_float(const char* str, int n, void* dest) {
|
738 |
double r;
|
739 |
if (!parse_double(str, n, &r)) return false;
|
740 |
*(reinterpret_cast<float*>(dest)) = static_cast<float>(r);
|
741 |
return true;
|
742 |
}
|
743 |
|
744 |
|
745 |
#define DEFINE_INTEGER_PARSERS(name) \
|
746 |
bool Arg::parse_##name(const char* str, int n, void* dest) { \
|
747 |
return parse_##name##_radix(str, n, dest, 10); \
|
748 |
} \
|
749 |
bool Arg::parse_##name##_hex(const char* str, int n, void* dest) { \
|
750 |
return parse_##name##_radix(str, n, dest, 16); \
|
751 |
} \
|
752 |
bool Arg::parse_##name##_octal(const char* str, int n, void* dest) { \
|
753 |
return parse_##name##_radix(str, n, dest, 8); \
|
754 |
} \
|
755 |
bool Arg::parse_##name##_cradix(const char* str, int n, void* dest) { \
|
756 |
return parse_##name##_radix(str, n, dest, 0); \
|
757 |
}
|
758 |
|
759 |
DEFINE_INTEGER_PARSERS(short);
|
760 |
DEFINE_INTEGER_PARSERS(ushort);
|
761 |
DEFINE_INTEGER_PARSERS(int);
|
762 |
DEFINE_INTEGER_PARSERS(uint);
|
763 |
DEFINE_INTEGER_PARSERS(long);
|
764 |
DEFINE_INTEGER_PARSERS(ulong);
|
765 |
DEFINE_INTEGER_PARSERS(longlong);
|
766 |
DEFINE_INTEGER_PARSERS(ulonglong);
|
767 |
|
768 |
#undef DEFINE_INTEGER_PARSERS
|
769 |
|
770 |
} // namespace pcrecpp
|