tools/pcre/pcrecpp.h
author John Schoenick <john@pointysoftware.net>
Wed Jun 01 20:36:16 2011 -0400 (2011-06-01)
changeset 3292 f428ffc8a3fe
parent 2762 d2d467bf1b5a
child 3831 babd0eee3c5e
permissions -rw-r--r--
Fixed regression in FindEntityByClassname from bug 4899 (bug 4941, r=predcrab).
dvander@2762
     1
// Copyright (c) 2005, Google Inc.
dvander@2762
     2
// All rights reserved.
dvander@2762
     3
//
dvander@2762
     4
// Redistribution and use in source and binary forms, with or without
dvander@2762
     5
// modification, are permitted provided that the following conditions are
dvander@2762
     6
// met:
dvander@2762
     7
//
dvander@2762
     8
//     * Redistributions of source code must retain the above copyright
dvander@2762
     9
// notice, this list of conditions and the following disclaimer.
dvander@2762
    10
//     * Redistributions in binary form must reproduce the above
dvander@2762
    11
// copyright notice, this list of conditions and the following disclaimer
dvander@2762
    12
// in the documentation and/or other materials provided with the
dvander@2762
    13
// distribution.
dvander@2762
    14
//     * Neither the name of Google Inc. nor the names of its
dvander@2762
    15
// contributors may be used to endorse or promote products derived from
dvander@2762
    16
// this software without specific prior written permission.
dvander@2762
    17
//
dvander@2762
    18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
dvander@2762
    19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
dvander@2762
    20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
dvander@2762
    21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
dvander@2762
    22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
dvander@2762
    23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
dvander@2762
    24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
dvander@2762
    25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
dvander@2762
    26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
dvander@2762
    27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
dvander@2762
    28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dvander@2762
    29
//
dvander@2762
    30
// Author: Sanjay Ghemawat
dvander@2762
    31
// Support for PCRE_XXX modifiers added by Giuseppe Maxia, July 2005
dvander@2762
    32
dvander@2762
    33
#ifndef _PCRECPP_H
dvander@2762
    34
#define _PCRECPP_H
dvander@2762
    35
dvander@2762
    36
// C++ interface to the pcre regular-expression library.  RE supports
dvander@2762
    37
// Perl-style regular expressions (with extensions like \d, \w, \s,
dvander@2762
    38
// ...).
dvander@2762
    39
//
dvander@2762
    40
// -----------------------------------------------------------------------
dvander@2762
    41
// REGEXP SYNTAX:
dvander@2762
    42
//
dvander@2762
    43
// This module is part of the pcre library and hence supports its syntax
dvander@2762
    44
// for regular expressions.
dvander@2762
    45
//
dvander@2762
    46
// The syntax is pretty similar to Perl's.  For those not familiar
dvander@2762
    47
// with Perl's regular expressions, here are some examples of the most
dvander@2762
    48
// commonly used extensions:
dvander@2762
    49
//
dvander@2762
    50
//   "hello (\\w+) world"  -- \w matches a "word" character
dvander@2762
    51
//   "version (\\d+)"      -- \d matches a digit
dvander@2762
    52
//   "hello\\s+world"      -- \s matches any whitespace character
dvander@2762
    53
//   "\\b(\\w+)\\b"        -- \b matches empty string at a word boundary
dvander@2762
    54
//   "(?i)hello"           -- (?i) turns on case-insensitive matching
dvander@2762
    55
//   "/\\*(.*?)\\*/"       -- .*? matches . minimum no. of times possible
dvander@2762
    56
//
dvander@2762
    57
// -----------------------------------------------------------------------
dvander@2762
    58
// MATCHING INTERFACE:
dvander@2762
    59
//
dvander@2762
    60
// The "FullMatch" operation checks that supplied text matches a
dvander@2762
    61
// supplied pattern exactly.
dvander@2762
    62
//
dvander@2762
    63
// Example: successful match
dvander@2762
    64
//    pcrecpp::RE re("h.*o");
dvander@2762
    65
//    re.FullMatch("hello");
dvander@2762
    66
//
dvander@2762
    67
// Example: unsuccessful match (requires full match):
dvander@2762
    68
//    pcrecpp::RE re("e");
dvander@2762
    69
//    !re.FullMatch("hello");
dvander@2762
    70
//
dvander@2762
    71
// Example: creating a temporary RE object:
dvander@2762
    72
//    pcrecpp::RE("h.*o").FullMatch("hello");
dvander@2762
    73
//
dvander@2762
    74
// You can pass in a "const char*" or a "string" for "text".  The
dvander@2762
    75
// examples below tend to use a const char*.
dvander@2762
    76
//
dvander@2762
    77
// You can, as in the different examples above, store the RE object
dvander@2762
    78
// explicitly in a variable or use a temporary RE object.  The
dvander@2762
    79
// examples below use one mode or the other arbitrarily.  Either
dvander@2762
    80
// could correctly be used for any of these examples.
dvander@2762
    81
//
dvander@2762
    82
// -----------------------------------------------------------------------
dvander@2762
    83
// MATCHING WITH SUB-STRING EXTRACTION:
dvander@2762
    84
//
dvander@2762
    85
// You can supply extra pointer arguments to extract matched subpieces.
dvander@2762
    86
//
dvander@2762
    87
// Example: extracts "ruby" into "s" and 1234 into "i"
dvander@2762
    88
//    int i;
dvander@2762
    89
//    string s;
dvander@2762
    90
//    pcrecpp::RE re("(\\w+):(\\d+)");
dvander@2762
    91
//    re.FullMatch("ruby:1234", &s, &i);
dvander@2762
    92
//
dvander@2762
    93
// Example: does not try to extract any extra sub-patterns
dvander@2762
    94
//    re.FullMatch("ruby:1234", &s);
dvander@2762
    95
//
dvander@2762
    96
// Example: does not try to extract into NULL
dvander@2762
    97
//    re.FullMatch("ruby:1234", NULL, &i);
dvander@2762
    98
//
dvander@2762
    99
// Example: integer overflow causes failure
dvander@2762
   100
//    !re.FullMatch("ruby:1234567891234", NULL, &i);
dvander@2762
   101
//
dvander@2762
   102
// Example: fails because there aren't enough sub-patterns:
dvander@2762
   103
//    !pcrecpp::RE("\\w+:\\d+").FullMatch("ruby:1234", &s);
dvander@2762
   104
//
dvander@2762
   105
// Example: fails because string cannot be stored in integer
dvander@2762
   106
//    !pcrecpp::RE("(.*)").FullMatch("ruby", &i);
dvander@2762
   107
//
dvander@2762
   108
// The provided pointer arguments can be pointers to any scalar numeric
dvander@2762
   109
// type, or one of
dvander@2762
   110
//    string        (matched piece is copied to string)
dvander@2762
   111
//    StringPiece   (StringPiece is mutated to point to matched piece)
dvander@2762
   112
//    T             (where "bool T::ParseFrom(const char*, int)" exists)
dvander@2762
   113
//    NULL          (the corresponding matched sub-pattern is not copied)
dvander@2762
   114
//
dvander@2762
   115
// CAVEAT: An optional sub-pattern that does not exist in the matched
dvander@2762
   116
// string is assigned the empty string.  Therefore, the following will
dvander@2762
   117
// return false (because the empty string is not a valid number):
dvander@2762
   118
//    int number;
dvander@2762
   119
//    pcrecpp::RE::FullMatch("abc", "[a-z]+(\\d+)?", &number);
dvander@2762
   120
//
dvander@2762
   121
// -----------------------------------------------------------------------
dvander@2762
   122
// DO_MATCH
dvander@2762
   123
//
dvander@2762
   124
// The matching interface supports at most 16 arguments per call.
dvander@2762
   125
// If you need more, consider using the more general interface
dvander@2762
   126
// pcrecpp::RE::DoMatch().  See pcrecpp.h for the signature for DoMatch.
dvander@2762
   127
//
dvander@2762
   128
// -----------------------------------------------------------------------
dvander@2762
   129
// PARTIAL MATCHES
dvander@2762
   130
//
dvander@2762
   131
// You can use the "PartialMatch" operation when you want the pattern
dvander@2762
   132
// to match any substring of the text.
dvander@2762
   133
//
dvander@2762
   134
// Example: simple search for a string:
dvander@2762
   135
//    pcrecpp::RE("ell").PartialMatch("hello");
dvander@2762
   136
//
dvander@2762
   137
// Example: find first number in a string:
dvander@2762
   138
//    int number;
dvander@2762
   139
//    pcrecpp::RE re("(\\d+)");
dvander@2762
   140
//    re.PartialMatch("x*100 + 20", &number);
dvander@2762
   141
//    assert(number == 100);
dvander@2762
   142
//
dvander@2762
   143
// -----------------------------------------------------------------------
dvander@2762
   144
// UTF-8 AND THE MATCHING INTERFACE:
dvander@2762
   145
//
dvander@2762
   146
// By default, pattern and text are plain text, one byte per character.
dvander@2762
   147
// The UTF8 flag, passed to the constructor, causes both pattern
dvander@2762
   148
// and string to be treated as UTF-8 text, still a byte stream but
dvander@2762
   149
// potentially multiple bytes per character. In practice, the text
dvander@2762
   150
// is likelier to be UTF-8 than the pattern, but the match returned
dvander@2762
   151
// may depend on the UTF8 flag, so always use it when matching
dvander@2762
   152
// UTF8 text.  E.g., "." will match one byte normally but with UTF8
dvander@2762
   153
// set may match up to three bytes of a multi-byte character.
dvander@2762
   154
//
dvander@2762
   155
// Example:
dvander@2762
   156
//    pcrecpp::RE_Options options;
dvander@2762
   157
//    options.set_utf8();
dvander@2762
   158
//    pcrecpp::RE re(utf8_pattern, options);
dvander@2762
   159
//    re.FullMatch(utf8_string);
dvander@2762
   160
//
dvander@2762
   161
// Example: using the convenience function UTF8():
dvander@2762
   162
//    pcrecpp::RE re(utf8_pattern, pcrecpp::UTF8());
dvander@2762
   163
//    re.FullMatch(utf8_string);
dvander@2762
   164
//
dvander@2762
   165
// NOTE: The UTF8 option is ignored if pcre was not configured with the
dvander@2762
   166
//       --enable-utf8 flag.
dvander@2762
   167
//
dvander@2762
   168
// -----------------------------------------------------------------------
dvander@2762
   169
// PASSING MODIFIERS TO THE REGULAR EXPRESSION ENGINE
dvander@2762
   170
//
dvander@2762
   171
// PCRE defines some modifiers to change the behavior of the regular
dvander@2762
   172
// expression engine.
dvander@2762
   173
// The C++ wrapper defines an auxiliary class, RE_Options, as a vehicle
dvander@2762
   174
// to pass such modifiers to a RE class.
dvander@2762
   175
//
dvander@2762
   176
// Currently, the following modifiers are supported
dvander@2762
   177
//
dvander@2762
   178
//    modifier              description               Perl corresponding
dvander@2762
   179
//
dvander@2762
   180
//    PCRE_CASELESS         case insensitive match    /i
dvander@2762
   181
//    PCRE_MULTILINE        multiple lines match      /m
dvander@2762
   182
//    PCRE_DOTALL           dot matches newlines      /s
dvander@2762
   183
//    PCRE_DOLLAR_ENDONLY   $ matches only at end     N/A
dvander@2762
   184
//    PCRE_EXTRA            strict escape parsing     N/A
dvander@2762
   185
//    PCRE_EXTENDED         ignore whitespaces        /x
dvander@2762
   186
//    PCRE_UTF8             handles UTF8 chars        built-in
dvander@2762
   187
//    PCRE_UNGREEDY         reverses * and *?         N/A
dvander@2762
   188
//    PCRE_NO_AUTO_CAPTURE  disables matching parens  N/A (*)
dvander@2762
   189
//
dvander@2762
   190
// (For a full account on how each modifier works, please check the
dvander@2762
   191
// PCRE API reference manual).
dvander@2762
   192
//
dvander@2762
   193
// (*) Both Perl and PCRE allow non matching parentheses by means of the
dvander@2762
   194
// "?:" modifier within the pattern itself. e.g. (?:ab|cd) does not
dvander@2762
   195
// capture, while (ab|cd) does.
dvander@2762
   196
//
dvander@2762
   197
// For each modifier, there are two member functions whose name is made
dvander@2762
   198
// out of the modifier in lowercase, without the "PCRE_" prefix. For
dvander@2762
   199
// instance, PCRE_CASELESS is handled by
dvander@2762
   200
//    bool caseless(),
dvander@2762
   201
// which returns true if the modifier is set, and
dvander@2762
   202
//    RE_Options & set_caseless(bool),
dvander@2762
   203
// which sets or unsets the modifier.
dvander@2762
   204
//
dvander@2762
   205
// Moreover, PCRE_EXTRA_MATCH_LIMIT can be accessed through the
dvander@2762
   206
// set_match_limit() and match_limit() member functions.
dvander@2762
   207
// Setting match_limit to a non-zero value will limit the executation of
dvander@2762
   208
// pcre to keep it from doing bad things like blowing the stack or taking
dvander@2762
   209
// an eternity to return a result.  A value of 5000 is good enough to stop
dvander@2762
   210
// stack blowup in a 2MB thread stack.  Setting match_limit to zero will
dvander@2762
   211
// disable match limiting.  Alternately, you can set match_limit_recursion()
dvander@2762
   212
// which uses PCRE_EXTRA_MATCH_LIMIT_RECURSION to limit how much pcre
dvander@2762
   213
// recurses.  match_limit() caps the number of matches pcre does;
dvander@2762
   214
// match_limit_recrusion() caps the depth of recursion.
dvander@2762
   215
//
dvander@2762
   216
// Normally, to pass one or more modifiers to a RE class, you declare
dvander@2762
   217
// a RE_Options object, set the appropriate options, and pass this
dvander@2762
   218
// object to a RE constructor. Example:
dvander@2762
   219
//
dvander@2762
   220
//    RE_options opt;
dvander@2762
   221
//    opt.set_caseless(true);
dvander@2762
   222
//
dvander@2762
   223
//    if (RE("HELLO", opt).PartialMatch("hello world")) ...
dvander@2762
   224
//
dvander@2762
   225
// RE_options has two constructors. The default constructor takes no
dvander@2762
   226
// arguments and creates a set of flags that are off by default.
dvander@2762
   227
//
dvander@2762
   228
// The optional parameter 'option_flags' is to facilitate transfer
dvander@2762
   229
// of legacy code from C programs.  This lets you do
dvander@2762
   230
//    RE(pattern, RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str);
dvander@2762
   231
//
dvander@2762
   232
// But new code is better off doing
dvander@2762
   233
//    RE(pattern,
dvander@2762
   234
//      RE_Options().set_caseless(true).set_multiline(true)).PartialMatch(str);
dvander@2762
   235
// (See below)
dvander@2762
   236
//
dvander@2762
   237
// If you are going to pass one of the most used modifiers, there are some
dvander@2762
   238
// convenience functions that return a RE_Options class with the
dvander@2762
   239
// appropriate modifier already set:
dvander@2762
   240
// CASELESS(), UTF8(), MULTILINE(), DOTALL(), EXTENDED()
dvander@2762
   241
//
dvander@2762
   242
// If you need to set several options at once, and you don't want to go
dvander@2762
   243
// through the pains of declaring a RE_Options object and setting several
dvander@2762
   244
// options, there is a parallel method that give you such ability on the
dvander@2762
   245
// fly. You can concatenate several set_xxxxx member functions, since each
dvander@2762
   246
// of them returns a reference to its class object.  e.g.: to pass
dvander@2762
   247
// PCRE_CASELESS, PCRE_EXTENDED, and PCRE_MULTILINE to a RE with one
dvander@2762
   248
// statement, you may write
dvander@2762
   249
//
dvander@2762
   250
//    RE(" ^ xyz \\s+ .* blah$", RE_Options()
dvander@2762
   251
//                            .set_caseless(true)
dvander@2762
   252
//                            .set_extended(true)
dvander@2762
   253
//                            .set_multiline(true)).PartialMatch(sometext);
dvander@2762
   254
//
dvander@2762
   255
// -----------------------------------------------------------------------
dvander@2762
   256
// SCANNING TEXT INCREMENTALLY
dvander@2762
   257
//
dvander@2762
   258
// The "Consume" operation may be useful if you want to repeatedly
dvander@2762
   259
// match regular expressions at the front of a string and skip over
dvander@2762
   260
// them as they match.  This requires use of the "StringPiece" type,
dvander@2762
   261
// which represents a sub-range of a real string.  Like RE, StringPiece
dvander@2762
   262
// is defined in the pcrecpp namespace.
dvander@2762
   263
//
dvander@2762
   264
// Example: read lines of the form "var = value" from a string.
dvander@2762
   265
//    string contents = ...;                 // Fill string somehow
dvander@2762
   266
//    pcrecpp::StringPiece input(contents);  // Wrap in a StringPiece
dvander@2762
   267
//
dvander@2762
   268
//    string var;
dvander@2762
   269
//    int value;
dvander@2762
   270
//    pcrecpp::RE re("(\\w+) = (\\d+)\n");
dvander@2762
   271
//    while (re.Consume(&input, &var, &value)) {
dvander@2762
   272
//      ...;
dvander@2762
   273
//    }
dvander@2762
   274
//
dvander@2762
   275
// Each successful call to "Consume" will set "var/value", and also
dvander@2762
   276
// advance "input" so it points past the matched text.
dvander@2762
   277
//
dvander@2762
   278
// The "FindAndConsume" operation is similar to "Consume" but does not
dvander@2762
   279
// anchor your match at the beginning of the string.  For example, you
dvander@2762
   280
// could extract all words from a string by repeatedly calling
dvander@2762
   281
//     pcrecpp::RE("(\\w+)").FindAndConsume(&input, &word)
dvander@2762
   282
//
dvander@2762
   283
// -----------------------------------------------------------------------
dvander@2762
   284
// PARSING HEX/OCTAL/C-RADIX NUMBERS
dvander@2762
   285
//
dvander@2762
   286
// By default, if you pass a pointer to a numeric value, the
dvander@2762
   287
// corresponding text is interpreted as a base-10 number.  You can
dvander@2762
   288
// instead wrap the pointer with a call to one of the operators Hex(),
dvander@2762
   289
// Octal(), or CRadix() to interpret the text in another base.  The
dvander@2762
   290
// CRadix operator interprets C-style "0" (base-8) and "0x" (base-16)
dvander@2762
   291
// prefixes, but defaults to base-10.
dvander@2762
   292
//
dvander@2762
   293
// Example:
dvander@2762
   294
//   int a, b, c, d;
dvander@2762
   295
//   pcrecpp::RE re("(.*) (.*) (.*) (.*)");
dvander@2762
   296
//   re.FullMatch("100 40 0100 0x40",
dvander@2762
   297
//                pcrecpp::Octal(&a), pcrecpp::Hex(&b),
dvander@2762
   298
//                pcrecpp::CRadix(&c), pcrecpp::CRadix(&d));
dvander@2762
   299
// will leave 64 in a, b, c, and d.
dvander@2762
   300
//
dvander@2762
   301
// -----------------------------------------------------------------------
dvander@2762
   302
// REPLACING PARTS OF STRINGS
dvander@2762
   303
//
dvander@2762
   304
// You can replace the first match of "pattern" in "str" with
dvander@2762
   305
// "rewrite".  Within "rewrite", backslash-escaped digits (\1 to \9)
dvander@2762
   306
// can be used to insert text matching corresponding parenthesized
dvander@2762
   307
// group from the pattern.  \0 in "rewrite" refers to the entire
dvander@2762
   308
// matching text.  E.g.,
dvander@2762
   309
//
dvander@2762
   310
//   string s = "yabba dabba doo";
dvander@2762
   311
//   pcrecpp::RE("b+").Replace("d", &s);
dvander@2762
   312
//
dvander@2762
   313
// will leave "s" containing "yada dabba doo".  The result is true if
dvander@2762
   314
// the pattern matches and a replacement occurs, or false otherwise.
dvander@2762
   315
//
dvander@2762
   316
// GlobalReplace() is like Replace(), except that it replaces all
dvander@2762
   317
// occurrences of the pattern in the string with the rewrite.
dvander@2762
   318
// Replacements are not subject to re-matching.  E.g.,
dvander@2762
   319
//
dvander@2762
   320
//   string s = "yabba dabba doo";
dvander@2762
   321
//   pcrecpp::RE("b+").GlobalReplace("d", &s);
dvander@2762
   322
//
dvander@2762
   323
// will leave "s" containing "yada dada doo".  It returns the number
dvander@2762
   324
// of replacements made.
dvander@2762
   325
//
dvander@2762
   326
// Extract() is like Replace(), except that if the pattern matches,
dvander@2762
   327
// "rewrite" is copied into "out" (an additional argument) with
dvander@2762
   328
// substitutions.  The non-matching portions of "text" are ignored.
dvander@2762
   329
// Returns true iff a match occurred and the extraction happened
dvander@2762
   330
// successfully.  If no match occurs, the string is left unaffected.
dvander@2762
   331
dvander@2762
   332
dvander@2762
   333
#include <string>
dvander@2762
   334
#include <pcre.h>
dvander@2762
   335
#include <pcrecpparg.h>   // defines the Arg class
dvander@2762
   336
// This isn't technically needed here, but we include it
dvander@2762
   337
// anyway so folks who include pcrecpp.h don't have to.
dvander@2762
   338
#include <pcre_stringpiece.h>
dvander@2762
   339
dvander@2762
   340
namespace pcrecpp {
dvander@2762
   341
dvander@2762
   342
#define PCRE_SET_OR_CLEAR(b, o) \
dvander@2762
   343
    if (b) all_options_ |= (o); else all_options_ &= ~(o); \
dvander@2762
   344
    return *this
dvander@2762
   345
dvander@2762
   346
#define PCRE_IS_SET(o)  \
dvander@2762
   347
        (all_options_ & o) == o
dvander@2762
   348
dvander@2762
   349
/***** Compiling regular expressions: the RE class *****/
dvander@2762
   350
dvander@2762
   351
// RE_Options allow you to set options to be passed along to pcre,
dvander@2762
   352
// along with other options we put on top of pcre.
dvander@2762
   353
// Only 9 modifiers, plus match_limit and match_limit_recursion,
dvander@2762
   354
// are supported now.
dvander@2762
   355
class PCRECPP_EXP_DEFN RE_Options {
dvander@2762
   356
 public:
dvander@2762
   357
  // constructor
dvander@2762
   358
  RE_Options() : match_limit_(0), match_limit_recursion_(0), all_options_(0) {}
dvander@2762
   359
dvander@2762
   360
  // alternative constructor.
dvander@2762
   361
  // To facilitate transfer of legacy code from C programs
dvander@2762
   362
  //
dvander@2762
   363
  // This lets you do
dvander@2762
   364
  //    RE(pattern, RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str);
dvander@2762
   365
  // But new code is better off doing
dvander@2762
   366
  //    RE(pattern,
dvander@2762
   367
  //      RE_Options().set_caseless(true).set_multiline(true)).PartialMatch(str);
dvander@2762
   368
  RE_Options(int option_flags) : match_limit_(0), match_limit_recursion_(0),
dvander@2762
   369
                                 all_options_(option_flags) {}
dvander@2762
   370
  // we're fine with the default destructor, copy constructor, etc.
dvander@2762
   371
dvander@2762
   372
  // accessors and mutators
dvander@2762
   373
  int match_limit() const { return match_limit_; };
dvander@2762
   374
  RE_Options &set_match_limit(int limit) {
dvander@2762
   375
    match_limit_ = limit;
dvander@2762
   376
    return *this;
dvander@2762
   377
  }
dvander@2762
   378
dvander@2762
   379
  int match_limit_recursion() const { return match_limit_recursion_; };
dvander@2762
   380
  RE_Options &set_match_limit_recursion(int limit) {
dvander@2762
   381
    match_limit_recursion_ = limit;
dvander@2762
   382
    return *this;
dvander@2762
   383
  }
dvander@2762
   384
dvander@2762
   385
  bool caseless() const {
dvander@2762
   386
    return PCRE_IS_SET(PCRE_CASELESS);
dvander@2762
   387
  }
dvander@2762
   388
  RE_Options &set_caseless(bool x) {
dvander@2762
   389
    PCRE_SET_OR_CLEAR(x, PCRE_CASELESS);
dvander@2762
   390
  }
dvander@2762
   391
dvander@2762
   392
  bool multiline() const {
dvander@2762
   393
    return PCRE_IS_SET(PCRE_MULTILINE);
dvander@2762
   394
  }
dvander@2762
   395
  RE_Options &set_multiline(bool x) {
dvander@2762
   396
    PCRE_SET_OR_CLEAR(x, PCRE_MULTILINE);
dvander@2762
   397
  }
dvander@2762
   398
dvander@2762
   399
  bool dotall() const {
dvander@2762
   400
    return PCRE_IS_SET(PCRE_DOTALL);
dvander@2762
   401
  }
dvander@2762
   402
  RE_Options &set_dotall(bool x) {
dvander@2762
   403
    PCRE_SET_OR_CLEAR(x, PCRE_DOTALL);
dvander@2762
   404
  }
dvander@2762
   405
dvander@2762
   406
  bool extended() const {
dvander@2762
   407
    return PCRE_IS_SET(PCRE_EXTENDED);
dvander@2762
   408
  }
dvander@2762
   409
  RE_Options &set_extended(bool x) {
dvander@2762
   410
    PCRE_SET_OR_CLEAR(x, PCRE_EXTENDED);
dvander@2762
   411
  }
dvander@2762
   412
dvander@2762
   413
  bool dollar_endonly() const {
dvander@2762
   414
    return PCRE_IS_SET(PCRE_DOLLAR_ENDONLY);
dvander@2762
   415
  }
dvander@2762
   416
  RE_Options &set_dollar_endonly(bool x) {
dvander@2762
   417
    PCRE_SET_OR_CLEAR(x, PCRE_DOLLAR_ENDONLY);
dvander@2762
   418
  }
dvander@2762
   419
dvander@2762
   420
  bool extra() const {
dvander@2762
   421
    return PCRE_IS_SET(PCRE_EXTRA);
dvander@2762
   422
  }
dvander@2762
   423
  RE_Options &set_extra(bool x) {
dvander@2762
   424
    PCRE_SET_OR_CLEAR(x, PCRE_EXTRA);
dvander@2762
   425
  }
dvander@2762
   426
dvander@2762
   427
  bool ungreedy() const {
dvander@2762
   428
    return PCRE_IS_SET(PCRE_UNGREEDY);
dvander@2762
   429
  }
dvander@2762
   430
  RE_Options &set_ungreedy(bool x) {
dvander@2762
   431
    PCRE_SET_OR_CLEAR(x, PCRE_UNGREEDY);
dvander@2762
   432
  }
dvander@2762
   433
dvander@2762
   434
  bool utf8() const {
dvander@2762
   435
    return PCRE_IS_SET(PCRE_UTF8);
dvander@2762
   436
  }
dvander@2762
   437
  RE_Options &set_utf8(bool x) {
dvander@2762
   438
    PCRE_SET_OR_CLEAR(x, PCRE_UTF8);
dvander@2762
   439
  }
dvander@2762
   440
dvander@2762
   441
  bool no_auto_capture() const {
dvander@2762
   442
    return PCRE_IS_SET(PCRE_NO_AUTO_CAPTURE);
dvander@2762
   443
  }
dvander@2762
   444
  RE_Options &set_no_auto_capture(bool x) {
dvander@2762
   445
    PCRE_SET_OR_CLEAR(x, PCRE_NO_AUTO_CAPTURE);
dvander@2762
   446
  }
dvander@2762
   447
dvander@2762
   448
  RE_Options &set_all_options(int opt) {
dvander@2762
   449
    all_options_ = opt;
dvander@2762
   450
    return *this;
dvander@2762
   451
  }
dvander@2762
   452
  int all_options() const {
dvander@2762
   453
    return all_options_ ;
dvander@2762
   454
  }
dvander@2762
   455
dvander@2762
   456
  // TODO: add other pcre flags
dvander@2762
   457
dvander@2762
   458
 private:
dvander@2762
   459
  int match_limit_;
dvander@2762
   460
  int match_limit_recursion_;
dvander@2762
   461
  int all_options_;
dvander@2762
   462
};
dvander@2762
   463
dvander@2762
   464
// These functions return some common RE_Options
dvander@2762
   465
static inline RE_Options UTF8() {
dvander@2762
   466
  return RE_Options().set_utf8(true);
dvander@2762
   467
}
dvander@2762
   468
dvander@2762
   469
static inline RE_Options CASELESS() {
dvander@2762
   470
  return RE_Options().set_caseless(true);
dvander@2762
   471
}
dvander@2762
   472
static inline RE_Options MULTILINE() {
dvander@2762
   473
  return RE_Options().set_multiline(true);
dvander@2762
   474
}
dvander@2762
   475
dvander@2762
   476
static inline RE_Options DOTALL() {
dvander@2762
   477
  return RE_Options().set_dotall(true);
dvander@2762
   478
}
dvander@2762
   479
dvander@2762
   480
static inline RE_Options EXTENDED() {
dvander@2762
   481
  return RE_Options().set_extended(true);
dvander@2762
   482
}
dvander@2762
   483
dvander@2762
   484
// Interface for regular expression matching.  Also corresponds to a
dvander@2762
   485
// pre-compiled regular expression.  An "RE" object is safe for
dvander@2762
   486
// concurrent use by multiple threads.
dvander@2762
   487
class PCRECPP_EXP_DEFN RE {
dvander@2762
   488
 public:
dvander@2762
   489
  // We provide implicit conversions from strings so that users can
dvander@2762
   490
  // pass in a string or a "const char*" wherever an "RE" is expected.
dvander@2762
   491
  RE(const string& pat) { Init(pat, NULL); }
dvander@2762
   492
  RE(const string& pat, const RE_Options& option) { Init(pat, &option); }
dvander@2762
   493
  RE(const char* pat) { Init(pat, NULL); }
dvander@2762
   494
  RE(const char* pat, const RE_Options& option) { Init(pat, &option); }
dvander@2762
   495
  RE(const unsigned char* pat) {
dvander@2762
   496
    Init(reinterpret_cast<const char*>(pat), NULL);
dvander@2762
   497
  }
dvander@2762
   498
  RE(const unsigned char* pat, const RE_Options& option) {
dvander@2762
   499
    Init(reinterpret_cast<const char*>(pat), &option);
dvander@2762
   500
  }
dvander@2762
   501
dvander@2762
   502
  // Copy constructor & assignment - note that these are expensive
dvander@2762
   503
  // because they recompile the expression.
dvander@2762
   504
  RE(const RE& re) { Init(re.pattern_, &re.options_); }
dvander@2762
   505
  const RE& operator=(const RE& re) {
dvander@2762
   506
    if (this != &re) {
dvander@2762
   507
      Cleanup();
dvander@2762
   508
dvander@2762
   509
      // This is the code that originally came from Google
dvander@2762
   510
      // Init(re.pattern_.c_str(), &re.options_);
dvander@2762
   511
dvander@2762
   512
      // This is the replacement from Ari Pollak
dvander@2762
   513
      Init(re.pattern_, &re.options_);
dvander@2762
   514
    }
dvander@2762
   515
    return *this;
dvander@2762
   516
  }
dvander@2762
   517
dvander@2762
   518
dvander@2762
   519
  ~RE();
dvander@2762
   520
dvander@2762
   521
  // The string specification for this RE.  E.g.
dvander@2762
   522
  //   RE re("ab*c?d+");
dvander@2762
   523
  //   re.pattern();    // "ab*c?d+"
dvander@2762
   524
  const string& pattern() const { return pattern_; }
dvander@2762
   525
dvander@2762
   526
  // If RE could not be created properly, returns an error string.
dvander@2762
   527
  // Else returns the empty string.
dvander@2762
   528
  const string& error() const { return *error_; }
dvander@2762
   529
dvander@2762
   530
  /***** The useful part: the matching interface *****/
dvander@2762
   531
dvander@2762
   532
  // This is provided so one can do pattern.ReplaceAll() just as
dvander@2762
   533
  // easily as ReplaceAll(pattern-text, ....)
dvander@2762
   534
dvander@2762
   535
  bool FullMatch(const StringPiece& text,
dvander@2762
   536
                 const Arg& ptr1 = no_arg,
dvander@2762
   537
                 const Arg& ptr2 = no_arg,
dvander@2762
   538
                 const Arg& ptr3 = no_arg,
dvander@2762
   539
                 const Arg& ptr4 = no_arg,
dvander@2762
   540
                 const Arg& ptr5 = no_arg,
dvander@2762
   541
                 const Arg& ptr6 = no_arg,
dvander@2762
   542
                 const Arg& ptr7 = no_arg,
dvander@2762
   543
                 const Arg& ptr8 = no_arg,
dvander@2762
   544
                 const Arg& ptr9 = no_arg,
dvander@2762
   545
                 const Arg& ptr10 = no_arg,
dvander@2762
   546
                 const Arg& ptr11 = no_arg,
dvander@2762
   547
                 const Arg& ptr12 = no_arg,
dvander@2762
   548
                 const Arg& ptr13 = no_arg,
dvander@2762
   549
                 const Arg& ptr14 = no_arg,
dvander@2762
   550
                 const Arg& ptr15 = no_arg,
dvander@2762
   551
                 const Arg& ptr16 = no_arg) const;
dvander@2762
   552
dvander@2762
   553
  bool PartialMatch(const StringPiece& text,
dvander@2762
   554
                    const Arg& ptr1 = no_arg,
dvander@2762
   555
                    const Arg& ptr2 = no_arg,
dvander@2762
   556
                    const Arg& ptr3 = no_arg,
dvander@2762
   557
                    const Arg& ptr4 = no_arg,
dvander@2762
   558
                    const Arg& ptr5 = no_arg,
dvander@2762
   559
                    const Arg& ptr6 = no_arg,
dvander@2762
   560
                    const Arg& ptr7 = no_arg,
dvander@2762
   561
                    const Arg& ptr8 = no_arg,
dvander@2762
   562
                    const Arg& ptr9 = no_arg,
dvander@2762
   563
                    const Arg& ptr10 = no_arg,
dvander@2762
   564
                    const Arg& ptr11 = no_arg,
dvander@2762
   565
                    const Arg& ptr12 = no_arg,
dvander@2762
   566
                    const Arg& ptr13 = no_arg,
dvander@2762
   567
                    const Arg& ptr14 = no_arg,
dvander@2762
   568
                    const Arg& ptr15 = no_arg,
dvander@2762
   569
                    const Arg& ptr16 = no_arg) const;
dvander@2762
   570
dvander@2762
   571
  bool Consume(StringPiece* input,
dvander@2762
   572
               const Arg& ptr1 = no_arg,
dvander@2762
   573
               const Arg& ptr2 = no_arg,
dvander@2762
   574
               const Arg& ptr3 = no_arg,
dvander@2762
   575
               const Arg& ptr4 = no_arg,
dvander@2762
   576
               const Arg& ptr5 = no_arg,
dvander@2762
   577
               const Arg& ptr6 = no_arg,
dvander@2762
   578
               const Arg& ptr7 = no_arg,
dvander@2762
   579
               const Arg& ptr8 = no_arg,
dvander@2762
   580
               const Arg& ptr9 = no_arg,
dvander@2762
   581
               const Arg& ptr10 = no_arg,
dvander@2762
   582
               const Arg& ptr11 = no_arg,
dvander@2762
   583
               const Arg& ptr12 = no_arg,
dvander@2762
   584
               const Arg& ptr13 = no_arg,
dvander@2762
   585
               const Arg& ptr14 = no_arg,
dvander@2762
   586
               const Arg& ptr15 = no_arg,
dvander@2762
   587
               const Arg& ptr16 = no_arg) const;
dvander@2762
   588
dvander@2762
   589
  bool FindAndConsume(StringPiece* input,
dvander@2762
   590
                      const Arg& ptr1 = no_arg,
dvander@2762
   591
                      const Arg& ptr2 = no_arg,
dvander@2762
   592
                      const Arg& ptr3 = no_arg,
dvander@2762
   593
                      const Arg& ptr4 = no_arg,
dvander@2762
   594
                      const Arg& ptr5 = no_arg,
dvander@2762
   595
                      const Arg& ptr6 = no_arg,
dvander@2762
   596
                      const Arg& ptr7 = no_arg,
dvander@2762
   597
                      const Arg& ptr8 = no_arg,
dvander@2762
   598
                      const Arg& ptr9 = no_arg,
dvander@2762
   599
                      const Arg& ptr10 = no_arg,
dvander@2762
   600
                      const Arg& ptr11 = no_arg,
dvander@2762
   601
                      const Arg& ptr12 = no_arg,
dvander@2762
   602
                      const Arg& ptr13 = no_arg,
dvander@2762
   603
                      const Arg& ptr14 = no_arg,
dvander@2762
   604
                      const Arg& ptr15 = no_arg,
dvander@2762
   605
                      const Arg& ptr16 = no_arg) const;
dvander@2762
   606
dvander@2762
   607
  bool Replace(const StringPiece& rewrite,
dvander@2762
   608
               string *str) const;
dvander@2762
   609
dvander@2762
   610
  int GlobalReplace(const StringPiece& rewrite,
dvander@2762
   611
                    string *str) const;
dvander@2762
   612
dvander@2762
   613
  bool Extract(const StringPiece &rewrite,
dvander@2762
   614
               const StringPiece &text,
dvander@2762
   615
               string *out) const;
dvander@2762
   616
dvander@2762
   617
  // Escapes all potentially meaningful regexp characters in
dvander@2762
   618
  // 'unquoted'.  The returned string, used as a regular expression,
dvander@2762
   619
  // will exactly match the original string.  For example,
dvander@2762
   620
  //           1.5-2.0?
dvander@2762
   621
  // may become:
dvander@2762
   622
  //           1\.5\-2\.0\?
dvander@2762
   623
  // Note QuoteMeta behaves the same as perl's QuoteMeta function,
dvander@2762
   624
  // *except* that it escapes the NUL character (\0) as backslash + 0,
dvander@2762
   625
  // rather than backslash + NUL.
dvander@2762
   626
  static string QuoteMeta(const StringPiece& unquoted);
dvander@2762
   627
dvander@2762
   628
dvander@2762
   629
  /***** Generic matching interface *****/
dvander@2762
   630
dvander@2762
   631
  // Type of match (TODO: Should be restructured as part of RE_Options)
dvander@2762
   632
  enum Anchor {
dvander@2762
   633
    UNANCHORED,         // No anchoring
dvander@2762
   634
    ANCHOR_START,       // Anchor at start only
dvander@2762
   635
    ANCHOR_BOTH         // Anchor at start and end
dvander@2762
   636
  };
dvander@2762
   637
dvander@2762
   638
  // General matching routine.  Stores the length of the match in
dvander@2762
   639
  // "*consumed" if successful.
dvander@2762
   640
  bool DoMatch(const StringPiece& text,
dvander@2762
   641
               Anchor anchor,
dvander@2762
   642
               int* consumed,
dvander@2762
   643
               const Arg* const* args, int n) const;
dvander@2762
   644
dvander@2762
   645
  // Return the number of capturing subpatterns, or -1 if the
dvander@2762
   646
  // regexp wasn't valid on construction.
dvander@2762
   647
  int NumberOfCapturingGroups() const;
dvander@2762
   648
dvander@2762
   649
  // The default value for an argument, to indicate the end of the argument
dvander@2762
   650
  // list. This must be used only in optional argument defaults. It should NOT
dvander@2762
   651
  // be passed explicitly. Some people have tried to use it like this:
dvander@2762
   652
  //
dvander@2762
   653
  //   FullMatch(x, y, &z, no_arg, &w);
dvander@2762
   654
  //
dvander@2762
   655
  // This is a mistake, and will not work.
dvander@2762
   656
  static Arg no_arg;
dvander@2762
   657
dvander@2762
   658
 private:
dvander@2762
   659
dvander@2762
   660
  void Init(const string& pattern, const RE_Options* options);
dvander@2762
   661
  void Cleanup();
dvander@2762
   662
dvander@2762
   663
  // Match against "text", filling in "vec" (up to "vecsize" * 2/3) with
dvander@2762
   664
  // pairs of integers for the beginning and end positions of matched
dvander@2762
   665
  // text.  The first pair corresponds to the entire matched text;
dvander@2762
   666
  // subsequent pairs correspond, in order, to parentheses-captured
dvander@2762
   667
  // matches.  Returns the number of pairs (one more than the number of
dvander@2762
   668
  // the last subpattern with a match) if matching was successful
dvander@2762
   669
  // and zero if the match failed.
dvander@2762
   670
  // I.e. for RE("(foo)|(bar)|(baz)") it will return 2, 3, and 4 when matching
dvander@2762
   671
  // against "foo", "bar", and "baz" respectively.
dvander@2762
   672
  // When matching RE("(foo)|hello") against "hello", it will return 1.
dvander@2762
   673
  // But the values for all subpattern are filled in into "vec".
dvander@2762
   674
  int TryMatch(const StringPiece& text,
dvander@2762
   675
               int startpos,
dvander@2762
   676
               Anchor anchor,
dvander@2762
   677
               int *vec,
dvander@2762
   678
               int vecsize) const;
dvander@2762
   679
dvander@2762
   680
  // Append the "rewrite" string, with backslash subsitutions from "text"
dvander@2762
   681
  // and "vec", to string "out".
dvander@2762
   682
  bool Rewrite(string *out,
dvander@2762
   683
               const StringPiece& rewrite,
dvander@2762
   684
               const StringPiece& text,
dvander@2762
   685
               int *vec,
dvander@2762
   686
               int veclen) const;
dvander@2762
   687
dvander@2762
   688
  // internal implementation for DoMatch
dvander@2762
   689
  bool DoMatchImpl(const StringPiece& text,
dvander@2762
   690
                   Anchor anchor,
dvander@2762
   691
                   int* consumed,
dvander@2762
   692
                   const Arg* const args[],
dvander@2762
   693
                   int n,
dvander@2762
   694
                   int* vec,
dvander@2762
   695
                   int vecsize) const;
dvander@2762
   696
dvander@2762
   697
  // Compile the regexp for the specified anchoring mode
dvander@2762
   698
  pcre* Compile(Anchor anchor);
dvander@2762
   699
dvander@2762
   700
  string        pattern_;
dvander@2762
   701
  RE_Options    options_;
dvander@2762
   702
  pcre*         re_full_;       // For full matches
dvander@2762
   703
  pcre*         re_partial_;    // For partial matches
dvander@2762
   704
  const string* error_;         // Error indicator (or points to empty string)
dvander@2762
   705
};
dvander@2762
   706
dvander@2762
   707
}   // namespace pcrecpp
dvander@2762
   708
dvander@2762
   709
#endif /* _PCRECPP_H */