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

Contents of /code/trunk/pcrecpp.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 179 - (hide annotations) (download)
Wed Jun 13 08:53:45 2007 UTC (6 years, 10 months ago) by ph10
File size: 29240 byte(s)
Apply C++ patch to fix a bad optimization.

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