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

Contents of /code/tags/pcre-7.5/pcrecpp.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 87 - (show annotations) (download)
Sat Feb 24 21:41:21 2007 UTC (7 years, 7 months ago) by nigel
Original Path: code/trunk/pcrecpp.h
File MIME type: text/plain
File size: 24956 byte(s)
Load pcre-6.5 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 _PCRECPP_H
34 #define _PCRECPP_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_EXTRA_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. Alternately, you can set match_limit_recursion()
206 // which uses PCRE_EXTRA_MATCH_LIMIT_RECURSION to limit how much pcre
207 // recurses. match_limit() caps the number of matches pcre does;
208 // match_limit_recrusion() caps the depth of recursion.
209 //
210 // Normally, to pass one or more modifiers to a RE class, you declare
211 // a RE_Options object, set the appropriate options, and pass this
212 // object to a RE constructor. Example:
213 //
214 // RE_options opt;
215 // opt.set_caseless(true);
216 //
217 // if (RE("HELLO", opt).PartialMatch("hello world")) ...
218 //
219 // RE_options has two constructors. The default constructor takes no
220 // arguments and creates a set of flags that are off by default.
221 //
222 // The optional parameter 'option_flags' is to facilitate transfer
223 // of legacy code from C programs. This lets you do
224 // RE(pattern, RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str);
225 //
226 // But new code is better off doing
227 // RE(pattern,
228 // RE_Options().set_caseless(true).set_multiline(true)).PartialMatch(str);
229 // (See below)
230 //
231 // If you are going to pass one of the most used modifiers, there are some
232 // convenience functions that return a RE_Options class with the
233 // appropriate modifier already set:
234 // CASELESS(), UTF8(), MULTILINE(), DOTALL(), EXTENDED()
235 //
236 // If you need to set several options at once, and you don't want to go
237 // through the pains of declaring a RE_Options object and setting several
238 // options, there is a parallel method that give you such ability on the
239 // fly. You can concatenate several set_xxxxx member functions, since each
240 // of them returns a reference to its class object. e.g.: to pass
241 // PCRE_CASELESS, PCRE_EXTENDED, and PCRE_MULTILINE to a RE with one
242 // statement, you may write
243 //
244 // RE(" ^ xyz \\s+ .* blah$", RE_Options()
245 // .set_caseless(true)
246 // .set_extended(true)
247 // .set_multiline(true)).PartialMatch(sometext);
248 //
249 // -----------------------------------------------------------------------
250 // SCANNING TEXT INCREMENTALLY
251 //
252 // The "Consume" operation may be useful if you want to repeatedly
253 // match regular expressions at the front of a string and skip over
254 // them as they match. This requires use of the "StringPiece" type,
255 // which represents a sub-range of a real string. Like RE, StringPiece
256 // is defined in the pcrecpp namespace.
257 //
258 // Example: read lines of the form "var = value" from a string.
259 // string contents = ...; // Fill string somehow
260 // pcrecpp::StringPiece input(contents); // Wrap in a StringPiece
261 //
262 // string var;
263 // int value;
264 // pcrecpp::RE re("(\\w+) = (\\d+)\n");
265 // while (re.Consume(&input, &var, &value)) {
266 // ...;
267 // }
268 //
269 // Each successful call to "Consume" will set "var/value", and also
270 // advance "input" so it points past the matched text.
271 //
272 // The "FindAndConsume" operation is similar to "Consume" but does not
273 // anchor your match at the beginning of the string. For example, you
274 // could extract all words from a string by repeatedly calling
275 // pcrecpp::RE("(\\w+)").FindAndConsume(&input, &word)
276 //
277 // -----------------------------------------------------------------------
278 // PARSING HEX/OCTAL/C-RADIX NUMBERS
279 //
280 // By default, if you pass a pointer to a numeric value, the
281 // corresponding text is interpreted as a base-10 number. You can
282 // instead wrap the pointer with a call to one of the operators Hex(),
283 // Octal(), or CRadix() to interpret the text in another base. The
284 // CRadix operator interprets C-style "0" (base-8) and "0x" (base-16)
285 // prefixes, but defaults to base-10.
286 //
287 // Example:
288 // int a, b, c, d;
289 // pcrecpp::RE re("(.*) (.*) (.*) (.*)");
290 // re.FullMatch("100 40 0100 0x40",
291 // pcrecpp::Octal(&a), pcrecpp::Hex(&b),
292 // pcrecpp::CRadix(&c), pcrecpp::CRadix(&d));
293 // will leave 64 in a, b, c, and d.
294 //
295 // -----------------------------------------------------------------------
296 // REPLACING PARTS OF STRINGS
297 //
298 // You can replace the first match of "pattern" in "str" with
299 // "rewrite". Within "rewrite", backslash-escaped digits (\1 to \9)
300 // can be used to insert text matching corresponding parenthesized
301 // group from the pattern. \0 in "rewrite" refers to the entire
302 // matching text. E.g.,
303 //
304 // string s = "yabba dabba doo";
305 // pcrecpp::RE("b+").Replace("d", &s);
306 //
307 // will leave "s" containing "yada dabba doo". The result is true if
308 // the pattern matches and a replacement occurs, or false otherwise.
309 //
310 // GlobalReplace() is like Replace(), except that it replaces all
311 // occurrences of the pattern in the string with the rewrite.
312 // Replacements are not subject to re-matching. E.g.,
313 //
314 // string s = "yabba dabba doo";
315 // pcrecpp::RE("b+").GlobalReplace("d", &s);
316 //
317 // will leave "s" containing "yada dada doo". It returns the number
318 // of replacements made.
319 //
320 // Extract() is like Replace(), except that if the pattern matches,
321 // "rewrite" is copied into "out" (an additional argument) with
322 // substitutions. The non-matching portions of "text" are ignored.
323 // Returns true iff a match occurred and the extraction happened
324 // successfully. If no match occurs, the string is left unaffected.
325
326
327 #include <string>
328 #include <pcrecpparg.h> // defines the Arg class
329 // These aren't technically needed here, but we include them
330 // anyway so folks who include pcrecpp.h don't have to include
331 // all these other header files as well.
332 #include <pcre.h>
333 #include <pcre_stringpiece.h>
334
335 namespace pcrecpp {
336
337 #define PCRE_SET_OR_CLEAR(b, o) \
338 if (b) all_options_ |= (o); else all_options_ &= ~(o); \
339 return *this
340
341 #define PCRE_IS_SET(o) \
342 (all_options_ & o) == o
343
344 // We convert user-passed pointers into special Arg objects
345 extern Arg no_arg;
346
347 /***** Compiling regular expressions: the RE class *****/
348
349 // RE_Options allow you to set options to be passed along to pcre,
350 // along with other options we put on top of pcre.
351 // Only 9 modifiers, plus match_limit and match_limit_recursion,
352 // are supported now.
353 class RE_Options {
354 public:
355 // constructor
356 RE_Options() : match_limit_(0), match_limit_recursion_(0), all_options_(0) {}
357
358 // alternative constructor.
359 // To facilitate transfer of legacy code from C programs
360 //
361 // This lets you do
362 // RE(pattern, RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str);
363 // But new code is better off doing
364 // RE(pattern,
365 // RE_Options().set_caseless(true).set_multiline(true)).PartialMatch(str);
366 RE_Options(int option_flags) : match_limit_(0), match_limit_recursion_(0),
367 all_options_(option_flags) {}
368 // we're fine with the default destructor, copy constructor, etc.
369
370 // accessors and mutators
371 int match_limit() const { return match_limit_; };
372 RE_Options &set_match_limit(int limit) {
373 match_limit_ = limit;
374 return *this;
375 }
376
377 int match_limit_recursion() const { return match_limit_recursion_; };
378 RE_Options &set_match_limit_recursion(int limit) {
379 match_limit_recursion_ = limit;
380 return *this;
381 }
382
383 bool caseless() const {
384 return PCRE_IS_SET(PCRE_CASELESS);
385 }
386 RE_Options &set_caseless(bool x) {
387 PCRE_SET_OR_CLEAR(x, PCRE_CASELESS);
388 }
389
390 bool multiline() const {
391 return PCRE_IS_SET(PCRE_MULTILINE);
392 }
393 RE_Options &set_multiline(bool x) {
394 PCRE_SET_OR_CLEAR(x, PCRE_MULTILINE);
395 }
396
397 bool dotall() const {
398 return PCRE_IS_SET(PCRE_DOTALL);
399 }
400 RE_Options &set_dotall(bool x) {
401 PCRE_SET_OR_CLEAR(x,PCRE_DOTALL);
402 }
403
404 bool extended() const {
405 return PCRE_IS_SET(PCRE_EXTENDED);
406 }
407 RE_Options &set_extended(bool x) {
408 PCRE_SET_OR_CLEAR(x,PCRE_EXTENDED);
409 }
410
411 bool dollar_endonly() const {
412 return PCRE_IS_SET(PCRE_DOLLAR_ENDONLY);
413 }
414 RE_Options &set_dollar_endonly(bool x) {
415 PCRE_SET_OR_CLEAR(x,PCRE_DOLLAR_ENDONLY);
416 }
417
418 bool extra() const {
419 return PCRE_IS_SET( PCRE_EXTRA);
420 }
421 RE_Options &set_extra(bool x) {
422 PCRE_SET_OR_CLEAR(x, PCRE_EXTRA);
423 }
424
425 bool ungreedy() const {
426 return PCRE_IS_SET(PCRE_UNGREEDY);
427 }
428 RE_Options &set_ungreedy(bool x) {
429 PCRE_SET_OR_CLEAR(x, PCRE_UNGREEDY);
430 }
431
432 bool utf8() const {
433 return PCRE_IS_SET(PCRE_UTF8);
434 }
435 RE_Options &set_utf8(bool x) {
436 PCRE_SET_OR_CLEAR(x, PCRE_UTF8);
437 }
438
439 bool no_auto_capture() const {
440 return PCRE_IS_SET(PCRE_NO_AUTO_CAPTURE);
441 }
442 RE_Options &set_no_auto_capture(bool x) {
443 PCRE_SET_OR_CLEAR(x, PCRE_NO_AUTO_CAPTURE);
444 }
445
446 RE_Options &set_all_options(int opt) {
447 all_options_ = opt;
448 return *this;
449 }
450 int all_options() const {
451 return all_options_ ;
452 }
453
454 // TODO: add other pcre flags
455
456 private:
457 int match_limit_;
458 int match_limit_recursion_;
459 int all_options_;
460 };
461
462 // These functions return some common RE_Options
463 static inline RE_Options UTF8() {
464 return RE_Options().set_utf8(true);
465 }
466
467 static inline RE_Options CASELESS() {
468 return RE_Options().set_caseless(true);
469 }
470 static inline RE_Options MULTILINE() {
471 return RE_Options().set_multiline(true);
472 }
473
474 static inline RE_Options DOTALL() {
475 return RE_Options().set_dotall(true);
476 }
477
478 static inline RE_Options EXTENDED() {
479 return RE_Options().set_extended(true);
480 }
481
482 // Interface for regular expression matching. Also corresponds to a
483 // pre-compiled regular expression. An "RE" object is safe for
484 // concurrent use by multiple threads.
485 class RE {
486 public:
487 // We provide implicit conversions from strings so that users can
488 // pass in a string or a "const char*" wherever an "RE" is expected.
489 RE(const char* pat) { Init(pat, NULL); }
490 RE(const char *pat, const RE_Options& option) { Init(pat, &option); }
491 RE(const string& pat) { Init(pat.c_str(), NULL); }
492 RE(const string& pat, const RE_Options& option) { Init(pat.c_str(), &option); }
493
494 ~RE();
495
496 // The string specification for this RE. E.g.
497 // RE re("ab*c?d+");
498 // re.pattern(); // "ab*c?d+"
499 const string& pattern() const { return pattern_; }
500
501 // If RE could not be created properly, returns an error string.
502 // Else returns the empty string.
503 const string& error() const { return *error_; }
504
505 /***** The useful part: the matching interface *****/
506
507 // This is provided so one can do pattern.ReplaceAll() just as
508 // easily as ReplaceAll(pattern-text, ....)
509
510 bool FullMatch(const StringPiece& text,
511 const Arg& ptr1 = no_arg,
512 const Arg& ptr2 = no_arg,
513 const Arg& ptr3 = no_arg,
514 const Arg& ptr4 = no_arg,
515 const Arg& ptr5 = no_arg,
516 const Arg& ptr6 = no_arg,
517 const Arg& ptr7 = no_arg,
518 const Arg& ptr8 = no_arg,
519 const Arg& ptr9 = no_arg,
520 const Arg& ptr10 = no_arg,
521 const Arg& ptr11 = no_arg,
522 const Arg& ptr12 = no_arg,
523 const Arg& ptr13 = no_arg,
524 const Arg& ptr14 = no_arg,
525 const Arg& ptr15 = no_arg,
526 const Arg& ptr16 = no_arg) const;
527
528 bool PartialMatch(const StringPiece& text,
529 const Arg& ptr1 = no_arg,
530 const Arg& ptr2 = no_arg,
531 const Arg& ptr3 = no_arg,
532 const Arg& ptr4 = no_arg,
533 const Arg& ptr5 = no_arg,
534 const Arg& ptr6 = no_arg,
535 const Arg& ptr7 = no_arg,
536 const Arg& ptr8 = no_arg,
537 const Arg& ptr9 = no_arg,
538 const Arg& ptr10 = no_arg,
539 const Arg& ptr11 = no_arg,
540 const Arg& ptr12 = no_arg,
541 const Arg& ptr13 = no_arg,
542 const Arg& ptr14 = no_arg,
543 const Arg& ptr15 = no_arg,
544 const Arg& ptr16 = no_arg) const;
545
546 bool Consume(StringPiece* input,
547 const Arg& ptr1 = no_arg,
548 const Arg& ptr2 = no_arg,
549 const Arg& ptr3 = no_arg,
550 const Arg& ptr4 = no_arg,
551 const Arg& ptr5 = no_arg,
552 const Arg& ptr6 = no_arg,
553 const Arg& ptr7 = no_arg,
554 const Arg& ptr8 = no_arg,
555 const Arg& ptr9 = no_arg,
556 const Arg& ptr10 = no_arg,
557 const Arg& ptr11 = no_arg,
558 const Arg& ptr12 = no_arg,
559 const Arg& ptr13 = no_arg,
560 const Arg& ptr14 = no_arg,
561 const Arg& ptr15 = no_arg,
562 const Arg& ptr16 = no_arg) const;
563
564 bool FindAndConsume(StringPiece* input,
565 const Arg& ptr1 = no_arg,
566 const Arg& ptr2 = no_arg,
567 const Arg& ptr3 = no_arg,
568 const Arg& ptr4 = no_arg,
569 const Arg& ptr5 = no_arg,
570 const Arg& ptr6 = no_arg,
571 const Arg& ptr7 = no_arg,
572 const Arg& ptr8 = no_arg,
573 const Arg& ptr9 = no_arg,
574 const Arg& ptr10 = no_arg,
575 const Arg& ptr11 = no_arg,
576 const Arg& ptr12 = no_arg,
577 const Arg& ptr13 = no_arg,
578 const Arg& ptr14 = no_arg,
579 const Arg& ptr15 = no_arg,
580 const Arg& ptr16 = no_arg) const;
581
582 bool Replace(const StringPiece& rewrite,
583 string *str) const;
584
585 int GlobalReplace(const StringPiece& rewrite,
586 string *str) const;
587
588 bool Extract(const StringPiece &rewrite,
589 const StringPiece &text,
590 string *out) const;
591
592 /***** Generic matching interface *****/
593
594 // Type of match (TODO: Should be restructured as part of RE_Options)
595 enum Anchor {
596 UNANCHORED, // No anchoring
597 ANCHOR_START, // Anchor at start only
598 ANCHOR_BOTH // Anchor at start and end
599 };
600
601 // General matching routine. Stores the length of the match in
602 // "*consumed" if successful.
603 bool DoMatch(const StringPiece& text,
604 Anchor anchor,
605 int* consumed,
606 const Arg* const* args, int n) const;
607
608 // Return the number of capturing subpatterns, or -1 if the
609 // regexp wasn't valid on construction.
610 int NumberOfCapturingGroups() const;
611
612 private:
613
614 void Init(const char* pattern, const RE_Options* options);
615
616 // Match against "text", filling in "vec" (up to "vecsize" * 2/3) with
617 // pairs of integers for the beginning and end positions of matched
618 // text. The first pair corresponds to the entire matched text;
619 // subsequent pairs correspond, in order, to parentheses-captured
620 // matches. Returns the number of pairs (one more than the number of
621 // the last subpattern with a match) if matching was successful
622 // and zero if the match failed.
623 // I.e. for RE("(foo)|(bar)|(baz)") it will return 2, 3, and 4 when matching
624 // against "foo", "bar", and "baz" respectively.
625 // When matching RE("(foo)|hello") against "hello", it will return 1.
626 // But the values for all subpattern are filled in into "vec".
627 int TryMatch(const StringPiece& text,
628 int startpos,
629 Anchor anchor,
630 int *vec,
631 int vecsize) const;
632
633 // Append the "rewrite" string, with backslash subsitutions from "text"
634 // and "vec", to string "out".
635 bool Rewrite(string *out,
636 const StringPiece& rewrite,
637 const StringPiece& text,
638 int *vec,
639 int veclen) const;
640
641 // internal implementation for DoMatch
642 bool DoMatchImpl(const StringPiece& text,
643 Anchor anchor,
644 int* consumed,
645 const Arg* const args[],
646 int n,
647 int* vec,
648 int vecsize) const;
649
650 // Compile the regexp for the specified anchoring mode
651 pcre* Compile(Anchor anchor);
652
653 string pattern_;
654 RE_Options options_;
655 pcre* re_full_; // For full matches
656 pcre* re_partial_; // For partial matches
657 const string* error_; // Error indicator (or points to empty string)
658
659 // Don't allow the default copy or assignment constructors --
660 // they're expensive and too easy to do by accident.
661 RE(const RE&);
662 void operator=(const RE&);
663 };
664
665 } // namespace pcrecpp
666
667 #endif /* _PCRECPP_H */

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12