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

Contents of /code/trunk/pcrecpp.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 234 - (show annotations) (download)
Tue Sep 11 11:36:27 2007 UTC (7 years ago) by ph10
File size: 29182 byte(s)
Macros for snprintf, strtoll, strtoull when compiling the C++ things under 
Windows.

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 _WIN32
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 static int NewlineMode(int pcre_options) {
341 // TODO: if we can make it threadsafe, cache this var
342 int newline_mode = 0;
343 /* if (newline_mode) return newline_mode; */ // do this once it's cached
344 if (pcre_options & (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF)) {
345 newline_mode = (pcre_options &
346 (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF));
347 } else {
348 int newline;
349 pcre_config(PCRE_CONFIG_NEWLINE, &newline);
350 if (newline == 10)
351 newline_mode = PCRE_NEWLINE_LF;
352 else if (newline == 13)
353 newline_mode = PCRE_NEWLINE_CR;
354 else if (newline == 3338)
355 newline_mode = PCRE_NEWLINE_CRLF;
356 else
357 assert("" == "Unexpected return value from pcre_config(NEWLINE)");
358 }
359 return newline_mode;
360 }
361
362 int RE::GlobalReplace(const StringPiece& rewrite,
363 string *str) const {
364 int count = 0;
365 int vec[kVecSize];
366 string out;
367 int start = 0;
368 int lastend = -1;
369
370 for (; start <= static_cast<int>(str->length()); count++) {
371 int matches = TryMatch(*str, start, UNANCHORED, vec, kVecSize);
372 if (matches <= 0)
373 break;
374 int matchstart = vec[0], matchend = vec[1];
375 assert(matchstart >= start);
376 assert(matchend >= matchstart);
377 if (matchstart == matchend && matchstart == lastend) {
378 // advance one character if we matched an empty string at the same
379 // place as the last match occurred
380 matchend = start + 1;
381 // If the current char is CR and we're in CRLF mode, skip LF too.
382 // Note it's better to call pcre_fullinfo() than to examine
383 // all_options(), since options_ could have changed bewteen
384 // compile-time and now, but this is simpler and safe enough.
385 if (start+1 < static_cast<int>(str->length()) &&
386 (*str)[start] == '\r' && (*str)[start+1] == '\n' &&
387 NewlineMode(options_.all_options()) == PCRE_NEWLINE_CRLF) {
388 matchend++;
389 }
390 // We also need to advance more than one char if we're in utf8 mode.
391 #ifdef SUPPORT_UTF8
392 if (options_.utf8()) {
393 while (matchend < static_cast<int>(str->length()) &&
394 ((*str)[matchend] & 0xc0) == 0x80)
395 matchend++;
396 }
397 #endif
398 if (matchend <= static_cast<int>(str->length()))
399 out.append(*str, start, matchend - start);
400 start = matchend;
401 } else {
402 out.append(*str, start, matchstart - start);
403 Rewrite(&out, rewrite, *str, vec, matches);
404 start = matchend;
405 lastend = matchend;
406 count++;
407 }
408 }
409
410 if (count == 0)
411 return 0;
412
413 if (start < static_cast<int>(str->length()))
414 out.append(*str, start, str->length() - start);
415 swap(out, *str);
416 return count;
417 }
418
419 bool RE::Extract(const StringPiece& rewrite,
420 const StringPiece& text,
421 string *out) const {
422 int vec[kVecSize];
423 int matches = TryMatch(text, 0, UNANCHORED, vec, kVecSize);
424 if (matches == 0)
425 return false;
426 out->erase();
427 return Rewrite(out, rewrite, text, vec, matches);
428 }
429
430 /*static*/ string RE::QuoteMeta(const StringPiece& unquoted) {
431 string result;
432
433 // Escape any ascii character not in [A-Za-z_0-9].
434 //
435 // Note that it's legal to escape a character even if it has no
436 // special meaning in a regular expression -- so this function does
437 // that. (This also makes it identical to the perl function of the
438 // same name; see `perldoc -f quotemeta`.)
439 for (int ii = 0; ii < unquoted.size(); ++ii) {
440 // Note that using 'isalnum' here raises the benchmark time from
441 // 32ns to 58ns:
442 if ((unquoted[ii] < 'a' || unquoted[ii] > 'z') &&
443 (unquoted[ii] < 'A' || unquoted[ii] > 'Z') &&
444 (unquoted[ii] < '0' || unquoted[ii] > '9') &&
445 unquoted[ii] != '_' &&
446 // If this is the part of a UTF8 or Latin1 character, we need
447 // to copy this byte without escaping. Experimentally this is
448 // what works correctly with the regexp library.
449 !(unquoted[ii] & 128)) {
450 result += '\\';
451 }
452 result += unquoted[ii];
453 }
454
455 return result;
456 }
457
458 /***** Actual matching and rewriting code *****/
459
460 int RE::TryMatch(const StringPiece& text,
461 int startpos,
462 Anchor anchor,
463 int *vec,
464 int vecsize) const {
465 pcre* re = (anchor == ANCHOR_BOTH) ? re_full_ : re_partial_;
466 if (re == NULL) {
467 //fprintf(stderr, "Matching against invalid re: %s\n", error_->c_str());
468 return 0;
469 }
470
471 pcre_extra extra = { 0, 0, 0, 0, 0, 0 };
472 if (options_.match_limit() > 0) {
473 extra.flags |= PCRE_EXTRA_MATCH_LIMIT;
474 extra.match_limit = options_.match_limit();
475 }
476 if (options_.match_limit_recursion() > 0) {
477 extra.flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
478 extra.match_limit_recursion = options_.match_limit_recursion();
479 }
480 int rc = pcre_exec(re, // The regular expression object
481 &extra,
482 (text.data() == NULL) ? "" : text.data(),
483 text.size(),
484 startpos,
485 (anchor == UNANCHORED) ? 0 : PCRE_ANCHORED,
486 vec,
487 vecsize);
488
489 // Handle errors
490 if (rc == PCRE_ERROR_NOMATCH) {
491 return 0;
492 } else if (rc < 0) {
493 //fprintf(stderr, "Unexpected return code: %d when matching '%s'\n",
494 // re, pattern_.c_str());
495 return 0;
496 } else if (rc == 0) {
497 // pcre_exec() returns 0 as a special case when the number of
498 // capturing subpatterns exceeds the size of the vector.
499 // When this happens, there is a match and the output vector
500 // is filled, but we miss out on the positions of the extra subpatterns.
501 rc = vecsize / 2;
502 }
503
504 return rc;
505 }
506
507 bool RE::DoMatchImpl(const StringPiece& text,
508 Anchor anchor,
509 int* consumed,
510 const Arg* const* args,
511 int n,
512 int* vec,
513 int vecsize) const {
514 assert((1 + n) * 3 <= vecsize); // results + PCRE workspace
515 int matches = TryMatch(text, 0, anchor, vec, vecsize);
516 assert(matches >= 0); // TryMatch never returns negatives
517 if (matches == 0)
518 return false;
519
520 *consumed = vec[1];
521
522 if (n == 0 || args == NULL) {
523 // We are not interested in results
524 return true;
525 }
526
527 if (NumberOfCapturingGroups() < n) {
528 // RE has fewer capturing groups than number of arg pointers passed in
529 return false;
530 }
531
532 // If we got here, we must have matched the whole pattern.
533 // We do not need (can not do) any more checks on the value of 'matches' here
534 // -- see the comment for TryMatch.
535 for (int i = 0; i < n; i++) {
536 const int start = vec[2*(i+1)];
537 const int limit = vec[2*(i+1)+1];
538 if (!args[i]->Parse(text.data() + start, limit-start)) {
539 // TODO: Should we indicate what the error was?
540 return false;
541 }
542 }
543
544 return true;
545 }
546
547 bool RE::DoMatch(const StringPiece& text,
548 Anchor anchor,
549 int* consumed,
550 const Arg* const args[],
551 int n) const {
552 assert(n >= 0);
553 size_t const vecsize = (1 + n) * 3; // results + PCRE workspace
554 // (as for kVecSize)
555 int space[21]; // use stack allocation for small vecsize (common case)
556 int* vec = vecsize <= 21 ? space : new int[vecsize];
557 bool retval = DoMatchImpl(text, anchor, consumed, args, n, vec, vecsize);
558 if (vec != space) delete [] vec;
559 return retval;
560 }
561
562 bool RE::Rewrite(string *out, const StringPiece &rewrite,
563 const StringPiece &text, int *vec, int veclen) const {
564 for (const char *s = rewrite.data(), *end = s + rewrite.size();
565 s < end; s++) {
566 int c = *s;
567 if (c == '\\') {
568 c = *++s;
569 if (isdigit(c)) {
570 int n = (c - '0');
571 if (n >= veclen) {
572 //fprintf(stderr, requested group %d in regexp %.*s\n",
573 // n, rewrite.size(), rewrite.data());
574 return false;
575 }
576 int start = vec[2 * n];
577 if (start >= 0)
578 out->append(text.data() + start, vec[2 * n + 1] - start);
579 } else if (c == '\\') {
580 out->push_back('\\');
581 } else {
582 //fprintf(stderr, "invalid rewrite pattern: %.*s\n",
583 // rewrite.size(), rewrite.data());
584 return false;
585 }
586 } else {
587 out->push_back(c);
588 }
589 }
590 return true;
591 }
592
593 // Return the number of capturing subpatterns, or -1 if the
594 // regexp wasn't valid on construction.
595 int RE::NumberOfCapturingGroups() const {
596 if (re_partial_ == NULL) return -1;
597
598 int result;
599 int pcre_retval = pcre_fullinfo(re_partial_, // The regular expression object
600 NULL, // We did not study the pattern
601 PCRE_INFO_CAPTURECOUNT,
602 &result);
603 assert(pcre_retval == 0);
604 return result;
605 }
606
607 /***** Parsers for various types *****/
608
609 bool Arg::parse_null(const char* str, int n, void* dest) {
610 // We fail if somebody asked us to store into a non-NULL void* pointer
611 return (dest == NULL);
612 }
613
614 bool Arg::parse_string(const char* str, int n, void* dest) {
615 reinterpret_cast<string*>(dest)->assign(str, n);
616 return true;
617 }
618
619 bool Arg::parse_stringpiece(const char* str, int n, void* dest) {
620 reinterpret_cast<StringPiece*>(dest)->set(str, n);
621 return true;
622 }
623
624 bool Arg::parse_char(const char* str, int n, void* dest) {
625 if (n != 1) return false;
626 *(reinterpret_cast<char*>(dest)) = str[0];
627 return true;
628 }
629
630 bool Arg::parse_uchar(const char* str, int n, void* dest) {
631 if (n != 1) return false;
632 *(reinterpret_cast<unsigned char*>(dest)) = str[0];
633 return true;
634 }
635
636 // Largest number spec that we are willing to parse
637 static const int kMaxNumberLength = 32;
638
639 // REQUIRES "buf" must have length at least kMaxNumberLength+1
640 // REQUIRES "n > 0"
641 // Copies "str" into "buf" and null-terminates if necessary.
642 // Returns one of:
643 // a. "str" if no termination is needed
644 // b. "buf" if the string was copied and null-terminated
645 // c. "" if the input was invalid and has no hope of being parsed
646 static const char* TerminateNumber(char* buf, const char* str, int n) {
647 if ((n > 0) && isspace(*str)) {
648 // We are less forgiving than the strtoxxx() routines and do not
649 // allow leading spaces.
650 return "";
651 }
652
653 // See if the character right after the input text may potentially
654 // look like a digit.
655 if (isdigit(str[n]) ||
656 ((str[n] >= 'a') && (str[n] <= 'f')) ||
657 ((str[n] >= 'A') && (str[n] <= 'F'))) {
658 if (n > kMaxNumberLength) return ""; // Input too big to be a valid number
659 memcpy(buf, str, n);
660 buf[n] = '\0';
661 return buf;
662 } else {
663 // We can parse right out of the supplied string, so return it.
664 return str;
665 }
666 }
667
668 bool Arg::parse_long_radix(const char* str,
669 int n,
670 void* dest,
671 int radix) {
672 if (n == 0) return false;
673 char buf[kMaxNumberLength+1];
674 str = TerminateNumber(buf, str, n);
675 char* end;
676 errno = 0;
677 long r = strtol(str, &end, radix);
678 if (end != str + n) return false; // Leftover junk
679 if (errno) return false;
680 *(reinterpret_cast<long*>(dest)) = r;
681 return true;
682 }
683
684 bool Arg::parse_ulong_radix(const char* str,
685 int n,
686 void* dest,
687 int radix) {
688 if (n == 0) return false;
689 char buf[kMaxNumberLength+1];
690 str = TerminateNumber(buf, str, n);
691 if (str[0] == '-') return false; // strtoul() on a negative number?!
692 char* end;
693 errno = 0;
694 unsigned long r = strtoul(str, &end, radix);
695 if (end != str + n) return false; // Leftover junk
696 if (errno) return false;
697 *(reinterpret_cast<unsigned long*>(dest)) = r;
698 return true;
699 }
700
701 bool Arg::parse_short_radix(const char* str,
702 int n,
703 void* dest,
704 int radix) {
705 long r;
706 if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
707 if (r < SHRT_MIN || r > SHRT_MAX) return false; // Out of range
708 *(reinterpret_cast<short*>(dest)) = r;
709 return true;
710 }
711
712 bool Arg::parse_ushort_radix(const char* str,
713 int n,
714 void* dest,
715 int radix) {
716 unsigned long r;
717 if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
718 if (r > USHRT_MAX) return false; // Out of range
719 *(reinterpret_cast<unsigned short*>(dest)) = r;
720 return true;
721 }
722
723 bool Arg::parse_int_radix(const char* str,
724 int n,
725 void* dest,
726 int radix) {
727 long r;
728 if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
729 if (r < INT_MIN || r > INT_MAX) return false; // Out of range
730 *(reinterpret_cast<int*>(dest)) = r;
731 return true;
732 }
733
734 bool Arg::parse_uint_radix(const char* str,
735 int n,
736 void* dest,
737 int radix) {
738 unsigned long r;
739 if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
740 if (r > UINT_MAX) return false; // Out of range
741 *(reinterpret_cast<unsigned int*>(dest)) = r;
742 return true;
743 }
744
745 bool Arg::parse_longlong_radix(const char* str,
746 int n,
747 void* dest,
748 int radix) {
749 #ifndef HAVE_LONG_LONG
750 return false;
751 #else
752 if (n == 0) return false;
753 char buf[kMaxNumberLength+1];
754 str = TerminateNumber(buf, str, n);
755 char* end;
756 errno = 0;
757 #if defined HAVE_STRTOQ
758 long long r = strtoq(str, &end, radix);
759 #elif defined HAVE_STRTOLL
760 long long r = strtoll(str, &end, radix);
761 #else
762 #error parse_longlong_radix: cannot convert input to a long-long
763 #endif
764 if (end != str + n) return false; // Leftover junk
765 if (errno) return false;
766 *(reinterpret_cast<long long*>(dest)) = r;
767 return true;
768 #endif /* HAVE_LONG_LONG */
769 }
770
771 bool Arg::parse_ulonglong_radix(const char* str,
772 int n,
773 void* dest,
774 int radix) {
775 #ifndef HAVE_UNSIGNED_LONG_LONG
776 return false;
777 #else
778 if (n == 0) return false;
779 char buf[kMaxNumberLength+1];
780 str = TerminateNumber(buf, str, n);
781 if (str[0] == '-') return false; // strtoull() on a negative number?!
782 char* end;
783 errno = 0;
784 #if defined HAVE_STRTOQ
785 unsigned long long r = strtouq(str, &end, radix);
786 #elif defined HAVE_STRTOLL
787 unsigned long long r = strtoull(str, &end, radix);
788 #else
789 #error parse_ulonglong_radix: cannot convert input to a long-long
790 #endif
791 if (end != str + n) return false; // Leftover junk
792 if (errno) return false;
793 *(reinterpret_cast<unsigned long long*>(dest)) = r;
794 return true;
795 #endif /* HAVE_UNSIGNED_LONG_LONG */
796 }
797
798 bool Arg::parse_double(const char* str, int n, void* dest) {
799 if (n == 0) return false;
800 static const int kMaxLength = 200;
801 char buf[kMaxLength];
802 if (n >= kMaxLength) return false;
803 memcpy(buf, str, n);
804 buf[n] = '\0';
805 errno = 0;
806 char* end;
807 double r = strtod(buf, &end);
808 if (end != buf + n) return false; // Leftover junk
809 if (errno) return false;
810 *(reinterpret_cast<double*>(dest)) = r;
811 return true;
812 }
813
814 bool Arg::parse_float(const char* str, int n, void* dest) {
815 double r;
816 if (!parse_double(str, n, &r)) return false;
817 *(reinterpret_cast<float*>(dest)) = static_cast<float>(r);
818 return true;
819 }
820
821
822 #define DEFINE_INTEGER_PARSERS(name) \
823 bool Arg::parse_##name(const char* str, int n, void* dest) { \
824 return parse_##name##_radix(str, n, dest, 10); \
825 } \
826 bool Arg::parse_##name##_hex(const char* str, int n, void* dest) { \
827 return parse_##name##_radix(str, n, dest, 16); \
828 } \
829 bool Arg::parse_##name##_octal(const char* str, int n, void* dest) { \
830 return parse_##name##_radix(str, n, dest, 8); \
831 } \
832 bool Arg::parse_##name##_cradix(const char* str, int n, void* dest) { \
833 return parse_##name##_radix(str, n, dest, 0); \
834 }
835
836 DEFINE_INTEGER_PARSERS(short) /* */
837 DEFINE_INTEGER_PARSERS(ushort) /* */
838 DEFINE_INTEGER_PARSERS(int) /* Don't use semicolons after these */
839 DEFINE_INTEGER_PARSERS(uint) /* statements because they can cause */
840 DEFINE_INTEGER_PARSERS(long) /* compiler warnings if the checking */
841 DEFINE_INTEGER_PARSERS(ulong) /* level is turned up high enough. */
842 DEFINE_INTEGER_PARSERS(longlong) /* */
843 DEFINE_INTEGER_PARSERS(ulonglong) /* */
844
845 #undef DEFINE_INTEGER_PARSERS
846
847 } // 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