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

Contents of /code/trunk/pcrecpp.cc

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5