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

Contents of /code/trunk/pcrecpp.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 253 - (show annotations) (download)
Mon Sep 17 10:51:30 2007 UTC (6 years, 11 months ago) by ph10
File size: 29732 byte(s)
Added checks for ANY and ANYCRLF to pcrecpp.cc where previously it checked only 
for CRLF.

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

Properties

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12