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