pcrecpp.h

Go to the documentation of this file.
00001 // Copyright (c) 2005, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: Sanjay Ghemawat
00031 // Support for PCRE_XXX modifiers added by Giuseppe Maxia, July 2005
00032 
00033 #ifndef _PCRECPP_H
00034 #define _PCRECPP_H
00035 
00036 // C++ interface to the pcre regular-expression library.  RE supports
00037 // Perl-style regular expressions (with extensions like \d, \w, \s,
00038 // ...).
00039 //
00040 // -----------------------------------------------------------------------
00041 // REGEXP SYNTAX:
00042 //
00043 // This module is part of the pcre library and hence supports its syntax
00044 // for regular expressions.
00045 //
00046 // The syntax is pretty similar to Perl's.  For those not familiar
00047 // with Perl's regular expressions, here are some examples of the most
00048 // commonly used extensions:
00049 //
00050 //   "hello (\\w+) world"  -- \w matches a "word" character
00051 //   "version (\\d+)"      -- \d matches a digit
00052 //   "hello\\s+world"      -- \s matches any whitespace character
00053 //   "\\b(\\w+)\\b"        -- \b matches empty string at a word boundary
00054 //   "(?i)hello"           -- (?i) turns on case-insensitive matching
00055 //   "/\\*(.*?)\\*/"       -- .*? matches . minimum no. of times possible
00056 //
00057 // -----------------------------------------------------------------------
00058 // MATCHING INTERFACE:
00059 //
00060 // The "FullMatch" operation checks that supplied text matches a
00061 // supplied pattern exactly.
00062 //
00063 // Example: successful match
00064 //    pcrecpp::RE re("h.*o");
00065 //    re.FullMatch("hello");
00066 //
00067 // Example: unsuccessful match (requires full match):
00068 //    pcrecpp::RE re("e");
00069 //    !re.FullMatch("hello");
00070 //
00071 // Example: creating a temporary RE object:
00072 //    pcrecpp::RE("h.*o").FullMatch("hello");
00073 //
00074 // You can pass in a "const char*" or a "string" for "text".  The
00075 // examples below tend to use a const char*.
00076 //
00077 // You can, as in the different examples above, store the RE object
00078 // explicitly in a variable or use a temporary RE object.  The
00079 // examples below use one mode or the other arbitrarily.  Either
00080 // could correctly be used for any of these examples.
00081 //
00082 // -----------------------------------------------------------------------
00083 // MATCHING WITH SUB-STRING EXTRACTION:
00084 //
00085 // You can supply extra pointer arguments to extract matched subpieces.
00086 //
00087 // Example: extracts "ruby" into "s" and 1234 into "i"
00088 //    int i;
00089 //    string s;
00090 //    pcrecpp::RE re("(\\w+):(\\d+)");
00091 //    re.FullMatch("ruby:1234", &s, &i);
00092 //
00093 // Example: does not try to extract any extra sub-patterns
00094 //    re.FullMatch("ruby:1234", &s);
00095 //
00096 // Example: does not try to extract into NULL
00097 //    re.FullMatch("ruby:1234", NULL, &i);
00098 //
00099 // Example: integer overflow causes failure
00100 //    !re.FullMatch("ruby:1234567891234", NULL, &i);
00101 //
00102 // Example: fails because there aren't enough sub-patterns:
00103 //    !pcrecpp::RE("\\w+:\\d+").FullMatch("ruby:1234", &s);
00104 //
00105 // Example: fails because string cannot be stored in integer
00106 //    !pcrecpp::RE("(.*)").FullMatch("ruby", &i);
00107 //
00108 // The provided pointer arguments can be pointers to any scalar numeric
00109 // type, or one of
00110 //    string        (matched piece is copied to string)
00111 //    StringPiece   (StringPiece is mutated to point to matched piece)
00112 //    T             (where "bool T::ParseFrom(const char*, int)" exists)
00113 //    NULL          (the corresponding matched sub-pattern is not copied)
00114 //
00115 // CAVEAT: An optional sub-pattern that does not exist in the matched
00116 // string is assigned the empty string.  Therefore, the following will
00117 // return false (because the empty string is not a valid number):
00118 //    int number;
00119 //    pcrecpp::RE::FullMatch("abc", "[a-z]+(\\d+)?", &number);
00120 //
00121 // -----------------------------------------------------------------------
00122 // DO_MATCH
00123 //
00124 // The matching interface supports at most 16 arguments per call.
00125 // If you need more, consider using the more general interface
00126 // pcrecpp::RE::DoMatch().  See pcrecpp.h for the signature for DoMatch.
00127 //
00128 // -----------------------------------------------------------------------
00129 // PARTIAL MATCHES
00130 //
00131 // You can use the "PartialMatch" operation when you want the pattern
00132 // to match any substring of the text.
00133 //
00134 // Example: simple search for a string:
00135 //    pcrecpp::RE("ell").PartialMatch("hello");
00136 //
00137 // Example: find first number in a string:
00138 //    int number;
00139 //    pcrecpp::RE re("(\\d+)");
00140 //    re.PartialMatch("x*100 + 20", &number);
00141 //    assert(number == 100);
00142 //
00143 // -----------------------------------------------------------------------
00144 // UTF-8 AND THE MATCHING INTERFACE:
00145 //
00146 // By default, pattern and text are plain text, one byte per character.
00147 // The UTF8 flag, passed to the constructor, causes both pattern
00148 // and string to be treated as UTF-8 text, still a byte stream but
00149 // potentially multiple bytes per character. In practice, the text
00150 // is likelier to be UTF-8 than the pattern, but the match returned
00151 // may depend on the UTF8 flag, so always use it when matching
00152 // UTF8 text.  E.g., "." will match one byte normally but with UTF8
00153 // set may match up to three bytes of a multi-byte character.
00154 //
00155 // Example:
00156 //    pcrecpp::RE_Options options;
00157 //    options.set_utf8();
00158 //    pcrecpp::RE re(utf8_pattern, options);
00159 //    re.FullMatch(utf8_string);
00160 //
00161 // Example: using the convenience function UTF8():
00162 //    pcrecpp::RE re(utf8_pattern, pcrecpp::UTF8());
00163 //    re.FullMatch(utf8_string);
00164 //
00165 // NOTE: The UTF8 option is ignored if pcre was not configured with the
00166 //       --enable-utf8 flag.
00167 //
00168 // -----------------------------------------------------------------------
00169 // PASSING MODIFIERS TO THE REGULAR EXPRESSION ENGINE
00170 //
00171 // PCRE defines some modifiers to change the behavior of the regular
00172 // expression engine.
00173 // The C++ wrapper defines an auxiliary class, RE_Options, as a vehicle
00174 // to pass such modifiers to a RE class.
00175 //
00176 // Currently, the following modifiers are supported
00177 //
00178 //    modifier              description               Perl corresponding
00179 //
00180 //    PCRE_CASELESS         case insensitive match    /i
00181 //    PCRE_MULTILINE        multiple lines match      /m
00182 //    PCRE_DOTALL           dot matches newlines      /s
00183 //    PCRE_DOLLAR_ENDONLY   $ matches only at end     N/A
00184 //    PCRE_EXTRA            strict escape parsing     N/A
00185 //    PCRE_EXTENDED         ignore whitespaces        /x
00186 //    PCRE_UTF8             handles UTF8 chars        built-in
00187 //    PCRE_UNGREEDY         reverses * and *?         N/A
00188 //    PCRE_NO_AUTO_CAPTURE  disables matching parens  N/A (*)
00189 //
00190 // (For a full account on how each modifier works, please check the
00191 // PCRE API reference manual).
00192 //
00193 // (*) Both Perl and PCRE allow non matching parentheses by means of the
00194 // "?:" modifier within the pattern itself. e.g. (?:ab|cd) does not
00195 // capture, while (ab|cd) does.
00196 //
00197 // For each modifier, there are two member functions whose name is made
00198 // out of the modifier in lowercase, without the "PCRE_" prefix. For
00199 // instance, PCRE_CASELESS is handled by
00200 //    bool caseless(),
00201 // which returns true if the modifier is set, and
00202 //    RE_Options & set_caseless(bool),
00203 // which sets or unsets the modifier.
00204 //
00205 // Moreover, PCRE_EXTRA_MATCH_LIMIT can be accessed through the
00206 // set_match_limit() and match_limit() member functions.
00207 // Setting match_limit to a non-zero value will limit the executation of
00208 // pcre to keep it from doing bad things like blowing the stack or taking
00209 // an eternity to return a result.  A value of 5000 is good enough to stop
00210 // stack blowup in a 2MB thread stack.  Setting match_limit to zero will
00211 // disable match limiting.  Alternately, you can set match_limit_recursion()
00212 // which uses PCRE_EXTRA_MATCH_LIMIT_RECURSION to limit how much pcre
00213 // recurses.  match_limit() caps the number of matches pcre does;
00214 // match_limit_recrusion() caps the depth of recursion.
00215 //
00216 // Normally, to pass one or more modifiers to a RE class, you declare
00217 // a RE_Options object, set the appropriate options, and pass this
00218 // object to a RE constructor. Example:
00219 //
00220 //    RE_options opt;
00221 //    opt.set_caseless(true);
00222 //
00223 //    if (RE("HELLO", opt).PartialMatch("hello world")) ...
00224 //
00225 // RE_options has two constructors. The default constructor takes no
00226 // arguments and creates a set of flags that are off by default.
00227 //
00228 // The optional parameter 'option_flags' is to facilitate transfer
00229 // of legacy code from C programs.  This lets you do
00230 //    RE(pattern, RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str);
00231 //
00232 // But new code is better off doing
00233 //    RE(pattern,
00234 //      RE_Options().set_caseless(true).set_multiline(true)).PartialMatch(str);
00235 // (See below)
00236 //
00237 // If you are going to pass one of the most used modifiers, there are some
00238 // convenience functions that return a RE_Options class with the
00239 // appropriate modifier already set:
00240 // CASELESS(), UTF8(), MULTILINE(), DOTALL(), EXTENDED()
00241 //
00242 // If you need to set several options at once, and you don't want to go
00243 // through the pains of declaring a RE_Options object and setting several
00244 // options, there is a parallel method that give you such ability on the
00245 // fly. You can concatenate several set_xxxxx member functions, since each
00246 // of them returns a reference to its class object.  e.g.: to pass
00247 // PCRE_CASELESS, PCRE_EXTENDED, and PCRE_MULTILINE to a RE with one
00248 // statement, you may write
00249 //
00250 //    RE(" ^ xyz \\s+ .* blah$", RE_Options()
00251 //                            .set_caseless(true)
00252 //                            .set_extended(true)
00253 //                            .set_multiline(true)).PartialMatch(sometext);
00254 //
00255 // -----------------------------------------------------------------------
00256 // SCANNING TEXT INCREMENTALLY
00257 //
00258 // The "Consume" operation may be useful if you want to repeatedly
00259 // match regular expressions at the front of a string and skip over
00260 // them as they match.  This requires use of the "StringPiece" type,
00261 // which represents a sub-range of a real string.  Like RE, StringPiece
00262 // is defined in the pcrecpp namespace.
00263 //
00264 // Example: read lines of the form "var = value" from a string.
00265 //    string contents = ...;                 // Fill string somehow
00266 //    pcrecpp::StringPiece input(contents);  // Wrap in a StringPiece
00267 //
00268 //    string var;
00269 //    int value;
00270 //    pcrecpp::RE re("(\\w+) = (\\d+)\n");
00271 //    while (re.Consume(&input, &var, &value)) {
00272 //      ...;
00273 //    }
00274 //
00275 // Each successful call to "Consume" will set "var/value", and also
00276 // advance "input" so it points past the matched text.
00277 //
00278 // The "FindAndConsume" operation is similar to "Consume" but does not
00279 // anchor your match at the beginning of the string.  For example, you
00280 // could extract all words from a string by repeatedly calling
00281 //     pcrecpp::RE("(\\w+)").FindAndConsume(&input, &word)
00282 //
00283 // -----------------------------------------------------------------------
00284 // PARSING HEX/OCTAL/C-RADIX NUMBERS
00285 //
00286 // By default, if you pass a pointer to a numeric value, the
00287 // corresponding text is interpreted as a base-10 number.  You can
00288 // instead wrap the pointer with a call to one of the operators Hex(),
00289 // Octal(), or CRadix() to interpret the text in another base.  The
00290 // CRadix operator interprets C-style "0" (base-8) and "0x" (base-16)
00291 // prefixes, but defaults to base-10.
00292 //
00293 // Example:
00294 //   int a, b, c, d;
00295 //   pcrecpp::RE re("(.*) (.*) (.*) (.*)");
00296 //   re.FullMatch("100 40 0100 0x40",
00297 //                pcrecpp::Octal(&a), pcrecpp::Hex(&b),
00298 //                pcrecpp::CRadix(&c), pcrecpp::CRadix(&d));
00299 // will leave 64 in a, b, c, and d.
00300 //
00301 // -----------------------------------------------------------------------
00302 // REPLACING PARTS OF STRINGS
00303 //
00304 // You can replace the first match of "pattern" in "str" with
00305 // "rewrite".  Within "rewrite", backslash-escaped digits (\1 to \9)
00306 // can be used to insert text matching corresponding parenthesized
00307 // group from the pattern.  \0 in "rewrite" refers to the entire
00308 // matching text.  E.g.,
00309 //
00310 //   string s = "yabba dabba doo";
00311 //   pcrecpp::RE("b+").Replace("d", &s);
00312 //
00313 // will leave "s" containing "yada dabba doo".  The result is true if
00314 // the pattern matches and a replacement occurs, or false otherwise.
00315 //
00316 // GlobalReplace() is like Replace(), except that it replaces all
00317 // occurrences of the pattern in the string with the rewrite.
00318 // Replacements are not subject to re-matching.  E.g.,
00319 //
00320 //   string s = "yabba dabba doo";
00321 //   pcrecpp::RE("b+").GlobalReplace("d", &s);
00322 //
00323 // will leave "s" containing "yada dada doo".  It returns the number
00324 // of replacements made.
00325 //
00326 // Extract() is like Replace(), except that if the pattern matches,
00327 // "rewrite" is copied into "out" (an additional argument) with
00328 // substitutions.  The non-matching portions of "text" are ignored.
00329 // Returns true iff a match occurred and the extraction happened
00330 // successfully.  If no match occurs, the string is left unaffected.
00331 
00332 
00333 #include <string>
00334 #include <pcre.h>
00335 #include <pcrecpparg.h>   // defines the Arg class
00336 // This isn't technically needed here, but we include it
00337 // anyway so folks who include pcrecpp.h don't have to.
00338 #include <pcre_stringpiece.h>
00339 
00340 namespace pcrecpp {
00341 
00342 #define PCRE_SET_OR_CLEAR(b, o) \
00343     if (b) all_options_ |= (o); else all_options_ &= ~(o); \
00344     return *this
00345 
00346 #define PCRE_IS_SET(o)  \
00347         (all_options_ & o) == o
00348 
00349 /***** Compiling regular expressions: the RE class *****/
00350 
00351 // RE_Options allow you to set options to be passed along to pcre,
00352 // along with other options we put on top of pcre.
00353 // Only 9 modifiers, plus match_limit and match_limit_recursion,
00354 // are supported now.
00355 class PCRECPP_EXP_DEFN RE_Options {
00356  public:
00357   // constructor
00358   RE_Options() : match_limit_(0), match_limit_recursion_(0), all_options_(0) {}
00359 
00360   // alternative constructor.
00361   // To facilitate transfer of legacy code from C programs
00362   //
00363   // This lets you do
00364   //    RE(pattern, RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str);
00365   // But new code is better off doing
00366   //    RE(pattern,
00367   //      RE_Options().set_caseless(true).set_multiline(true)).PartialMatch(str);
00368   RE_Options(int option_flags) : match_limit_(0), match_limit_recursion_(0),
00369                                  all_options_(option_flags) {}
00370   // we're fine with the default destructor, copy constructor, etc.
00371 
00372   // accessors and mutators
00373   int match_limit() const { return match_limit_; };
00374   RE_Options &set_match_limit(int limit) {
00375     match_limit_ = limit;
00376     return *this;
00377   }
00378 
00379   int match_limit_recursion() const { return match_limit_recursion_; };
00380   RE_Options &set_match_limit_recursion(int limit) {
00381     match_limit_recursion_ = limit;
00382     return *this;
00383   }
00384 
00385   bool caseless() const {
00386     return PCRE_IS_SET(PCRE_CASELESS);
00387   }
00388   RE_Options &set_caseless(bool x) {
00389     PCRE_SET_OR_CLEAR(x, PCRE_CASELESS);
00390   }
00391 
00392   bool multiline() const {
00393     return PCRE_IS_SET(PCRE_MULTILINE);
00394   }
00395   RE_Options &set_multiline(bool x) {
00396     PCRE_SET_OR_CLEAR(x, PCRE_MULTILINE);
00397   }
00398 
00399   bool dotall() const {
00400     return PCRE_IS_SET(PCRE_DOTALL);
00401   }
00402   RE_Options &set_dotall(bool x) {
00403     PCRE_SET_OR_CLEAR(x, PCRE_DOTALL);
00404   }
00405 
00406   bool extended() const {
00407     return PCRE_IS_SET(PCRE_EXTENDED);
00408   }
00409   RE_Options &set_extended(bool x) {
00410     PCRE_SET_OR_CLEAR(x, PCRE_EXTENDED);
00411   }
00412 
00413   bool dollar_endonly() const {
00414     return PCRE_IS_SET(PCRE_DOLLAR_ENDONLY);
00415   }
00416   RE_Options &set_dollar_endonly(bool x) {
00417     PCRE_SET_OR_CLEAR(x, PCRE_DOLLAR_ENDONLY);
00418   }
00419 
00420   bool extra() const {
00421     return PCRE_IS_SET(PCRE_EXTRA);
00422   }
00423   RE_Options &set_extra(bool x) {
00424     PCRE_SET_OR_CLEAR(x, PCRE_EXTRA);
00425   }
00426 
00427   bool ungreedy() const {
00428     return PCRE_IS_SET(PCRE_UNGREEDY);
00429   }
00430   RE_Options &set_ungreedy(bool x) {
00431     PCRE_SET_OR_CLEAR(x, PCRE_UNGREEDY);
00432   }
00433 
00434   bool utf8() const {
00435     return PCRE_IS_SET(PCRE_UTF8);
00436   }
00437   RE_Options &set_utf8(bool x) {
00438     PCRE_SET_OR_CLEAR(x, PCRE_UTF8);
00439   }
00440 
00441   bool no_auto_capture() const {
00442     return PCRE_IS_SET(PCRE_NO_AUTO_CAPTURE);
00443   }
00444   RE_Options &set_no_auto_capture(bool x) {
00445     PCRE_SET_OR_CLEAR(x, PCRE_NO_AUTO_CAPTURE);
00446   }
00447 
00448   RE_Options &set_all_options(int opt) {
00449     all_options_ = opt;
00450     return *this;
00451   }
00452   int all_options() const {
00453     return all_options_ ;
00454   }
00455 
00456   // TODO: add other pcre flags
00457 
00458  private:
00459   int match_limit_;
00460   int match_limit_recursion_;
00461   int all_options_;
00462 };
00463 
00464 // These functions return some common RE_Options
00465 static inline RE_Options UTF8() {
00466   return RE_Options().set_utf8(true);
00467 }
00468 
00469 static inline RE_Options CASELESS() {
00470   return RE_Options().set_caseless(true);
00471 }
00472 static inline RE_Options MULTILINE() {
00473   return RE_Options().set_multiline(true);
00474 }
00475 
00476 static inline RE_Options DOTALL() {
00477   return RE_Options().set_dotall(true);
00478 }
00479 
00480 static inline RE_Options EXTENDED() {
00481   return RE_Options().set_extended(true);
00482 }
00483 
00484 // Interface for regular expression matching.  Also corresponds to a
00485 // pre-compiled regular expression.  An "RE" object is safe for
00486 // concurrent use by multiple threads.
00487 class PCRECPP_EXP_DEFN RE {
00488  public:
00489   // We provide implicit conversions from strings so that users can
00490   // pass in a string or a "const char*" wherever an "RE" is expected.
00491   RE(const string& pat) { Init(pat, NULL); }
00492   RE(const string& pat, const RE_Options& option) { Init(pat, &option); }
00493   RE(const char* pat) { Init(pat, NULL); }
00494   RE(const char* pat, const RE_Options& option) { Init(pat, &option); }
00495   RE(const unsigned char* pat) {
00496     Init(reinterpret_cast<const char*>(pat), NULL);
00497   }
00498   RE(const unsigned char* pat, const RE_Options& option) {
00499     Init(reinterpret_cast<const char*>(pat), &option);
00500   }
00501 
00502   // Copy constructor & assignment - note that these are expensive
00503   // because they recompile the expression.
00504   RE(const RE& re) { Init(re.pattern_, &re.options_); }
00505   const RE& operator=(const RE& re) {
00506     if (this != &re) {
00507       Cleanup();
00508 
00509       // This is the code that originally came from Google
00510       // Init(re.pattern_.c_str(), &re.options_);
00511 
00512       // This is the replacement from Ari Pollak
00513       Init(re.pattern_, &re.options_);
00514     }
00515     return *this;
00516   }
00517 
00518 
00519   ~RE();
00520 
00521   // The string specification for this RE.  E.g.
00522   //   RE re("ab*c?d+");
00523   //   re.pattern();    // "ab*c?d+"
00524   const string& pattern() const { return pattern_; }
00525 
00526   // If RE could not be created properly, returns an error string.
00527   // Else returns the empty string.
00528   const string& error() const { return *error_; }
00529 
00530   /***** The useful part: the matching interface *****/
00531 
00532   // This is provided so one can do pattern.ReplaceAll() just as
00533   // easily as ReplaceAll(pattern-text, ....)
00534 
00535   bool FullMatch(const StringPiece& text,
00536                  const Arg& ptr1 = no_arg,
00537                  const Arg& ptr2 = no_arg,
00538                  const Arg& ptr3 = no_arg,
00539                  const Arg& ptr4 = no_arg,
00540                  const Arg& ptr5 = no_arg,
00541                  const Arg& ptr6 = no_arg,
00542                  const Arg& ptr7 = no_arg,
00543                  const Arg& ptr8 = no_arg,
00544                  const Arg& ptr9 = no_arg,
00545                  const Arg& ptr10 = no_arg,
00546                  const Arg& ptr11 = no_arg,
00547                  const Arg& ptr12 = no_arg,
00548                  const Arg& ptr13 = no_arg,
00549                  const Arg& ptr14 = no_arg,
00550                  const Arg& ptr15 = no_arg,
00551                  const Arg& ptr16 = no_arg) const;
00552 
00553   bool PartialMatch(const StringPiece& text,
00554                     const Arg& ptr1 = no_arg,
00555                     const Arg& ptr2 = no_arg,
00556                     const Arg& ptr3 = no_arg,
00557                     const Arg& ptr4 = no_arg,
00558                     const Arg& ptr5 = no_arg,
00559                     const Arg& ptr6 = no_arg,
00560                     const Arg& ptr7 = no_arg,
00561                     const Arg& ptr8 = no_arg,
00562                     const Arg& ptr9 = no_arg,
00563                     const Arg& ptr10 = no_arg,
00564                     const Arg& ptr11 = no_arg,
00565                     const Arg& ptr12 = no_arg,
00566                     const Arg& ptr13 = no_arg,
00567                     const Arg& ptr14 = no_arg,
00568                     const Arg& ptr15 = no_arg,
00569                     const Arg& ptr16 = no_arg) const;
00570 
00571   bool Consume(StringPiece* input,
00572                const Arg& ptr1 = no_arg,
00573                const Arg& ptr2 = no_arg,
00574                const Arg& ptr3 = no_arg,
00575                const Arg& ptr4 = no_arg,
00576                const Arg& ptr5 = no_arg,
00577                const Arg& ptr6 = no_arg,
00578                const Arg& ptr7 = no_arg,
00579                const Arg& ptr8 = no_arg,
00580                const Arg& ptr9 = no_arg,
00581                const Arg& ptr10 = no_arg,
00582                const Arg& ptr11 = no_arg,
00583                const Arg& ptr12 = no_arg,
00584                const Arg& ptr13 = no_arg,
00585                const Arg& ptr14 = no_arg,
00586                const Arg& ptr15 = no_arg,
00587                const Arg& ptr16 = no_arg) const;
00588 
00589   bool FindAndConsume(StringPiece* input,
00590                       const Arg& ptr1 = no_arg,
00591                       const Arg& ptr2 = no_arg,
00592                       const Arg& ptr3 = no_arg,
00593                       const Arg& ptr4 = no_arg,
00594                       const Arg& ptr5 = no_arg,
00595                       const Arg& ptr6 = no_arg,
00596                       const Arg& ptr7 = no_arg,
00597                       const Arg& ptr8 = no_arg,
00598                       const Arg& ptr9 = no_arg,
00599                       const Arg& ptr10 = no_arg,
00600                       const Arg& ptr11 = no_arg,
00601                       const Arg& ptr12 = no_arg,
00602                       const Arg& ptr13 = no_arg,
00603                       const Arg& ptr14 = no_arg,
00604                       const Arg& ptr15 = no_arg,
00605                       const Arg& ptr16 = no_arg) const;
00606 
00607   bool Replace(const StringPiece& rewrite,
00608                string *str) const;
00609 
00610   int GlobalReplace(const StringPiece& rewrite,
00611                     string *str) const;
00612 
00613   bool Extract(const StringPiece &rewrite,
00614                const StringPiece &text,
00615                string *out) const;
00616 
00617   // Escapes all potentially meaningful regexp characters in
00618   // 'unquoted'.  The returned string, used as a regular expression,
00619   // will exactly match the original string.  For example,
00620   //           1.5-2.0?
00621   // may become:
00622   //           1\.5\-2\.0\?
00623   // Note QuoteMeta behaves the same as perl's QuoteMeta function,
00624   // *except* that it escapes the NUL character (\0) as backslash + 0,
00625   // rather than backslash + NUL.
00626   static string QuoteMeta(const StringPiece& unquoted);
00627 
00628 
00629   /***** Generic matching interface *****/
00630 
00631   // Type of match (TODO: Should be restructured as part of RE_Options)
00632   enum Anchor {
00633     UNANCHORED,         // No anchoring
00634     ANCHOR_START,       // Anchor at start only
00635     ANCHOR_BOTH         // Anchor at start and end
00636   };
00637 
00638   // General matching routine.  Stores the length of the match in
00639   // "*consumed" if successful.
00640   bool DoMatch(const StringPiece& text,
00641                Anchor anchor,
00642                int* consumed,
00643                const Arg* const* args, int n) const;
00644 
00645   // Return the number of capturing subpatterns, or -1 if the
00646   // regexp wasn't valid on construction.
00647   int NumberOfCapturingGroups() const;
00648 
00649   // The default value for an argument, to indicate no arg was passed in
00650   static Arg no_arg;
00651 
00652  private:
00653 
00654   void Init(const string& pattern, const RE_Options* options);
00655   void Cleanup();
00656 
00657   // Match against "text", filling in "vec" (up to "vecsize" * 2/3) with
00658   // pairs of integers for the beginning and end positions of matched
00659   // text.  The first pair corresponds to the entire matched text;
00660   // subsequent pairs correspond, in order, to parentheses-captured
00661   // matches.  Returns the number of pairs (one more than the number of
00662   // the last subpattern with a match) if matching was successful
00663   // and zero if the match failed.
00664   // I.e. for RE("(foo)|(bar)|(baz)") it will return 2, 3, and 4 when matching
00665   // against "foo", "bar", and "baz" respectively.
00666   // When matching RE("(foo)|hello") against "hello", it will return 1.
00667   // But the values for all subpattern are filled in into "vec".
00668   int TryMatch(const StringPiece& text,
00669                int startpos,
00670                Anchor anchor,
00671                int *vec,
00672                int vecsize) const;
00673 
00674   // Append the "rewrite" string, with backslash subsitutions from "text"
00675   // and "vec", to string "out".
00676   bool Rewrite(string *out,
00677                const StringPiece& rewrite,
00678                const StringPiece& text,
00679                int *vec,
00680                int veclen) const;
00681 
00682   // internal implementation for DoMatch
00683   bool DoMatchImpl(const StringPiece& text,
00684                    Anchor anchor,
00685                    int* consumed,
00686                    const Arg* const args[],
00687                    int n,
00688                    int* vec,
00689                    int vecsize) const;
00690 
00691   // Compile the regexp for the specified anchoring mode
00692   pcre* Compile(Anchor anchor);
00693 
00694   string        pattern_;
00695   RE_Options    options_;
00696   pcre*         re_full_;       // For full matches
00697   pcre*         re_partial_;    // For partial matches
00698   const string* error_;         // Error indicator (or points to empty string)
00699 };
00700 
00701 }   // namespace pcrecpp
00702 
00703 #endif /* _PCRECPP_H */

Generated on Tue Jul 5 14:11:58 2011 for ROOT_528-00b_version by  doxygen 1.5.1