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

Contents of /code/trunk/pcrecpp.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 96 - (show annotations) (download)
Fri Mar 2 13:10:43 2007 UTC (7 years, 6 months ago) by nigel
File size: 30088 byte(s)
 r6896@hex:  nm | 2007-03-02 13:09:14 +0000
 Added EOL and keywork properties throughout

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