/[pcre]/code/trunk/pcrecpp.cc
ViewVC logotype

Contents of /code/trunk/pcrecpp.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 326 - (show annotations)
Sat Mar 8 17:24:02 2008 UTC (7 years, 3 months ago) by ph10
File size: 30950 byte(s)
Error occurred while calculating annotation data.
Craig's patch to the QuoteMeta function in pcrecpp.cc so that it escapes the
NUL character as backslash + 0 rather than backslash + NUL, because PCRE
doesn't support NULs in patterns.
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 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <ctype.h>
39 #include <limits.h> /* for SHRT_MIN, USHRT_MAX, etc */
40 #include <assert.h>
41 #include <errno.h>
42 #include <string>
43 #include <algorithm>
44
45 #include "pcrecpp_internal.h"
46 #include "pcre.h"
47 #include "pcrecpp.h"
48 #include "pcre_stringpiece.h"
49
50
51 namespace pcrecpp {
52
53 // Maximum number of args we can set
54 static const int kMaxArgs = 16;
55 static const int kVecSize = (1 + kMaxArgs) * 3; // results + PCRE workspace
56
57 // Special object that stands-in for no argument
58 Arg RE::no_arg((void*)NULL);
59
60 // This is for ABI compatibility with old versions of pcre (pre-7.6),
61 // which defined a global no_arg variable instead of putting it in the
62 // RE class. This works on GCC >= 3, at least. We could probably have
63 // a more inclusive test if we ever needed it.
64 #if defined(__GNUC__) && __GNUC__ >= 3
65 extern Arg no_arg __attribute__((alias("_ZN7pcrecpp2RE6no_argE")));
66 #endif
67
68 // If a regular expression has no error, its error_ field points here
69 static const string empty_string;
70
71 // If the user doesn't ask for any options, we just use this one
72 static RE_Options default_options;
73
74 void RE::Init(const string& pat, const RE_Options* options) {
75 pattern_ = pat;
76 if (options == NULL) {
77 options_ = default_options;
78 } else {
79 options_ = *options;
80 }
81 error_ = &empty_string;
82 re_full_ = NULL;
83 re_partial_ = NULL;
84
85 re_partial_ = Compile(UNANCHORED);
86 if (re_partial_ != NULL) {
87 re_full_ = Compile(ANCHOR_BOTH);
88 }
89 }
90
91 void RE::Cleanup() {
92 if (re_full_ != NULL) (*pcre_free)(re_full_);
93 if (re_partial_ != NULL) (*pcre_free)(re_partial_);
94 if (error_ != &empty_string) delete error_;
95 }
96
97
98 RE::~RE() {
99 Cleanup();
100 }
101
102
103 pcre* RE::Compile(Anchor anchor) {
104 // First, convert RE_Options into pcre options
105 int pcre_options = 0;
106 pcre_options = options_.all_options();
107
108 // Special treatment for anchoring. This is needed because at
109 // runtime pcre only provides an option for anchoring at the
110 // beginning of a string (unless you use offset).
111 //
112 // There are three types of anchoring we want:
113 // UNANCHORED Compile the original pattern, and use
114 // a pcre unanchored match.
115 // ANCHOR_START Compile the original pattern, and use
116 // a pcre anchored match.
117 // ANCHOR_BOTH Tack a "\z" to the end of the original pattern
118 // and use a pcre anchored match.
119
120 const char* compile_error;
121 int eoffset;
122 pcre* re;
123 if (anchor != ANCHOR_BOTH) {
124 re = pcre_compile(pattern_.c_str(), pcre_options,
125 &compile_error, &eoffset, NULL);
126 } else {
127 // Tack a '\z' at the end of RE. Parenthesize it first so that
128 // the '\z' applies to all top-level alternatives in the regexp.
129 string wrapped = "(?:"; // A non-counting grouping operator
130 wrapped += pattern_;
131 wrapped += ")\\z";
132 re = pcre_compile(wrapped.c_str(), pcre_options,
133 &compile_error, &eoffset, NULL);
134 }
135 if (re == NULL) {
136 if (error_ == &empty_string) error_ = new string(compile_error);
137 }
138 return re;
139 }
140
141 /***** Matching interfaces *****/
142
143 bool RE::FullMatch(const StringPiece& text,
144 const Arg& ptr1,
145 const Arg& ptr2,
146 const Arg& ptr3,
147 const Arg& ptr4,
148 const Arg& ptr5,
149 const Arg& ptr6,
150 const Arg& ptr7,
151 const Arg& ptr8,
152 const Arg& ptr9,
153 const Arg& ptr10,
154 const Arg& ptr11,
155 const Arg& ptr12,
156 const Arg& ptr13,
157 const Arg& ptr14,
158 const Arg& ptr15,
159 const Arg& ptr16) const {
160 const Arg* args[kMaxArgs];
161 int n = 0;
162 if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
163 if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
164 if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
165 if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
166 if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
167 if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
168 if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
169 if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
170 if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
171 if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
172 if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
173 if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
174 if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
175 if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
176 if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
177 if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
178 done:
179
180 int consumed;
181 int vec[kVecSize];
182 return DoMatchImpl(text, ANCHOR_BOTH, &consumed, args, n, vec, kVecSize);
183 }
184
185 bool RE::PartialMatch(const StringPiece& text,
186 const Arg& ptr1,
187 const Arg& ptr2,
188 const Arg& ptr3,
189 const Arg& ptr4,
190 const Arg& ptr5,
191 const Arg& ptr6,
192 const Arg& ptr7,
193 const Arg& ptr8,
194 const Arg& ptr9,
195 const Arg& ptr10,
196 const Arg& ptr11,
197 const Arg& ptr12,
198 const Arg& ptr13,
199 const Arg& ptr14,
200 const Arg& ptr15,
201 const Arg& ptr16) const {
202 const Arg* args[kMaxArgs];
203 int n = 0;
204 if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
205 if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
206 if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
207 if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
208 if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
209 if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
210 if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
211 if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
212 if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
213 if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
214 if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
215 if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
216 if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
217 if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
218 if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
219 if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
220 done:
221
222 int consumed;
223 int vec[kVecSize];
224 return DoMatchImpl(text, UNANCHORED, &consumed, args, n, vec, kVecSize);
225 }
226
227 bool RE::Consume(StringPiece* input,
228 const Arg& ptr1,
229 const Arg& ptr2,
230 const Arg& ptr3,
231 const Arg& ptr4,
232 const Arg& ptr5,
233 const Arg& ptr6,
234 const Arg& ptr7,
235 const Arg& ptr8,
236 const Arg& ptr9,
237 const Arg& ptr10,
238 const Arg& ptr11,
239 const Arg& ptr12,
240 const Arg& ptr13,
241 const Arg& ptr14,
242 const Arg& ptr15,
243 const Arg& ptr16) const {
244 const Arg* args[kMaxArgs];
245 int n = 0;
246 if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
247 if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
248 if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
249 if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
250 if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
251 if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
252 if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
253 if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
254 if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
255 if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
256 if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
257 if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
258 if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
259 if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
260 if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
261 if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
262 done:
263
264 int consumed;
265 int vec[kVecSize];
266 if (DoMatchImpl(*input, ANCHOR_START, &consumed,
267 args, n, vec, kVecSize)) {
268 input->remove_prefix(consumed);
269 return true;
270 } else {
271 return false;
272 }
273 }
274
275 bool RE::FindAndConsume(StringPiece* input,
276 const Arg& ptr1,
277 const Arg& ptr2,
278 const Arg& ptr3,
279 const Arg& ptr4,
280 const Arg& ptr5,
281 const Arg& ptr6,
282 const Arg& ptr7,
283 const Arg& ptr8,
284 const Arg& ptr9,
285 const Arg& ptr10,
286 const Arg& ptr11,
287 const Arg& ptr12,
288 const Arg& ptr13,
289 const Arg& ptr14,
290 const Arg& ptr15,
291 const Arg& ptr16) const {
292 const Arg* args[kMaxArgs];
293 int n = 0;
294 if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
295 if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
296 if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
297 if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
298 if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
299 if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
300 if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
301 if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
302 if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
303 if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
304 if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
305 if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
306 if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
307 if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
308 if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
309 if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
310 done:
311
312 int consumed;
313 int vec[kVecSize];
314 if (DoMatchImpl(*input, UNANCHORED, &consumed,
315 args, n, vec, kVecSize)) {
316 input->remove_prefix(consumed);
317 return true;
318 } else {
319 return false;
320 }
321 }
322
323 bool RE::Replace(const StringPiece& rewrite,
324 string *str) const {
325 int vec[kVecSize];
326 int matches = TryMatch(*str, 0, UNANCHORED, vec, kVecSize);
327 if (matches == 0)
328 return false;
329
330 string s;
331 if (!Rewrite(&s, rewrite, *str, vec, matches))
332 return false;
333
334 assert(vec[0] >= 0);
335 assert(vec[1] >= 0);
336 str->replace(vec[0], vec[1] - vec[0], s);
337 return true;
338 }
339
340 // Returns PCRE_NEWLINE_CRLF, PCRE_NEWLINE_CR, or PCRE_NEWLINE_LF.
341 // Note that PCRE_NEWLINE_CRLF is defined to be P_N_CR | P_N_LF.
342 // Modified by PH to add PCRE_NEWLINE_ANY and PCRE_NEWLINE_ANYCRLF.
343
344 static int NewlineMode(int pcre_options) {
345 // TODO: if we can make it threadsafe, cache this var
346 int newline_mode = 0;
347 /* if (newline_mode) return newline_mode; */ // do this once it's cached
348 if (pcre_options & (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|
349 PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF)) {
350 newline_mode = (pcre_options &
351 (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|
352 PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF));
353 } else {
354 int newline;
355 pcre_config(PCRE_CONFIG_NEWLINE, &newline);
356 if (newline == 10)
357 newline_mode = PCRE_NEWLINE_LF;
358 else if (newline == 13)
359 newline_mode = PCRE_NEWLINE_CR;
360 else if (newline == 3338)
361 newline_mode = PCRE_NEWLINE_CRLF;
362 else if (newline == -1)
363 newline_mode = PCRE_NEWLINE_ANY;
364 else if (newline == -2)
365 newline_mode = PCRE_NEWLINE_ANYCRLF;
366 else
367 assert("" == "Unexpected return value from pcre_config(NEWLINE)");
368 }
369 return newline_mode;
370 }
371
372 int RE::GlobalReplace(const StringPiece& rewrite,
373 string *str) const {
374 int count = 0;
375 int vec[kVecSize];
376 string out;
377 int start = 0;
378 int lastend = -1;
379
380 while (start <= static_cast<int>(str->length())) {
381 int matches = TryMatch(*str, start, UNANCHORED, vec, kVecSize);
382 if (matches <= 0)
383 break;
384 int matchstart = vec[0], matchend = vec[1];
385 assert(matchstart >= start);
386 assert(matchend >= matchstart);
387 if (matchstart == matchend && matchstart == lastend) {
388 // advance one character if we matched an empty string at the same
389 // place as the last match occurred
390 matchend = start + 1;
391 // If the current char is CR and we're in CRLF mode, skip LF too.
392 // Note it's better to call pcre_fullinfo() than to examine
393 // all_options(), since options_ could have changed bewteen
394 // compile-time and now, but this is simpler and safe enough.
395 // Modified by PH to add ANY and ANYCRLF.
396 if (start+1 < static_cast<int>(str->length()) &&
397 (*str)[start] == '\r' && (*str)[start+1] == '\n' &&
398 (NewlineMode(options_.all_options()) == PCRE_NEWLINE_CRLF ||
399 NewlineMode(options_.all_options()) == PCRE_NEWLINE_ANY ||
400 NewlineMode(options_.all_options()) == PCRE_NEWLINE_ANYCRLF)
401 ) {
402 matchend++;
403 }
404 // We also need to advance more than one char if we're in utf8 mode.
405 #ifdef SUPPORT_UTF8
406 if (options_.utf8()) {
407 while (matchend < static_cast<int>(str->length()) &&
408 ((*str)[matchend] & 0xc0) == 0x80)
409 matchend++;
410 }
411 #endif
412 if (matchend <= static_cast<int>(str->length()))
413 out.append(*str, start, matchend - start);
414 start = matchend;
415 } else {
416 out.append(*str, start, matchstart - start);
417 Rewrite(&out, rewrite, *str, vec, matches);
418 start = matchend;
419 lastend = matchend;
420 count++;
421 }
422 }
423
424 if (count == 0)
425 return 0;
426
427 if (start < static_cast<int>(str->length()))
428 out.append(*str, start, str->length() - start);
429 swap(out, *str);
430 return count;
431 }
432
433 bool RE::Extract(const StringPiece& rewrite,
434 const StringPiece& text,
435 string *out) const {
436 int vec[kVecSize];
437 int matches = TryMatch(text, 0, UNANCHORED, vec, kVecSize);
438 if (matches == 0)
439 return false;
440 out->erase();
441 return Rewrite(out, rewrite, text, vec, matches);
442 }
443
444 /*static*/ string RE::QuoteMeta(const StringPiece& unquoted) {
445 string result;
446
447 // Escape any ascii character not in [A-Za-z_0-9].
448 //
449 // Note that it's legal to escape a character even if it has no
450 // special meaning in a regular expression -- so this function does
451 // that. (This also makes it identical to the perl function of the
452 // same name; see `perldoc -f quotemeta`.) The one exception is
453 // escaping NUL: rather than doing backslash + NUL, like perl does,
454 // we do '\0', because pcre itself doesn't take embedded NUL chars.
455 for (int ii = 0; ii < unquoted.size(); ++ii) {
456 // Note that using 'isalnum' here raises the benchmark time from
457 // 32ns to 58ns:
458 if (unquoted[ii] == '\0') {
459 result += "\\0";
460 } else if ((unquoted[ii] < 'a' || unquoted[ii] > 'z') &&
461 (unquoted[ii] < 'A' || unquoted[ii] > 'Z') &&
462 (unquoted[ii] < '0' || unquoted[ii] > '9') &&
463 unquoted[ii] != '_' &&
464 // If this is the part of a UTF8 or Latin1 character, we need
465 // to copy this byte without escaping. Experimentally this is
466 // what works correctly with the regexp library.
467 !(unquoted[ii] & 128)) {
468 result += '\\';
469 result += unquoted[ii];
470 } else {
471 result += unquoted[ii];
472 }
473 }
474
475 return result;
476 }
477
478 /***** Actual matching and rewriting code *****/
479
480 int RE::TryMatch(const StringPiece& text,
481 int startpos,
482 Anchor anchor,
483 int *vec,
484 int vecsize) const {
485 pcre* re = (anchor == ANCHOR_BOTH) ? re_full_ : re_partial_;
486 if (re == NULL) {
487 //fprintf(stderr, "Matching against invalid re: %s\n", error_->c_str());
488 return 0;
489 }
490
491 pcre_extra extra = { 0, 0, 0, 0, 0, 0 };
492 if (options_.match_limit() > 0) {
493 extra.flags |= PCRE_EXTRA_MATCH_LIMIT;
494 extra.match_limit = options_.match_limit();
495 }
496 if (options_.match_limit_recursion() > 0) {
497 extra.flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
498 extra.match_limit_recursion = options_.match_limit_recursion();
499 }
500 int rc = pcre_exec(re, // The regular expression object
501 &extra,
502 (text.data() == NULL) ? "" : text.data(),
503 text.size(),
504 startpos,
505 (anchor == UNANCHORED) ? 0 : PCRE_ANCHORED,
506 vec,
507 vecsize);
508
509 // Handle errors
510 if (rc == PCRE_ERROR_NOMATCH) {
511 return 0;
512 } else if (rc < 0) {
513 //fprintf(stderr, "Unexpected return code: %d when matching '%s'\n",
514 // re, pattern_.c_str());
515 return 0;
516 } else if (rc == 0) {
517 // pcre_exec() returns 0 as a special case when the number of
518 // capturing subpatterns exceeds the size of the vector.
519 // When this happens, there is a match and the output vector
520 // is filled, but we miss out on the positions of the extra subpatterns.
521 rc = vecsize / 2;
522 }
523
524 return rc;
525 }
526
527 bool RE::DoMatchImpl(const StringPiece& text,
528 Anchor anchor,
529 int* consumed,
530 const Arg* const* args,
531 int n,
532 int* vec,
533 int vecsize) const {
534 assert((1 + n) * 3 <= vecsize); // results + PCRE workspace
535 int matches = TryMatch(text, 0, anchor, vec, vecsize);
536 assert(matches >= 0); // TryMatch never returns negatives
537 if (matches == 0)
538 return false;
539
540 *consumed = vec[1];
541
542 if (n == 0 || args == NULL) {
543 // We are not interested in results
544 return true;
545 }
546
547 if (NumberOfCapturingGroups() < n) {
548 // RE has fewer capturing groups than number of arg pointers passed in
549 return false;
550 }
551
552 // If we got here, we must have matched the whole pattern.
553 // We do not need (can not do) any more checks on the value of 'matches' here
554 // -- see the comment for TryMatch.
555 for (int i = 0; i < n; i++) {
556 const int start = vec[2*(i+1)];
557 const int limit = vec[2*(i+1)+1];
558 if (!args[i]->Parse(text.data() + start, limit-start)) {
559 // TODO: Should we indicate what the error was?
560 return false;
561 }
562 }
563
564 return true;
565 }
566
567 bool RE::DoMatch(const StringPiece& text,
568 Anchor anchor,
569 int* consumed,
570 const Arg* const args[],
571 int n) const {
572 assert(n >= 0);
573 size_t const vecsize = (1 + n) * 3; // results + PCRE workspace
574 // (as for kVecSize)
575 int space[21]; // use stack allocation for small vecsize (common case)
576 int* vec = vecsize <= 21 ? space : new int[vecsize];
577 bool retval = DoMatchImpl(text, anchor, consumed, args, n, vec, vecsize);
578 if (vec != space) delete [] vec;
579 return retval;
580 }
581
582 bool RE::Rewrite(string *out, const StringPiece &rewrite,
583 const StringPiece &text, int *vec, int veclen) const {
584 for (const char *s = rewrite.data(), *end = s + rewrite.size();
585 s < end; s++) {
586 int c = *s;
587 if (c == '\\') {
588 c = *++s;
589 if (isdigit(c)) {
590 int n = (c - '0');
591 if (n >= veclen) {
592 //fprintf(stderr, requested group %d in regexp %.*s\n",
593 // n, rewrite.size(), rewrite.data());
594 return false;
595 }
596 int start = vec[2 * n];
597 if (start >= 0)
598 out->append(text.data() + start, vec[2 * n + 1] - start);
599 } else if (c == '\\') {
600 out->push_back('\\');
601 } else {
602 //fprintf(stderr, "invalid rewrite pattern: %.*s\n",
603 // rewrite.size(), rewrite.data());
604 return false;
605 }
606 } else {
607 out->push_back(c);
608 }
609 }
610 return true;
611 }
612
613 // Return the number of capturing subpatterns, or -1 if the
614 // regexp wasn't valid on construction.
615 int RE::NumberOfCapturingGroups() const {
616 if (re_partial_ == NULL) return -1;
617
618 int result;
619 int pcre_retval = pcre_fullinfo(re_partial_, // The regular expression object
620 NULL, // We did not study the pattern
621 PCRE_INFO_CAPTURECOUNT,
622 &result);
623 assert(pcre_retval == 0);
624 return result;
625 }
626
627 /***** Parsers for various types *****/
628
629 bool Arg::parse_null(const char* str, int n, void* dest) {
630 // We fail if somebody asked us to store into a non-NULL void* pointer
631 return (dest == NULL);
632 }
633
634 bool Arg::parse_string(const char* str, int n, void* dest) {
635 if (dest == NULL) return true;
636 reinterpret_cast<string*>(dest)->assign(str, n);
637 return true;
638 }
639
640 bool Arg::parse_stringpiece(const char* str, int n, void* dest) {
641 if (dest == NULL) return true;
642 reinterpret_cast<StringPiece*>(dest)->set(str, n);
643 return true;
644 }
645
646 bool Arg::parse_char(const char* str, int n, void* dest) {
647 if (n != 1) return false;
648 if (dest == NULL) return true;
649 *(reinterpret_cast<char*>(dest)) = str[0];
650 return true;
651 }
652
653 bool Arg::parse_uchar(const char* str, int n, void* dest) {
654 if (n != 1) return false;
655 if (dest == NULL) return true;
656 *(reinterpret_cast<unsigned char*>(dest)) = str[0];
657 return true;
658 }
659
660 // Largest number spec that we are willing to parse
661 static const int kMaxNumberLength = 32;
662
663 // REQUIRES "buf" must have length at least kMaxNumberLength+1
664 // REQUIRES "n > 0"
665 // Copies "str" into "buf" and null-terminates if necessary.
666 // Returns one of:
667 // a. "str" if no termination is needed
668 // b. "buf" if the string was copied and null-terminated
669 // c. "" if the input was invalid and has no hope of being parsed
670 static const char* TerminateNumber(char* buf, const char* str, int n) {
671 if ((n > 0) && isspace(*str)) {
672 // We are less forgiving than the strtoxxx() routines and do not
673 // allow leading spaces.
674 return "";
675 }
676
677 // See if the character right after the input text may potentially
678 // look like a digit.
679 if (isdigit(str[n]) ||
680 ((str[n] >= 'a') && (str[n] <= 'f')) ||
681 ((str[n] >= 'A') && (str[n] <= 'F'))) {
682 if (n > kMaxNumberLength) return ""; // Input too big to be a valid number
683 memcpy(buf, str, n);
684 buf[n] = '\0';
685 return buf;
686 } else {
687 // We can parse right out of the supplied string, so return it.
688 return str;
689 }
690 }
691
692 bool Arg::parse_long_radix(const char* str,
693 int n,
694 void* dest,
695 int radix) {
696 if (n == 0) return false;
697 char buf[kMaxNumberLength+1];
698 str = TerminateNumber(buf, str, n);
699 char* end;
700 errno = 0;
701 long r = strtol(str, &end, radix);
702 if (end != str + n) return false; // Leftover junk
703 if (errno) return false;
704 if (dest == NULL) return true;
705 *(reinterpret_cast<long*>(dest)) = r;
706 return true;
707 }
708
709 bool Arg::parse_ulong_radix(const char* str,
710 int n,
711 void* dest,
712 int radix) {
713 if (n == 0) return false;
714 char buf[kMaxNumberLength+1];
715 str = TerminateNumber(buf, str, n);
716 if (str[0] == '-') return false; // strtoul() on a negative number?!
717 char* end;
718 errno = 0;
719 unsigned long r = strtoul(str, &end, radix);
720 if (end != str + n) return false; // Leftover junk
721 if (errno) return false;
722 if (dest == NULL) return true;
723 *(reinterpret_cast<unsigned long*>(dest)) = r;
724 return true;
725 }
726
727 bool Arg::parse_short_radix(const char* str,
728 int n,
729 void* dest,
730 int radix) {
731 long r;
732 if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
733 if (r < SHRT_MIN || r > SHRT_MAX) return false; // Out of range
734 if (dest == NULL) return true;
735 *(reinterpret_cast<short*>(dest)) = static_cast<short>(r);
736 return true;
737 }
738
739 bool Arg::parse_ushort_radix(const char* str,
740 int n,
741 void* dest,
742 int radix) {
743 unsigned long r;
744 if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
745 if (r > USHRT_MAX) return false; // Out of range
746 if (dest == NULL) return true;
747 *(reinterpret_cast<unsigned short*>(dest)) = static_cast<unsigned short>(r);
748 return true;
749 }
750
751 bool Arg::parse_int_radix(const char* str,
752 int n,
753 void* dest,
754 int radix) {
755 long r;
756 if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
757 if (r < INT_MIN || r > INT_MAX) return false; // Out of range
758 if (dest == NULL) return true;
759 *(reinterpret_cast<int*>(dest)) = r;
760 return true;
761 }
762
763 bool Arg::parse_uint_radix(const char* str,
764 int n,
765 void* dest,
766 int radix) {
767 unsigned long r;
768 if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
769 if (r > UINT_MAX) return false; // Out of range
770 if (dest == NULL) return true;
771 *(reinterpret_cast<unsigned int*>(dest)) = r;
772 return true;
773 }
774
775 bool Arg::parse_longlong_radix(const char* str,
776 int n,
777 void* dest,
778 int radix) {
779 #ifndef HAVE_LONG_LONG
780 return false;
781 #else
782 if (n == 0) return false;
783 char buf[kMaxNumberLength+1];
784 str = TerminateNumber(buf, str, n);
785 char* end;
786 errno = 0;
787 #if defined HAVE_STRTOQ
788 long long r = strtoq(str, &end, radix);
789 #elif defined HAVE_STRTOLL
790 long long r = strtoll(str, &end, radix);
791 #elif defined HAVE__STRTOI64
792 long long r = _strtoi64(str, &end, radix);
793 #else
794 #error parse_longlong_radix: cannot convert input to a long-long
795 #endif
796 if (end != str + n) return false; // Leftover junk
797 if (errno) return false;
798 if (dest == NULL) return true;
799 *(reinterpret_cast<long long*>(dest)) = r;
800 return true;
801 #endif /* HAVE_LONG_LONG */
802 }
803
804 bool Arg::parse_ulonglong_radix(const char* str,
805 int n,
806 void* dest,
807 int radix) {
808 #ifndef HAVE_UNSIGNED_LONG_LONG
809 return false;
810 #else
811 if (n == 0) return false;
812 char buf[kMaxNumberLength+1];
813 str = TerminateNumber(buf, str, n);
814 if (str[0] == '-') return false; // strtoull() on a negative number?!
815 char* end;
816 errno = 0;
817 #if defined HAVE_STRTOQ
818 unsigned long long r = strtouq(str, &end, radix);
819 #elif defined HAVE_STRTOLL
820 unsigned long long r = strtoull(str, &end, radix);
821 #elif defined HAVE__STRTOI64
822 unsigned long long r = _strtoui64(str, &end, radix);
823 #else
824 #error parse_ulonglong_radix: cannot convert input to a long-long
825 #endif
826 if (end != str + n) return false; // Leftover junk
827 if (errno) return false;
828 if (dest == NULL) return true;
829 *(reinterpret_cast<unsigned long long*>(dest)) = r;
830 return true;
831 #endif /* HAVE_UNSIGNED_LONG_LONG */
832 }
833
834 bool Arg::parse_double(const char* str, int n, void* dest) {
835 if (n == 0) return false;
836 static const int kMaxLength = 200;
837 char buf[kMaxLength];
838 if (n >= kMaxLength) return false;
839 memcpy(buf, str, n);
840 buf[n] = '\0';
841 errno = 0;
842 char* end;
843 double r = strtod(buf, &end);
844 if (end != buf + n) return false; // Leftover junk
845 if (errno) return false;
846 if (dest == NULL) return true;
847 *(reinterpret_cast<double*>(dest)) = r;
848 return true;
849 }
850
851 bool Arg::parse_float(const char* str, int n, void* dest) {
852 double r;
853 if (!parse_double(str, n, &r)) return false;
854 if (dest == NULL) return true;
855 *(reinterpret_cast<float*>(dest)) = static_cast<float>(r);
856 return true;
857 }
858
859
860 #define DEFINE_INTEGER_PARSERS(name) \
861 bool Arg::parse_##name(const char* str, int n, void* dest) { \
862 return parse_##name##_radix(str, n, dest, 10); \
863 } \
864 bool Arg::parse_##name##_hex(const char* str, int n, void* dest) { \
865 return parse_##name##_radix(str, n, dest, 16); \
866 } \
867 bool Arg::parse_##name##_octal(const char* str, int n, void* dest) { \
868 return parse_##name##_radix(str, n, dest, 8); \
869 } \
870 bool Arg::parse_##name##_cradix(const char* str, int n, void* dest) { \
871 return parse_##name##_radix(str, n, dest, 0); \
872 }
873
874 DEFINE_INTEGER_PARSERS(short) /* */
875 DEFINE_INTEGER_PARSERS(ushort) /* */
876 DEFINE_INTEGER_PARSERS(int) /* Don't use semicolons after these */
877 DEFINE_INTEGER_PARSERS(uint) /* statements because they can cause */
878 DEFINE_INTEGER_PARSERS(long) /* compiler warnings if the checking */
879 DEFINE_INTEGER_PARSERS(ulong) /* level is turned up high enough. */
880 DEFINE_INTEGER_PARSERS(longlong) /* */
881 DEFINE_INTEGER_PARSERS(ulonglong) /* */
882
883 #undef DEFINE_INTEGER_PARSERS
884
885 } // namespace pcrecpp

Properties

Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

  ViewVC Help
Powered by ViewVC 1.1.5