/[pcre]/code/tags/pcre-6.2/pcrecpp.h.in
ViewVC logotype

Contents of /code/tags/pcre-6.2/pcrecpp.h.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 81 - (show annotations)
Sat Feb 24 21:40:59 2007 UTC (8 years, 5 months ago) by nigel
Original Path: code/trunk/pcrecpp.h.in
File size: 29028 byte(s)
Error occurred while calculating annotation data.
Load pcre-6.2 into code/trunk.
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 // Support for PCRE_XXX modifiers added by Giuseppe Maxia, July 2005
32
33 #ifndef _PCRE_REGEXP_H
34 #define _PCRE_REGEXP_H
35
36 // C++ interface to the pcre regular-expression library. RE supports
37 // Perl-style regular expressions (with extensions like \d, \w, \s,
38 // ...).
39 //
40 // -----------------------------------------------------------------------
41 // REGEXP SYNTAX:
42 //
43 // This module is part of the pcre library and hence supports its syntax
44 // for regular expressions.
45 //
46 // The syntax is pretty similar to Perl's. For those not familiar
47 // with Perl's regular expressions, here are some examples of the most
48 // commonly used extensions:
49 //
50 // "hello (\\w+) world" -- \w matches a "word" character
51 // "version (\\d+)" -- \d matches a digit
52 // "hello\\s+world" -- \s matches any whitespace character
53 // "\\b(\\w+)\\b" -- \b matches empty string at a word boundary
54 // "(?i)hello" -- (?i) turns on case-insensitive matching
55 // "/\\*(.*?)\\*/" -- .*? matches . minimum no. of times possible
56 //
57 // -----------------------------------------------------------------------
58 // MATCHING INTERFACE:
59 //
60 // The "FullMatch" operation checks that supplied text matches a
61 // supplied pattern exactly.
62 //
63 // Example: successful match
64 // pcrecpp::RE re("h.*o");
65 // re.FullMatch("hello");
66 //
67 // Example: unsuccessful match (requires full match):
68 // pcrecpp::RE re("e");
69 // !re.FullMatch("hello");
70 //
71 // Example: creating a temporary RE object:
72 // pcrecpp::RE("h.*o").FullMatch("hello");
73 //
74 // You can pass in a "const char*" or a "string" for "text". The
75 // examples below tend to use a const char*.
76 //
77 // You can, as in the different examples above, store the RE object
78 // explicitly in a variable or use a temporary RE object. The
79 // examples below use one mode or the other arbitrarily. Either
80 // could correctly be used for any of these examples.
81 //
82 // -----------------------------------------------------------------------
83 // MATCHING WITH SUB-STRING EXTRACTION:
84 //
85 // You can supply extra pointer arguments to extract matched subpieces.
86 //
87 // Example: extracts "ruby" into "s" and 1234 into "i"
88 // int i;
89 // string s;
90 // pcrecpp::RE re("(\\w+):(\\d+)");
91 // re.FullMatch("ruby:1234", &s, &i);
92 //
93 // Example: does not try to extract any extra sub-patterns
94 // re.FullMatch("ruby:1234", &s);
95 //
96 // Example: does not try to extract into NULL
97 // re.FullMatch("ruby:1234", NULL, &i);
98 //
99 // Example: integer overflow causes failure
100 // !re.FullMatch("ruby:1234567891234", NULL, &i);
101 //
102 // Example: fails because there aren't enough sub-patterns:
103 // !pcrecpp::RE("\\w+:\\d+").FullMatch("ruby:1234", &s);
104 //
105 // Example: fails because string cannot be stored in integer
106 // !pcrecpp::RE("(.*)").FullMatch("ruby", &i);
107 //
108 // The provided pointer arguments can be pointers to any scalar numeric
109 // type, or one of
110 // string (matched piece is copied to string)
111 // StringPiece (StringPiece is mutated to point to matched piece)
112 // T (where "bool T::ParseFrom(const char*, int)" exists)
113 // NULL (the corresponding matched sub-pattern is not copied)
114 //
115 // -----------------------------------------------------------------------
116 // DO_MATCH
117 //
118 // The matching interface supports at most 16 arguments per call.
119 // If you need more, consider using the more general interface
120 // pcrecpp::RE::DoMatch(). See pcrecpp.h for the signature for DoMatch.
121 //
122 // -----------------------------------------------------------------------
123 // PARTIAL MATCHES
124 //
125 // You can use the "PartialMatch" operation when you want the pattern
126 // to match any substring of the text.
127 //
128 // Example: simple search for a string:
129 // pcrecpp::RE("ell").PartialMatch("hello");
130 //
131 // Example: find first number in a string:
132 // int number;
133 // pcrecpp::RE re("(\\d+)");
134 // re.PartialMatch("x*100 + 20", &number);
135 // assert(number == 100);
136 //
137 // -----------------------------------------------------------------------
138 // UTF-8 AND THE MATCHING INTERFACE:
139 //
140 // By default, pattern and text are plain text, one byte per character.
141 // The UTF8 flag, passed to the constructor, causes both pattern
142 // and string to be treated as UTF-8 text, still a byte stream but
143 // potentially multiple bytes per character. In practice, the text
144 // is likelier to be UTF-8 than the pattern, but the match returned
145 // may depend on the UTF8 flag, so always use it when matching
146 // UTF8 text. E.g., "." will match one byte normally but with UTF8
147 // set may match up to three bytes of a multi-byte character.
148 //
149 // Example:
150 // pcrecpp::RE_Options options;
151 // options.set_utf8();
152 // pcrecpp::RE re(utf8_pattern, options);
153 // re.FullMatch(utf8_string);
154 //
155 // Example: using the convenience function UTF8():
156 // pcrecpp::RE re(utf8_pattern, pcrecpp::UTF8());
157 // re.FullMatch(utf8_string);
158 //
159 // NOTE: The UTF8 option is ignored if pcre was not configured with the
160 // --enable-utf8 flag.
161 //
162 // -----------------------------------------------------------------------
163 // PASSING MODIFIERS TO THE REGULAR EXPRESSION ENGINE
164 //
165 // PCRE defines some modifiers to change the behavior of the regular
166 // expression engine.
167 // The C++ wrapper defines an auxiliary class, RE_Options, as a vehicle
168 // to pass such modifiers to a RE class.
169 //
170 // Currently, the following modifiers are supported
171 //
172 // modifier description Perl corresponding
173 //
174 // PCRE_CASELESS case insensitive match /i
175 // PCRE_MULTILINE multiple lines match /m
176 // PCRE_DOTALL dot matches newlines /s
177 // PCRE_DOLLAR_ENDONLY $ matches only at end N/A
178 // PCRE_EXTRA strict escape parsing N/A
179 // PCRE_EXTENDED ignore whitespaces /x
180 // PCRE_UTF8 handles UTF8 chars built-in
181 // PCRE_UNGREEDY reverses * and *? N/A
182 // PCRE_NO_AUTO_CAPTURE disables matching parens N/A (*)
183 //
184 // (For a full account on how each modifier works, please check the
185 // PCRE API reference manual).
186 //
187 // (*) Both Perl and PCRE allow non matching parentheses by means of the
188 // "?:" modifier within the pattern itself. e.g. (?:ab|cd) does not
189 // capture, while (ab|cd) does.
190 //
191 // For each modifier, there are two member functions whose name is made
192 // out of the modifier in lowercase, without the "PCRE_" prefix. For
193 // instance, PCRE_CASELESS is handled by
194 // bool caseless(),
195 // which returns true if the modifier is set, and
196 // RE_Options & set_caseless(bool),
197 // which sets or unsets the modifier.
198 //
199 // Moreover, PCRE_CONFIG_MATCH_LIMIT can be accessed through the
200 // set_match_limit() and match_limit() member functions.
201 // Setting match_limit to a non-zero value will limit the executation of
202 // pcre to keep it from doing bad things like blowing the stack or taking
203 // an eternity to return a result. A value of 5000 is good enough to stop
204 // stack blowup in a 2MB thread stack. Setting match_limit to zero will
205 // disable match limiting.
206 //
207 // Normally, to pass one or more modifiers to a RE class, you declare
208 // a RE_Options object, set the appropriate options, and pass this
209 // object to a RE constructor. Example:
210 //
211 // RE_options opt;
212 // opt.set_caseless(true);
213 //
214 // if (RE("HELLO", opt).PartialMatch("hello world")) ...
215 //
216 // RE_options has two constructors. The default constructor takes no
217 // arguments and creates a set of flags that are off by default.
218 //
219 // The optional parameter 'option_flags' is to facilitate transfer
220 // of legacy code from C programs. This lets you do
221 // RE(pattern, RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str);
222 //
223 // But new code is better off doing
224 // RE(pattern,
225 // RE_Options().set_caseless(true).set_multiline(true)).PartialMatch(str);
226 // (See below)
227 //
228 // If you are going to pass one of the most used modifiers, there are some
229 // convenience functions that return a RE_Options class with the
230 // appropriate modifier already set:
231 // CASELESS(), UTF8(), MULTILINE(), DOTALL(), EXTENDED()
232 //
233 // If you need to set several options at once, and you don't want to go
234 // through the pains of declaring a RE_Options object and setting several
235 // options, there is a parallel method that give you such ability on the
236 // fly. You can concatenate several set_xxxxx member functions, since each
237 // of them returns a reference to its class object. e.g.: to pass
238 // PCRE_CASELESS, PCRE_EXTENDED, and PCRE_MULTILINE to a RE with one
239 // statement, you may write
240 //
241 // RE(" ^ xyz \\s+ .* blah$", RE_Options()
242 // .set_caseless(true)
243 // .set_extended(true)
244 // .set_multiline(true)).PartialMatch(sometext);
245 //
246 // -----------------------------------------------------------------------
247 // SCANNING TEXT INCREMENTALLY
248 //
249 // The "Consume" operation may be useful if you want to repeatedly
250 // match regular expressions at the front of a string and skip over
251 // them as they match. This requires use of the "StringPiece" type,
252 // which represents a sub-range of a real string. Like RE, StringPiece
253 // is defined in the pcrecpp namespace.
254 //
255 // Example: read lines of the form "var = value" from a string.
256 // string contents = ...; // Fill string somehow
257 // pcrecpp::StringPiece input(contents); // Wrap in a StringPiece
258 //
259 // string var;
260 // int value;
261 // pcrecpp::RE re("(\\w+) = (\\d+)\n");
262 // while (re.Consume(&input, &var, &value)) {
263 // ...;
264 // }
265 //
266 // Each successful call to "Consume" will set "var/value", and also
267 // advance "input" so it points past the matched text.
268 //
269 // The "FindAndConsume" operation is similar to "Consume" but does not
270 // anchor your match at the beginning of the string. For example, you
271 // could extract all words from a string by repeatedly calling
272 // pcrecpp::RE("(\\w+)").FindAndConsume(&input, &word)
273 //
274 // -----------------------------------------------------------------------
275 // PARSING HEX/OCTAL/C-RADIX NUMBERS
276 //
277 // By default, if you pass a pointer to a numeric value, the
278 // corresponding text is interpreted as a base-10 number. You can
279 // instead wrap the pointer with a call to one of the operators Hex(),
280 // Octal(), or CRadix() to interpret the text in another base. The
281 // CRadix operator interprets C-style "0" (base-8) and "0x" (base-16)
282 // prefixes, but defaults to base-10.
283 //
284 // Example:
285 // int a, b, c, d;
286 // pcrecpp::RE re("(.*) (.*) (.*) (.*)");
287 // re.FullMatch("100 40 0100 0x40",
288 // pcrecpp::Octal(&a), pcrecpp::Hex(&b),
289 // pcrecpp::CRadix(&c), pcrecpp::CRadix(&d));
290 // will leave 64 in a, b, c, and d.
291 //
292 // -----------------------------------------------------------------------
293 // REPLACING PARTS OF STRINGS
294 //
295 // You can replace the first match of "pattern" in "str" with
296 // "rewrite". Within "rewrite", backslash-escaped digits (\1 to \9)
297 // can be used to insert text matching corresponding parenthesized
298 // group from the pattern. \0 in "rewrite" refers to the entire
299 // matching text. E.g.,
300 //
301 // string s = "yabba dabba doo";
302 // pcrecpp::RE("b+").Replace("d", &s);
303 //
304 // will leave "s" containing "yada dabba doo". The result is true if
305 // the pattern matches and a replacement occurs, or false otherwise.
306 //
307 // GlobalReplace() is like Replace(), except that it replaces all
308 // occurrences of the pattern in the string with the rewrite.
309 // Replacements are not subject to re-matching. E.g.,
310 //
311 // string s = "yabba dabba doo";
312 // pcrecpp::RE("b+").GlobalReplace("d", &s);
313 //
314 // will leave "s" containing "yada dada doo". It returns the number
315 // of replacements made.
316 //
317 // Extract() is like Replace(), except that if the pattern matches,
318 // "rewrite" is copied into "out" (an additional argument) with
319 // substitutions. The non-matching portions of "text" are ignored.
320 // Returns true iff a match occurred and the extraction happened
321 // successfully. If no match occurs, the string is left unaffected.
322
323
324 #include <string>
325 // These aren't technically needed here, but we include them
326 // anyway so folks who include pcrecpp.h don't have to include
327 // all these other header files as well.
328 #include <pcre.h>
329 #include <pcre_stringpiece.h>
330
331 namespace pcrecpp {
332
333 #define PCRE_SET_OR_CLEAR(b, o) \
334 if (b) all_options_ |= (o); else all_options_ &= ~(o); \
335 return *this
336
337 #define PCRE_IS_SET(o) \
338 (all_options_ & o) == o
339
340 // We convert user-passed pointers into special Arg objects
341 class Arg;
342 extern Arg no_arg;
343
344 /***** Compiling regular expressions: the RE class *****/
345
346 // RE_Options allow you to set options to be passed along to pcre,
347 // along with other options we put on top of pcre.
348 // Only 9 modifiers, plus match_limit are supported now.
349 class RE_Options {
350 public:
351 // constructor
352 RE_Options() : match_limit_(0), all_options_(0) {}
353
354 // alternative constructor.
355 // To facilitate transfer of legacy code from C programs
356 //
357 // This lets you do
358 // RE(pattern, RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str);
359 // But new code is better off doing
360 // RE(pattern,
361 // RE_Options().set_caseless(true).set_multiline(true)).PartialMatch(str);
362 RE_Options(int option_flags) : match_limit_(0), all_options_ (option_flags) {}
363 // we're fine with the default destructor, copy constructor, etc.
364
365 // accessors and mutators
366 int match_limit() const { return match_limit_; };
367 RE_Options &set_match_limit(int limit) {
368 match_limit_ = limit;
369 return *this;
370 }
371
372 bool caseless() const {
373 return PCRE_IS_SET(PCRE_CASELESS);
374 }
375 RE_Options &set_caseless(bool x) {
376 PCRE_SET_OR_CLEAR(x, PCRE_CASELESS);
377 }
378
379 bool multiline() const {
380 return PCRE_IS_SET(PCRE_MULTILINE);
381 }
382 RE_Options &set_multiline(bool x) {
383 PCRE_SET_OR_CLEAR(x, PCRE_MULTILINE);
384 }
385
386 bool dotall() const {
387 return PCRE_IS_SET(PCRE_DOTALL);
388 }
389 RE_Options &set_dotall(bool x) {
390 PCRE_SET_OR_CLEAR(x,PCRE_DOTALL);
391 }
392
393 bool extended() const {
394 return PCRE_IS_SET(PCRE_EXTENDED);
395 }
396 RE_Options &set_extended(bool x) {
397 PCRE_SET_OR_CLEAR(x,PCRE_EXTENDED);
398 }
399
400 bool dollar_endonly() const {
401 return PCRE_IS_SET(PCRE_DOLLAR_ENDONLY);
402 }
403 RE_Options &set_dollar_endonly(bool x) {
404 PCRE_SET_OR_CLEAR(x,PCRE_DOLLAR_ENDONLY);
405 }
406
407 bool extra() const {
408 return PCRE_IS_SET( PCRE_EXTRA);
409 }
410 RE_Options &set_extra(bool x) {
411 PCRE_SET_OR_CLEAR(x, PCRE_EXTRA);
412 }
413
414 bool ungreedy() const {
415 return PCRE_IS_SET(PCRE_UNGREEDY);
416 }
417 RE_Options &set_ungreedy(bool x) {
418 PCRE_SET_OR_CLEAR(x, PCRE_UNGREEDY);
419 }
420
421 bool utf8() const {
422 return PCRE_IS_SET(PCRE_UTF8);
423 }
424 RE_Options &set_utf8(bool x) {
425 PCRE_SET_OR_CLEAR(x, PCRE_UTF8);
426 }
427
428 bool no_auto_capture() const {
429 return PCRE_IS_SET(PCRE_NO_AUTO_CAPTURE);
430 }
431 RE_Options &set_no_auto_capture(bool x) {
432 PCRE_SET_OR_CLEAR(x, PCRE_NO_AUTO_CAPTURE);
433 }
434
435 RE_Options &set_all_options(int opt) {
436 all_options_ = opt;
437 return *this;
438 }
439 int all_options() const {
440 return all_options_ ;
441 }
442
443 // TODO: add other pcre flags
444
445 private:
446 int match_limit_;
447 int all_options_;
448 };
449
450 // These functions return some common RE_Options
451 static inline RE_Options UTF8() {
452 return RE_Options().set_utf8(true);
453 }
454
455 static inline RE_Options CASELESS() {
456 return RE_Options().set_caseless(true);
457 }
458 static inline RE_Options MULTILINE() {
459 return RE_Options().set_multiline(true);
460 }
461
462 static inline RE_Options DOTALL() {
463 return RE_Options().set_dotall(true);
464 }
465
466 static inline RE_Options EXTENDED() {
467 return RE_Options().set_extended(true);
468 }
469
470 // Interface for regular expression matching. Also corresponds to a
471 // pre-compiled regular expression. An "RE" object is safe for
472 // concurrent use by multiple threads.
473 class RE {
474 public:
475 // We provide implicit conversions from strings so that users can
476 // pass in a string or a "const char*" wherever an "RE" is expected.
477 RE(const char* pat) { Init(pat, NULL); }
478 RE(const char *pat, const RE_Options& option) { Init(pat, &option); }
479 RE(const string& pat) { Init(pat.c_str(), NULL); }
480 RE(const string& pat, const RE_Options& option) { Init(pat.c_str(), &option); }
481
482 ~RE();
483
484 // The string specification for this RE. E.g.
485 // RE re("ab*c?d+");
486 // re.pattern(); // "ab*c?d+"
487 const string& pattern() const { return pattern_; }
488
489 // If RE could not be created properly, returns an error string.
490 // Else returns the empty string.
491 const string& error() const { return *error_; }
492
493 /***** The useful part: the matching interface *****/
494
495 // This is provided so one can do pattern.ReplaceAll() just as
496 // easily as ReplaceAll(pattern-text, ....)
497
498 bool FullMatch(const StringPiece& text,
499 const Arg& ptr1 = no_arg,
500 const Arg& ptr2 = no_arg,
501 const Arg& ptr3 = no_arg,
502 const Arg& ptr4 = no_arg,
503 const Arg& ptr5 = no_arg,
504 const Arg& ptr6 = no_arg,
505 const Arg& ptr7 = no_arg,
506 const Arg& ptr8 = no_arg,
507 const Arg& ptr9 = no_arg,
508 const Arg& ptr10 = no_arg,
509 const Arg& ptr11 = no_arg,
510 const Arg& ptr12 = no_arg,
511 const Arg& ptr13 = no_arg,
512 const Arg& ptr14 = no_arg,
513 const Arg& ptr15 = no_arg,
514 const Arg& ptr16 = no_arg) const;
515
516 bool PartialMatch(const StringPiece& text,
517 const Arg& ptr1 = no_arg,
518 const Arg& ptr2 = no_arg,
519 const Arg& ptr3 = no_arg,
520 const Arg& ptr4 = no_arg,
521 const Arg& ptr5 = no_arg,
522 const Arg& ptr6 = no_arg,
523 const Arg& ptr7 = no_arg,
524 const Arg& ptr8 = no_arg,
525 const Arg& ptr9 = no_arg,
526 const Arg& ptr10 = no_arg,
527 const Arg& ptr11 = no_arg,
528 const Arg& ptr12 = no_arg,
529 const Arg& ptr13 = no_arg,
530 const Arg& ptr14 = no_arg,
531 const Arg& ptr15 = no_arg,
532 const Arg& ptr16 = no_arg) const;
533
534 bool Consume(StringPiece* input,
535 const Arg& ptr1 = no_arg,
536 const Arg& ptr2 = no_arg,
537 const Arg& ptr3 = no_arg,
538 const Arg& ptr4 = no_arg,
539 const Arg& ptr5 = no_arg,
540 const Arg& ptr6 = no_arg,
541 const Arg& ptr7 = no_arg,
542 const Arg& ptr8 = no_arg,
543 const Arg& ptr9 = no_arg,
544 const Arg& ptr10 = no_arg,
545 const Arg& ptr11 = no_arg,
546 const Arg& ptr12 = no_arg,
547 const Arg& ptr13 = no_arg,
548 const Arg& ptr14 = no_arg,
549 const Arg& ptr15 = no_arg,
550 const Arg& ptr16 = no_arg) const;
551
552 bool FindAndConsume(StringPiece* input,
553 const Arg& ptr1 = no_arg,
554 const Arg& ptr2 = no_arg,
555 const Arg& ptr3 = no_arg,
556 const Arg& ptr4 = no_arg,
557 const Arg& ptr5 = no_arg,
558 const Arg& ptr6 = no_arg,
559 const Arg& ptr7 = no_arg,
560 const Arg& ptr8 = no_arg,
561 const Arg& ptr9 = no_arg,
562 const Arg& ptr10 = no_arg,
563 const Arg& ptr11 = no_arg,
564 const Arg& ptr12 = no_arg,
565 const Arg& ptr13 = no_arg,
566 const Arg& ptr14 = no_arg,
567 const Arg& ptr15 = no_arg,
568 const Arg& ptr16 = no_arg) const;
569
570 bool Replace(const StringPiece& rewrite,
571 string *str) const;
572
573 int GlobalReplace(const StringPiece& rewrite,
574 string *str) const;
575
576 bool Extract(const StringPiece &rewrite,
577 const StringPiece &text,
578 string *out) const;
579
580 /***** Generic matching interface *****/
581
582 // Type of match (TODO: Should be restructured as part of RE_Options)
583 enum Anchor {
584 UNANCHORED, // No anchoring
585 ANCHOR_START, // Anchor at start only
586 ANCHOR_BOTH // Anchor at start and end
587 };
588
589 // General matching routine. Stores the length of the match in
590 // "*consumed" if successful.
591 bool DoMatch(const StringPiece& text,
592 Anchor anchor,
593 int* consumed,
594 const Arg* const* args, int n) const;
595
596 // Return the number of capturing subpatterns, or -1 if the
597 // regexp wasn't valid on construction.
598 int NumberOfCapturingGroups();
599
600 private:
601
602 void Init(const char* pattern, const RE_Options* options);
603
604 // Match against "text", filling in "vec" (up to "vecsize" * 2/3) with
605 // pairs of integers for the beginning and end positions of matched
606 // text. The first pair corresponds to the entire matched text;
607 // subsequent pairs correspond, in order, to parentheses-captured
608 // matches. Returns the number of pairs (one more than the number of
609 // the last subpattern with a match) if matching was successful
610 // and zero if the match failed.
611 // I.e. for RE("(foo)|(bar)|(baz)") it will return 2, 3, and 4 when matching
612 // against "foo", "bar", and "baz" respectively.
613 // When matching RE("(foo)|hello") against "hello", it will return 1.
614 // But the values for all subpattern are filled in into "vec".
615 int TryMatch(const StringPiece& text,
616 int startpos,
617 Anchor anchor,
618 int *vec,
619 int vecsize) const;
620
621 // Append the "rewrite" string, with backslash subsitutions from "text"
622 // and "vec", to string "out".
623 bool Rewrite(string *out,
624 const StringPiece& rewrite,
625 const StringPiece& text,
626 int *vec,
627 int veclen) const;
628
629 // internal implementation for DoMatch
630 bool DoMatchImpl(const StringPiece& text,
631 Anchor anchor,
632 int* consumed,
633 const Arg* const args[],
634 int n,
635 int* vec,
636 int vecsize) const;
637
638 // Compile the regexp for the specified anchoring mode
639 pcre* Compile(Anchor anchor);
640
641 string pattern_;
642 RE_Options options_;
643 pcre* re_full_; // For full matches
644 pcre* re_partial_; // For partial matches
645 const string* error_; // Error indicator (or points to empty string)
646 int match_limit_; // limit on execution resources
647
648 // Don't allow the default copy or assignment constructors --
649 // they're expensive and too easy to do by accident.
650 RE(const RE&);
651 void operator=(const RE&);
652 };
653
654
655 /***** Implementation details *****/
656
657 // Hex/Octal/Binary?
658
659 // Special class for parsing into objects that define a ParseFrom() method
660 template <class T>
661 class _RE_MatchObject {
662 public:
663 static inline bool Parse(const char* str, int n, void* dest) {
664 T* object = reinterpret_cast<T*>(dest);
665 return object->ParseFrom(str, n);
666 }
667 };
668
669 class Arg {
670 public:
671 // Empty constructor so we can declare arrays of Arg
672 Arg();
673
674 // Constructor specially designed for NULL arguments
675 Arg(void*);
676
677 typedef bool (*Parser)(const char* str, int n, void* dest);
678
679 // Type-specific parsers
680 #define PCRE_MAKE_PARSER(type,name) \
681 Arg(type* p) : arg_(p), parser_(name) { } \
682 Arg(type* p, Parser parser) : arg_(p), parser_(parser) { }
683
684
685 PCRE_MAKE_PARSER(char, parse_char);
686 PCRE_MAKE_PARSER(unsigned char, parse_uchar);
687 PCRE_MAKE_PARSER(short, parse_short);
688 PCRE_MAKE_PARSER(unsigned short, parse_ushort);
689 PCRE_MAKE_PARSER(int, parse_int);
690 PCRE_MAKE_PARSER(unsigned int, parse_uint);
691 PCRE_MAKE_PARSER(long, parse_long);
692 PCRE_MAKE_PARSER(unsigned long, parse_ulong);
693 #if @pcre_has_long_long@
694 PCRE_MAKE_PARSER(long long, parse_longlong);
695 #endif
696 #if @pcre_has_ulong_long@
697 PCRE_MAKE_PARSER(unsigned long long, parse_ulonglong);
698 #endif
699 PCRE_MAKE_PARSER(float, parse_float);
700 PCRE_MAKE_PARSER(double, parse_double);
701 PCRE_MAKE_PARSER(string, parse_string);
702 PCRE_MAKE_PARSER(StringPiece, parse_stringpiece);
703
704 #undef PCRE_MAKE_PARSER
705
706 // Generic constructor
707 template <class T> Arg(T*, Parser parser);
708 // Generic constructor template
709 template <class T> Arg(T* p)
710 : arg_(p), parser_(_RE_MatchObject<T>::Parse) {
711 }
712
713 // Parse the data
714 bool Parse(const char* str, int n) const;
715
716 private:
717 void* arg_;
718 Parser parser_;
719
720 static bool parse_null (const char* str, int n, void* dest);
721 static bool parse_char (const char* str, int n, void* dest);
722 static bool parse_uchar (const char* str, int n, void* dest);
723 static bool parse_float (const char* str, int n, void* dest);
724 static bool parse_double (const char* str, int n, void* dest);
725 static bool parse_string (const char* str, int n, void* dest);
726 static bool parse_stringpiece (const char* str, int n, void* dest);
727
728 #define PCRE_DECLARE_INTEGER_PARSER(name) \
729 private: \
730 static bool parse_ ## name(const char* str, int n, void* dest); \
731 static bool parse_ ## name ## _radix( \
732 const char* str, int n, void* dest, int radix); \
733 public: \
734 static bool parse_ ## name ## _hex(const char* str, int n, void* dest); \
735 static bool parse_ ## name ## _octal(const char* str, int n, void* dest); \
736 static bool parse_ ## name ## _cradix(const char* str, int n, void* dest)
737
738 PCRE_DECLARE_INTEGER_PARSER(short);
739 PCRE_DECLARE_INTEGER_PARSER(ushort);
740 PCRE_DECLARE_INTEGER_PARSER(int);
741 PCRE_DECLARE_INTEGER_PARSER(uint);
742 PCRE_DECLARE_INTEGER_PARSER(long);
743 PCRE_DECLARE_INTEGER_PARSER(ulong);
744 PCRE_DECLARE_INTEGER_PARSER(longlong);
745 PCRE_DECLARE_INTEGER_PARSER(ulonglong);
746
747 #undef PCRE_DECLARE_INTEGER_PARSER
748 };
749
750 inline Arg::Arg() : arg_(NULL), parser_(parse_null) { }
751 inline Arg::Arg(void* p) : arg_(p), parser_(parse_null) { }
752
753 inline bool Arg::Parse(const char* str, int n) const {
754 return (*parser_)(str, n, arg_);
755 }
756
757 // This part of the parser, appropriate only for ints, deals with bases
758 #define MAKE_INTEGER_PARSER(type, name) \
759 inline Arg Hex(type* ptr) { \
760 return Arg(ptr, Arg::parse_ ## name ## _hex); } \
761 inline Arg Octal(type* ptr) { \
762 return Arg(ptr, Arg::parse_ ## name ## _octal); } \
763 inline Arg CRadix(type* ptr) { \
764 return Arg(ptr, Arg::parse_ ## name ## _cradix); }
765
766 MAKE_INTEGER_PARSER(short, short);
767 MAKE_INTEGER_PARSER(unsigned short, ushort);
768 MAKE_INTEGER_PARSER(int, int);
769 MAKE_INTEGER_PARSER(unsigned int, uint);
770 MAKE_INTEGER_PARSER(long, long);
771 MAKE_INTEGER_PARSER(unsigned long, ulong);
772 #if @pcre_has_long_long@
773 MAKE_INTEGER_PARSER(long long, longlong);
774 #endif
775 #if @pcre_has_ulong_long@
776 MAKE_INTEGER_PARSER(unsigned long long, ulonglong);
777 #endif
778
779 #undef PCRE_IS_SET
780 #undef PCRE_SET_OR_CLEAR
781 #undef MAKE_INTEGER_PARSER
782
783 } // namespace pcrecpp
784
785
786 #endif /* _PCRE_REGEXP_H */

  ViewVC Help
Powered by ViewVC 1.1.5