jpayne@69: // Copyright (c) 2005, Google Inc. jpayne@69: // All rights reserved. jpayne@69: // jpayne@69: // Redistribution and use in source and binary forms, with or without jpayne@69: // modification, are permitted provided that the following conditions are jpayne@69: // met: jpayne@69: // jpayne@69: // * Redistributions of source code must retain the above copyright jpayne@69: // notice, this list of conditions and the following disclaimer. jpayne@69: // * Redistributions in binary form must reproduce the above jpayne@69: // copyright notice, this list of conditions and the following disclaimer jpayne@69: // in the documentation and/or other materials provided with the jpayne@69: // distribution. jpayne@69: // * Neither the name of Google Inc. nor the names of its jpayne@69: // contributors may be used to endorse or promote products derived from jpayne@69: // this software without specific prior written permission. jpayne@69: // jpayne@69: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS jpayne@69: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT jpayne@69: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR jpayne@69: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT jpayne@69: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, jpayne@69: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT jpayne@69: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, jpayne@69: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY jpayne@69: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT jpayne@69: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE jpayne@69: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jpayne@69: // jpayne@69: // Author: Sanjay Ghemawat jpayne@69: // Support for PCRE_XXX modifiers added by Giuseppe Maxia, July 2005 jpayne@69: jpayne@69: #ifndef _PCRECPP_H jpayne@69: #define _PCRECPP_H jpayne@69: jpayne@69: // C++ interface to the pcre regular-expression library. RE supports jpayne@69: // Perl-style regular expressions (with extensions like \d, \w, \s, jpayne@69: // ...). jpayne@69: // jpayne@69: // ----------------------------------------------------------------------- jpayne@69: // REGEXP SYNTAX: jpayne@69: // jpayne@69: // This module is part of the pcre library and hence supports its syntax jpayne@69: // for regular expressions. jpayne@69: // jpayne@69: // The syntax is pretty similar to Perl's. For those not familiar jpayne@69: // with Perl's regular expressions, here are some examples of the most jpayne@69: // commonly used extensions: jpayne@69: // jpayne@69: // "hello (\\w+) world" -- \w matches a "word" character jpayne@69: // "version (\\d+)" -- \d matches a digit jpayne@69: // "hello\\s+world" -- \s matches any whitespace character jpayne@69: // "\\b(\\w+)\\b" -- \b matches empty string at a word boundary jpayne@69: // "(?i)hello" -- (?i) turns on case-insensitive matching jpayne@69: // "/\\*(.*?)\\*/" -- .*? matches . minimum no. of times possible jpayne@69: // jpayne@69: // ----------------------------------------------------------------------- jpayne@69: // MATCHING INTERFACE: jpayne@69: // jpayne@69: // The "FullMatch" operation checks that supplied text matches a jpayne@69: // supplied pattern exactly. jpayne@69: // jpayne@69: // Example: successful match jpayne@69: // pcrecpp::RE re("h.*o"); jpayne@69: // re.FullMatch("hello"); jpayne@69: // jpayne@69: // Example: unsuccessful match (requires full match): jpayne@69: // pcrecpp::RE re("e"); jpayne@69: // !re.FullMatch("hello"); jpayne@69: // jpayne@69: // Example: creating a temporary RE object: jpayne@69: // pcrecpp::RE("h.*o").FullMatch("hello"); jpayne@69: // jpayne@69: // You can pass in a "const char*" or a "string" for "text". The jpayne@69: // examples below tend to use a const char*. jpayne@69: // jpayne@69: // You can, as in the different examples above, store the RE object jpayne@69: // explicitly in a variable or use a temporary RE object. The jpayne@69: // examples below use one mode or the other arbitrarily. Either jpayne@69: // could correctly be used for any of these examples. jpayne@69: // jpayne@69: // ----------------------------------------------------------------------- jpayne@69: // MATCHING WITH SUB-STRING EXTRACTION: jpayne@69: // jpayne@69: // You can supply extra pointer arguments to extract matched subpieces. jpayne@69: // jpayne@69: // Example: extracts "ruby" into "s" and 1234 into "i" jpayne@69: // int i; jpayne@69: // string s; jpayne@69: // pcrecpp::RE re("(\\w+):(\\d+)"); jpayne@69: // re.FullMatch("ruby:1234", &s, &i); jpayne@69: // jpayne@69: // Example: does not try to extract any extra sub-patterns jpayne@69: // re.FullMatch("ruby:1234", &s); jpayne@69: // jpayne@69: // Example: does not try to extract into NULL jpayne@69: // re.FullMatch("ruby:1234", NULL, &i); jpayne@69: // jpayne@69: // Example: integer overflow causes failure jpayne@69: // !re.FullMatch("ruby:1234567891234", NULL, &i); jpayne@69: // jpayne@69: // Example: fails because there aren't enough sub-patterns: jpayne@69: // !pcrecpp::RE("\\w+:\\d+").FullMatch("ruby:1234", &s); jpayne@69: // jpayne@69: // Example: fails because string cannot be stored in integer jpayne@69: // !pcrecpp::RE("(.*)").FullMatch("ruby", &i); jpayne@69: // jpayne@69: // The provided pointer arguments can be pointers to any scalar numeric jpayne@69: // type, or one of jpayne@69: // string (matched piece is copied to string) jpayne@69: // StringPiece (StringPiece is mutated to point to matched piece) jpayne@69: // T (where "bool T::ParseFrom(const char*, int)" exists) jpayne@69: // NULL (the corresponding matched sub-pattern is not copied) jpayne@69: // jpayne@69: // CAVEAT: An optional sub-pattern that does not exist in the matched jpayne@69: // string is assigned the empty string. Therefore, the following will jpayne@69: // return false (because the empty string is not a valid number): jpayne@69: // int number; jpayne@69: // pcrecpp::RE::FullMatch("abc", "[a-z]+(\\d+)?", &number); jpayne@69: // jpayne@69: // ----------------------------------------------------------------------- jpayne@69: // DO_MATCH jpayne@69: // jpayne@69: // The matching interface supports at most 16 arguments per call. jpayne@69: // If you need more, consider using the more general interface jpayne@69: // pcrecpp::RE::DoMatch(). See pcrecpp.h for the signature for DoMatch. jpayne@69: // jpayne@69: // ----------------------------------------------------------------------- jpayne@69: // PARTIAL MATCHES jpayne@69: // jpayne@69: // You can use the "PartialMatch" operation when you want the pattern jpayne@69: // to match any substring of the text. jpayne@69: // jpayne@69: // Example: simple search for a string: jpayne@69: // pcrecpp::RE("ell").PartialMatch("hello"); jpayne@69: // jpayne@69: // Example: find first number in a string: jpayne@69: // int number; jpayne@69: // pcrecpp::RE re("(\\d+)"); jpayne@69: // re.PartialMatch("x*100 + 20", &number); jpayne@69: // assert(number == 100); jpayne@69: // jpayne@69: // ----------------------------------------------------------------------- jpayne@69: // UTF-8 AND THE MATCHING INTERFACE: jpayne@69: // jpayne@69: // By default, pattern and text are plain text, one byte per character. jpayne@69: // The UTF8 flag, passed to the constructor, causes both pattern jpayne@69: // and string to be treated as UTF-8 text, still a byte stream but jpayne@69: // potentially multiple bytes per character. In practice, the text jpayne@69: // is likelier to be UTF-8 than the pattern, but the match returned jpayne@69: // may depend on the UTF8 flag, so always use it when matching jpayne@69: // UTF8 text. E.g., "." will match one byte normally but with UTF8 jpayne@69: // set may match up to three bytes of a multi-byte character. jpayne@69: // jpayne@69: // Example: jpayne@69: // pcrecpp::RE_Options options; jpayne@69: // options.set_utf8(); jpayne@69: // pcrecpp::RE re(utf8_pattern, options); jpayne@69: // re.FullMatch(utf8_string); jpayne@69: // jpayne@69: // Example: using the convenience function UTF8(): jpayne@69: // pcrecpp::RE re(utf8_pattern, pcrecpp::UTF8()); jpayne@69: // re.FullMatch(utf8_string); jpayne@69: // jpayne@69: // NOTE: The UTF8 option is ignored if pcre was not configured with the jpayne@69: // --enable-utf8 flag. jpayne@69: // jpayne@69: // ----------------------------------------------------------------------- jpayne@69: // PASSING MODIFIERS TO THE REGULAR EXPRESSION ENGINE jpayne@69: // jpayne@69: // PCRE defines some modifiers to change the behavior of the regular jpayne@69: // expression engine. jpayne@69: // The C++ wrapper defines an auxiliary class, RE_Options, as a vehicle jpayne@69: // to pass such modifiers to a RE class. jpayne@69: // jpayne@69: // Currently, the following modifiers are supported jpayne@69: // jpayne@69: // modifier description Perl corresponding jpayne@69: // jpayne@69: // PCRE_CASELESS case insensitive match /i jpayne@69: // PCRE_MULTILINE multiple lines match /m jpayne@69: // PCRE_DOTALL dot matches newlines /s jpayne@69: // PCRE_DOLLAR_ENDONLY $ matches only at end N/A jpayne@69: // PCRE_EXTRA strict escape parsing N/A jpayne@69: // PCRE_EXTENDED ignore whitespaces /x jpayne@69: // PCRE_UTF8 handles UTF8 chars built-in jpayne@69: // PCRE_UNGREEDY reverses * and *? N/A jpayne@69: // PCRE_NO_AUTO_CAPTURE disables matching parens N/A (*) jpayne@69: // jpayne@69: // (For a full account on how each modifier works, please check the jpayne@69: // PCRE API reference manual). jpayne@69: // jpayne@69: // (*) Both Perl and PCRE allow non matching parentheses by means of the jpayne@69: // "?:" modifier within the pattern itself. e.g. (?:ab|cd) does not jpayne@69: // capture, while (ab|cd) does. jpayne@69: // jpayne@69: // For each modifier, there are two member functions whose name is made jpayne@69: // out of the modifier in lowercase, without the "PCRE_" prefix. For jpayne@69: // instance, PCRE_CASELESS is handled by jpayne@69: // bool caseless(), jpayne@69: // which returns true if the modifier is set, and jpayne@69: // RE_Options & set_caseless(bool), jpayne@69: // which sets or unsets the modifier. jpayne@69: // jpayne@69: // Moreover, PCRE_EXTRA_MATCH_LIMIT can be accessed through the jpayne@69: // set_match_limit() and match_limit() member functions. jpayne@69: // Setting match_limit to a non-zero value will limit the executation of jpayne@69: // pcre to keep it from doing bad things like blowing the stack or taking jpayne@69: // an eternity to return a result. A value of 5000 is good enough to stop jpayne@69: // stack blowup in a 2MB thread stack. Setting match_limit to zero will jpayne@69: // disable match limiting. Alternately, you can set match_limit_recursion() jpayne@69: // which uses PCRE_EXTRA_MATCH_LIMIT_RECURSION to limit how much pcre jpayne@69: // recurses. match_limit() caps the number of matches pcre does; jpayne@69: // match_limit_recrusion() caps the depth of recursion. jpayne@69: // jpayne@69: // Normally, to pass one or more modifiers to a RE class, you declare jpayne@69: // a RE_Options object, set the appropriate options, and pass this jpayne@69: // object to a RE constructor. Example: jpayne@69: // jpayne@69: // RE_options opt; jpayne@69: // opt.set_caseless(true); jpayne@69: // jpayne@69: // if (RE("HELLO", opt).PartialMatch("hello world")) ... jpayne@69: // jpayne@69: // RE_options has two constructors. The default constructor takes no jpayne@69: // arguments and creates a set of flags that are off by default. jpayne@69: // jpayne@69: // The optional parameter 'option_flags' is to facilitate transfer jpayne@69: // of legacy code from C programs. This lets you do jpayne@69: // RE(pattern, RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str); jpayne@69: // jpayne@69: // But new code is better off doing jpayne@69: // RE(pattern, jpayne@69: // RE_Options().set_caseless(true).set_multiline(true)).PartialMatch(str); jpayne@69: // (See below) jpayne@69: // jpayne@69: // If you are going to pass one of the most used modifiers, there are some jpayne@69: // convenience functions that return a RE_Options class with the jpayne@69: // appropriate modifier already set: jpayne@69: // CASELESS(), UTF8(), MULTILINE(), DOTALL(), EXTENDED() jpayne@69: // jpayne@69: // If you need to set several options at once, and you don't want to go jpayne@69: // through the pains of declaring a RE_Options object and setting several jpayne@69: // options, there is a parallel method that give you such ability on the jpayne@69: // fly. You can concatenate several set_xxxxx member functions, since each jpayne@69: // of them returns a reference to its class object. e.g.: to pass jpayne@69: // PCRE_CASELESS, PCRE_EXTENDED, and PCRE_MULTILINE to a RE with one jpayne@69: // statement, you may write jpayne@69: // jpayne@69: // RE(" ^ xyz \\s+ .* blah$", RE_Options() jpayne@69: // .set_caseless(true) jpayne@69: // .set_extended(true) jpayne@69: // .set_multiline(true)).PartialMatch(sometext); jpayne@69: // jpayne@69: // ----------------------------------------------------------------------- jpayne@69: // SCANNING TEXT INCREMENTALLY jpayne@69: // jpayne@69: // The "Consume" operation may be useful if you want to repeatedly jpayne@69: // match regular expressions at the front of a string and skip over jpayne@69: // them as they match. This requires use of the "StringPiece" type, jpayne@69: // which represents a sub-range of a real string. Like RE, StringPiece jpayne@69: // is defined in the pcrecpp namespace. jpayne@69: // jpayne@69: // Example: read lines of the form "var = value" from a string. jpayne@69: // string contents = ...; // Fill string somehow jpayne@69: // pcrecpp::StringPiece input(contents); // Wrap in a StringPiece jpayne@69: // jpayne@69: // string var; jpayne@69: // int value; jpayne@69: // pcrecpp::RE re("(\\w+) = (\\d+)\n"); jpayne@69: // while (re.Consume(&input, &var, &value)) { jpayne@69: // ...; jpayne@69: // } jpayne@69: // jpayne@69: // Each successful call to "Consume" will set "var/value", and also jpayne@69: // advance "input" so it points past the matched text. jpayne@69: // jpayne@69: // The "FindAndConsume" operation is similar to "Consume" but does not jpayne@69: // anchor your match at the beginning of the string. For example, you jpayne@69: // could extract all words from a string by repeatedly calling jpayne@69: // pcrecpp::RE("(\\w+)").FindAndConsume(&input, &word) jpayne@69: // jpayne@69: // ----------------------------------------------------------------------- jpayne@69: // PARSING HEX/OCTAL/C-RADIX NUMBERS jpayne@69: // jpayne@69: // By default, if you pass a pointer to a numeric value, the jpayne@69: // corresponding text is interpreted as a base-10 number. You can jpayne@69: // instead wrap the pointer with a call to one of the operators Hex(), jpayne@69: // Octal(), or CRadix() to interpret the text in another base. The jpayne@69: // CRadix operator interprets C-style "0" (base-8) and "0x" (base-16) jpayne@69: // prefixes, but defaults to base-10. jpayne@69: // jpayne@69: // Example: jpayne@69: // int a, b, c, d; jpayne@69: // pcrecpp::RE re("(.*) (.*) (.*) (.*)"); jpayne@69: // re.FullMatch("100 40 0100 0x40", jpayne@69: // pcrecpp::Octal(&a), pcrecpp::Hex(&b), jpayne@69: // pcrecpp::CRadix(&c), pcrecpp::CRadix(&d)); jpayne@69: // will leave 64 in a, b, c, and d. jpayne@69: // jpayne@69: // ----------------------------------------------------------------------- jpayne@69: // REPLACING PARTS OF STRINGS jpayne@69: // jpayne@69: // You can replace the first match of "pattern" in "str" with jpayne@69: // "rewrite". Within "rewrite", backslash-escaped digits (\1 to \9) jpayne@69: // can be used to insert text matching corresponding parenthesized jpayne@69: // group from the pattern. \0 in "rewrite" refers to the entire jpayne@69: // matching text. E.g., jpayne@69: // jpayne@69: // string s = "yabba dabba doo"; jpayne@69: // pcrecpp::RE("b+").Replace("d", &s); jpayne@69: // jpayne@69: // will leave "s" containing "yada dabba doo". The result is true if jpayne@69: // the pattern matches and a replacement occurs, or false otherwise. jpayne@69: // jpayne@69: // GlobalReplace() is like Replace(), except that it replaces all jpayne@69: // occurrences of the pattern in the string with the rewrite. jpayne@69: // Replacements are not subject to re-matching. E.g., jpayne@69: // jpayne@69: // string s = "yabba dabba doo"; jpayne@69: // pcrecpp::RE("b+").GlobalReplace("d", &s); jpayne@69: // jpayne@69: // will leave "s" containing "yada dada doo". It returns the number jpayne@69: // of replacements made. jpayne@69: // jpayne@69: // Extract() is like Replace(), except that if the pattern matches, jpayne@69: // "rewrite" is copied into "out" (an additional argument) with jpayne@69: // substitutions. The non-matching portions of "text" are ignored. jpayne@69: // Returns true iff a match occurred and the extraction happened jpayne@69: // successfully. If no match occurs, the string is left unaffected. jpayne@69: jpayne@69: jpayne@69: #include jpayne@69: #include jpayne@69: #include // defines the Arg class jpayne@69: // This isn't technically needed here, but we include it jpayne@69: // anyway so folks who include pcrecpp.h don't have to. jpayne@69: #include jpayne@69: jpayne@69: namespace pcrecpp { jpayne@69: jpayne@69: #define PCRE_SET_OR_CLEAR(b, o) \ jpayne@69: if (b) all_options_ |= (o); else all_options_ &= ~(o); \ jpayne@69: return *this jpayne@69: jpayne@69: #define PCRE_IS_SET(o) \ jpayne@69: (all_options_ & o) == o jpayne@69: jpayne@69: /***** Compiling regular expressions: the RE class *****/ jpayne@69: jpayne@69: // RE_Options allow you to set options to be passed along to pcre, jpayne@69: // along with other options we put on top of pcre. jpayne@69: // Only 9 modifiers, plus match_limit and match_limit_recursion, jpayne@69: // are supported now. jpayne@69: class PCRECPP_EXP_DEFN RE_Options { jpayne@69: public: jpayne@69: // constructor jpayne@69: RE_Options() : match_limit_(0), match_limit_recursion_(0), all_options_(0) {} jpayne@69: jpayne@69: // alternative constructor. jpayne@69: // To facilitate transfer of legacy code from C programs jpayne@69: // jpayne@69: // This lets you do jpayne@69: // RE(pattern, RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str); jpayne@69: // But new code is better off doing jpayne@69: // RE(pattern, jpayne@69: // RE_Options().set_caseless(true).set_multiline(true)).PartialMatch(str); jpayne@69: RE_Options(int option_flags) : match_limit_(0), match_limit_recursion_(0), jpayne@69: all_options_(option_flags) {} jpayne@69: // we're fine with the default destructor, copy constructor, etc. jpayne@69: jpayne@69: // accessors and mutators jpayne@69: int match_limit() const { return match_limit_; }; jpayne@69: RE_Options &set_match_limit(int limit) { jpayne@69: match_limit_ = limit; jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: int match_limit_recursion() const { return match_limit_recursion_; }; jpayne@69: RE_Options &set_match_limit_recursion(int limit) { jpayne@69: match_limit_recursion_ = limit; jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: bool caseless() const { jpayne@69: return PCRE_IS_SET(PCRE_CASELESS); jpayne@69: } jpayne@69: RE_Options &set_caseless(bool x) { jpayne@69: PCRE_SET_OR_CLEAR(x, PCRE_CASELESS); jpayne@69: } jpayne@69: jpayne@69: bool multiline() const { jpayne@69: return PCRE_IS_SET(PCRE_MULTILINE); jpayne@69: } jpayne@69: RE_Options &set_multiline(bool x) { jpayne@69: PCRE_SET_OR_CLEAR(x, PCRE_MULTILINE); jpayne@69: } jpayne@69: jpayne@69: bool dotall() const { jpayne@69: return PCRE_IS_SET(PCRE_DOTALL); jpayne@69: } jpayne@69: RE_Options &set_dotall(bool x) { jpayne@69: PCRE_SET_OR_CLEAR(x, PCRE_DOTALL); jpayne@69: } jpayne@69: jpayne@69: bool extended() const { jpayne@69: return PCRE_IS_SET(PCRE_EXTENDED); jpayne@69: } jpayne@69: RE_Options &set_extended(bool x) { jpayne@69: PCRE_SET_OR_CLEAR(x, PCRE_EXTENDED); jpayne@69: } jpayne@69: jpayne@69: bool dollar_endonly() const { jpayne@69: return PCRE_IS_SET(PCRE_DOLLAR_ENDONLY); jpayne@69: } jpayne@69: RE_Options &set_dollar_endonly(bool x) { jpayne@69: PCRE_SET_OR_CLEAR(x, PCRE_DOLLAR_ENDONLY); jpayne@69: } jpayne@69: jpayne@69: bool extra() const { jpayne@69: return PCRE_IS_SET(PCRE_EXTRA); jpayne@69: } jpayne@69: RE_Options &set_extra(bool x) { jpayne@69: PCRE_SET_OR_CLEAR(x, PCRE_EXTRA); jpayne@69: } jpayne@69: jpayne@69: bool ungreedy() const { jpayne@69: return PCRE_IS_SET(PCRE_UNGREEDY); jpayne@69: } jpayne@69: RE_Options &set_ungreedy(bool x) { jpayne@69: PCRE_SET_OR_CLEAR(x, PCRE_UNGREEDY); jpayne@69: } jpayne@69: jpayne@69: bool utf8() const { jpayne@69: return PCRE_IS_SET(PCRE_UTF8); jpayne@69: } jpayne@69: RE_Options &set_utf8(bool x) { jpayne@69: PCRE_SET_OR_CLEAR(x, PCRE_UTF8); jpayne@69: } jpayne@69: jpayne@69: bool no_auto_capture() const { jpayne@69: return PCRE_IS_SET(PCRE_NO_AUTO_CAPTURE); jpayne@69: } jpayne@69: RE_Options &set_no_auto_capture(bool x) { jpayne@69: PCRE_SET_OR_CLEAR(x, PCRE_NO_AUTO_CAPTURE); jpayne@69: } jpayne@69: jpayne@69: RE_Options &set_all_options(int opt) { jpayne@69: all_options_ = opt; jpayne@69: return *this; jpayne@69: } jpayne@69: int all_options() const { jpayne@69: return all_options_ ; jpayne@69: } jpayne@69: jpayne@69: // TODO: add other pcre flags jpayne@69: jpayne@69: private: jpayne@69: int match_limit_; jpayne@69: int match_limit_recursion_; jpayne@69: int all_options_; jpayne@69: }; jpayne@69: jpayne@69: // These functions return some common RE_Options jpayne@69: static inline RE_Options UTF8() { jpayne@69: return RE_Options().set_utf8(true); jpayne@69: } jpayne@69: jpayne@69: static inline RE_Options CASELESS() { jpayne@69: return RE_Options().set_caseless(true); jpayne@69: } jpayne@69: static inline RE_Options MULTILINE() { jpayne@69: return RE_Options().set_multiline(true); jpayne@69: } jpayne@69: jpayne@69: static inline RE_Options DOTALL() { jpayne@69: return RE_Options().set_dotall(true); jpayne@69: } jpayne@69: jpayne@69: static inline RE_Options EXTENDED() { jpayne@69: return RE_Options().set_extended(true); jpayne@69: } jpayne@69: jpayne@69: // Interface for regular expression matching. Also corresponds to a jpayne@69: // pre-compiled regular expression. An "RE" object is safe for jpayne@69: // concurrent use by multiple threads. jpayne@69: class PCRECPP_EXP_DEFN RE { jpayne@69: public: jpayne@69: // We provide implicit conversions from strings so that users can jpayne@69: // pass in a string or a "const char*" wherever an "RE" is expected. jpayne@69: RE(const string& pat) { Init(pat, NULL); } jpayne@69: RE(const string& pat, const RE_Options& option) { Init(pat, &option); } jpayne@69: RE(const char* pat) { Init(pat, NULL); } jpayne@69: RE(const char* pat, const RE_Options& option) { Init(pat, &option); } jpayne@69: RE(const unsigned char* pat) { jpayne@69: Init(reinterpret_cast(pat), NULL); jpayne@69: } jpayne@69: RE(const unsigned char* pat, const RE_Options& option) { jpayne@69: Init(reinterpret_cast(pat), &option); jpayne@69: } jpayne@69: jpayne@69: // Copy constructor & assignment - note that these are expensive jpayne@69: // because they recompile the expression. jpayne@69: RE(const RE& re) { Init(re.pattern_, &re.options_); } jpayne@69: const RE& operator=(const RE& re) { jpayne@69: if (this != &re) { jpayne@69: Cleanup(); jpayne@69: jpayne@69: // This is the code that originally came from Google jpayne@69: // Init(re.pattern_.c_str(), &re.options_); jpayne@69: jpayne@69: // This is the replacement from Ari Pollak jpayne@69: Init(re.pattern_, &re.options_); jpayne@69: } jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: jpayne@69: ~RE(); jpayne@69: jpayne@69: // The string specification for this RE. E.g. jpayne@69: // RE re("ab*c?d+"); jpayne@69: // re.pattern(); // "ab*c?d+" jpayne@69: const string& pattern() const { return pattern_; } jpayne@69: jpayne@69: // If RE could not be created properly, returns an error string. jpayne@69: // Else returns the empty string. jpayne@69: const string& error() const { return *error_; } jpayne@69: jpayne@69: /***** The useful part: the matching interface *****/ jpayne@69: jpayne@69: // This is provided so one can do pattern.ReplaceAll() just as jpayne@69: // easily as ReplaceAll(pattern-text, ....) jpayne@69: jpayne@69: bool FullMatch(const StringPiece& text, jpayne@69: const Arg& ptr1 = no_arg, jpayne@69: const Arg& ptr2 = no_arg, jpayne@69: const Arg& ptr3 = no_arg, jpayne@69: const Arg& ptr4 = no_arg, jpayne@69: const Arg& ptr5 = no_arg, jpayne@69: const Arg& ptr6 = no_arg, jpayne@69: const Arg& ptr7 = no_arg, jpayne@69: const Arg& ptr8 = no_arg, jpayne@69: const Arg& ptr9 = no_arg, jpayne@69: const Arg& ptr10 = no_arg, jpayne@69: const Arg& ptr11 = no_arg, jpayne@69: const Arg& ptr12 = no_arg, jpayne@69: const Arg& ptr13 = no_arg, jpayne@69: const Arg& ptr14 = no_arg, jpayne@69: const Arg& ptr15 = no_arg, jpayne@69: const Arg& ptr16 = no_arg) const; jpayne@69: jpayne@69: bool PartialMatch(const StringPiece& text, jpayne@69: const Arg& ptr1 = no_arg, jpayne@69: const Arg& ptr2 = no_arg, jpayne@69: const Arg& ptr3 = no_arg, jpayne@69: const Arg& ptr4 = no_arg, jpayne@69: const Arg& ptr5 = no_arg, jpayne@69: const Arg& ptr6 = no_arg, jpayne@69: const Arg& ptr7 = no_arg, jpayne@69: const Arg& ptr8 = no_arg, jpayne@69: const Arg& ptr9 = no_arg, jpayne@69: const Arg& ptr10 = no_arg, jpayne@69: const Arg& ptr11 = no_arg, jpayne@69: const Arg& ptr12 = no_arg, jpayne@69: const Arg& ptr13 = no_arg, jpayne@69: const Arg& ptr14 = no_arg, jpayne@69: const Arg& ptr15 = no_arg, jpayne@69: const Arg& ptr16 = no_arg) const; jpayne@69: jpayne@69: bool Consume(StringPiece* input, jpayne@69: const Arg& ptr1 = no_arg, jpayne@69: const Arg& ptr2 = no_arg, jpayne@69: const Arg& ptr3 = no_arg, jpayne@69: const Arg& ptr4 = no_arg, jpayne@69: const Arg& ptr5 = no_arg, jpayne@69: const Arg& ptr6 = no_arg, jpayne@69: const Arg& ptr7 = no_arg, jpayne@69: const Arg& ptr8 = no_arg, jpayne@69: const Arg& ptr9 = no_arg, jpayne@69: const Arg& ptr10 = no_arg, jpayne@69: const Arg& ptr11 = no_arg, jpayne@69: const Arg& ptr12 = no_arg, jpayne@69: const Arg& ptr13 = no_arg, jpayne@69: const Arg& ptr14 = no_arg, jpayne@69: const Arg& ptr15 = no_arg, jpayne@69: const Arg& ptr16 = no_arg) const; jpayne@69: jpayne@69: bool FindAndConsume(StringPiece* input, jpayne@69: const Arg& ptr1 = no_arg, jpayne@69: const Arg& ptr2 = no_arg, jpayne@69: const Arg& ptr3 = no_arg, jpayne@69: const Arg& ptr4 = no_arg, jpayne@69: const Arg& ptr5 = no_arg, jpayne@69: const Arg& ptr6 = no_arg, jpayne@69: const Arg& ptr7 = no_arg, jpayne@69: const Arg& ptr8 = no_arg, jpayne@69: const Arg& ptr9 = no_arg, jpayne@69: const Arg& ptr10 = no_arg, jpayne@69: const Arg& ptr11 = no_arg, jpayne@69: const Arg& ptr12 = no_arg, jpayne@69: const Arg& ptr13 = no_arg, jpayne@69: const Arg& ptr14 = no_arg, jpayne@69: const Arg& ptr15 = no_arg, jpayne@69: const Arg& ptr16 = no_arg) const; jpayne@69: jpayne@69: bool Replace(const StringPiece& rewrite, jpayne@69: string *str) const; jpayne@69: jpayne@69: int GlobalReplace(const StringPiece& rewrite, jpayne@69: string *str) const; jpayne@69: jpayne@69: bool Extract(const StringPiece &rewrite, jpayne@69: const StringPiece &text, jpayne@69: string *out) const; jpayne@69: jpayne@69: // Escapes all potentially meaningful regexp characters in jpayne@69: // 'unquoted'. The returned string, used as a regular expression, jpayne@69: // will exactly match the original string. For example, jpayne@69: // 1.5-2.0? jpayne@69: // may become: jpayne@69: // 1\.5\-2\.0\? jpayne@69: // Note QuoteMeta behaves the same as perl's QuoteMeta function, jpayne@69: // *except* that it escapes the NUL character (\0) as backslash + 0, jpayne@69: // rather than backslash + NUL. jpayne@69: static string QuoteMeta(const StringPiece& unquoted); jpayne@69: jpayne@69: jpayne@69: /***** Generic matching interface *****/ jpayne@69: jpayne@69: // Type of match (TODO: Should be restructured as part of RE_Options) jpayne@69: enum Anchor { jpayne@69: UNANCHORED, // No anchoring jpayne@69: ANCHOR_START, // Anchor at start only jpayne@69: ANCHOR_BOTH // Anchor at start and end jpayne@69: }; jpayne@69: jpayne@69: // General matching routine. Stores the length of the match in jpayne@69: // "*consumed" if successful. jpayne@69: bool DoMatch(const StringPiece& text, jpayne@69: Anchor anchor, jpayne@69: int* consumed, jpayne@69: const Arg* const* args, int n) const; jpayne@69: jpayne@69: // Return the number of capturing subpatterns, or -1 if the jpayne@69: // regexp wasn't valid on construction. jpayne@69: int NumberOfCapturingGroups() const; jpayne@69: jpayne@69: // The default value for an argument, to indicate the end of the argument jpayne@69: // list. This must be used only in optional argument defaults. It should NOT jpayne@69: // be passed explicitly. Some people have tried to use it like this: jpayne@69: // jpayne@69: // FullMatch(x, y, &z, no_arg, &w); jpayne@69: // jpayne@69: // This is a mistake, and will not work. jpayne@69: static Arg no_arg; jpayne@69: jpayne@69: private: jpayne@69: jpayne@69: void Init(const string& pattern, const RE_Options* options); jpayne@69: void Cleanup(); jpayne@69: jpayne@69: // Match against "text", filling in "vec" (up to "vecsize" * 2/3) with jpayne@69: // pairs of integers for the beginning and end positions of matched jpayne@69: // text. The first pair corresponds to the entire matched text; jpayne@69: // subsequent pairs correspond, in order, to parentheses-captured jpayne@69: // matches. Returns the number of pairs (one more than the number of jpayne@69: // the last subpattern with a match) if matching was successful jpayne@69: // and zero if the match failed. jpayne@69: // I.e. for RE("(foo)|(bar)|(baz)") it will return 2, 3, and 4 when matching jpayne@69: // against "foo", "bar", and "baz" respectively. jpayne@69: // When matching RE("(foo)|hello") against "hello", it will return 1. jpayne@69: // But the values for all subpattern are filled in into "vec". jpayne@69: int TryMatch(const StringPiece& text, jpayne@69: int startpos, jpayne@69: Anchor anchor, jpayne@69: bool empty_ok, jpayne@69: int *vec, jpayne@69: int vecsize) const; jpayne@69: jpayne@69: // Append the "rewrite" string, with backslash subsitutions from "text" jpayne@69: // and "vec", to string "out". jpayne@69: bool Rewrite(string *out, jpayne@69: const StringPiece& rewrite, jpayne@69: const StringPiece& text, jpayne@69: int *vec, jpayne@69: int veclen) const; jpayne@69: jpayne@69: // internal implementation for DoMatch jpayne@69: bool DoMatchImpl(const StringPiece& text, jpayne@69: Anchor anchor, jpayne@69: int* consumed, jpayne@69: const Arg* const args[], jpayne@69: int n, jpayne@69: int* vec, jpayne@69: int vecsize) const; jpayne@69: jpayne@69: // Compile the regexp for the specified anchoring mode jpayne@69: pcre* Compile(Anchor anchor); jpayne@69: jpayne@69: string pattern_; jpayne@69: RE_Options options_; jpayne@69: pcre* re_full_; // For full matches jpayne@69: pcre* re_partial_; // For partial matches jpayne@69: const string* error_; // Error indicator (or points to empty string) jpayne@69: }; jpayne@69: jpayne@69: } // namespace pcrecpp jpayne@69: jpayne@69: #endif /* _PCRECPP_H */