1 |
// Copyright (c) 2005, Google Inc.
|
2 |
// All rights reserved.
|
3 |
//
|
4 |
// Redistribution and use in source and binary forms, with or without
|
5 |
// modification, are permitted provided that the following conditions are
|
6 |
// met:
|
7 |
//
|
8 |
// * Redistributions of source code must retain the above copyright
|
9 |
// notice, this list of conditions and the following disclaimer.
|
10 |
// * Redistributions in binary form must reproduce the above
|
11 |
// copyright notice, this list of conditions and the following disclaimer
|
12 |
// in the documentation and/or other materials provided with the
|
13 |
// distribution.
|
14 |
// * Neither the name of Google Inc. nor the names of its
|
15 |
// contributors may be used to endorse or promote products derived from
|
16 |
// this software without specific prior written permission.
|
17 |
//
|
18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29 |
//
|
30 |
// Author: Sanjay Ghemawat
|
31 |
|
32 |
#ifndef _PCRE_REGEXP_H
|
33 |
#define _PCRE_REGEXP_H
|
34 |
|
35 |
// C++ interface to the pcre regular-expression library. RE supports
|
36 |
// Perl-style regular expressions (with extensions like \d, \w, \s,
|
37 |
// ...).
|
38 |
//
|
39 |
// -----------------------------------------------------------------------
|
40 |
// REGEXP SYNTAX:
|
41 |
//
|
42 |
// This module is part of the pcre library and hence supports its syntax
|
43 |
// for regular expressions.
|
44 |
//
|
45 |
// The syntax is pretty similar to Perl's. For those not familiar
|
46 |
// with Perl's regular expressions, here are some examples of the most
|
47 |
// commonly used extensions:
|
48 |
//
|
49 |
// "hello (\\w+) world" -- \w matches a "word" character
|
50 |
// "version (\\d+)" -- \d matches a digit
|
51 |
// "hello\\s+world" -- \s matches any whitespace character
|
52 |
// "\\b(\\w+)\\b" -- \b matches empty string at a word boundary
|
53 |
// "(?i)hello" -- (?i) turns on case-insensitive matching
|
54 |
// "/\\*(.*?)\\*/" -- .*? matches . minimum no. of times possible
|
55 |
//
|
56 |
// -----------------------------------------------------------------------
|
57 |
// MATCHING INTERFACE:
|
58 |
//
|
59 |
// The "FullMatch" operation checks that supplied text matches a
|
60 |
// supplied pattern exactly.
|
61 |
//
|
62 |
// Example: successful match
|
63 |
// pcrecpp::RE re("h.*o");
|
64 |
// re.FullMatch("hello");
|
65 |
//
|
66 |
// Example: unsuccessful match (requires full match):
|
67 |
// pcrecpp::RE re("e");
|
68 |
// !re.FullMatch("hello");
|
69 |
//
|
70 |
// Example: creating a temporary RE object:
|
71 |
// pcrecpp::RE("h.*o").FullMatch("hello");
|
72 |
//
|
73 |
// You can pass in a "const char*" or a "string" for "text". The
|
74 |
// examples below tend to use a const char*.
|
75 |
//
|
76 |
// You can, as in the different examples above, store the RE object
|
77 |
// explicitly in a variable or use a temporary RE object. The
|
78 |
// examples below use one mode or the other arbitrarily. Either
|
79 |
// could correctly be used for any of these examples.
|
80 |
//
|
81 |
// -----------------------------------------------------------------------
|
82 |
// MATCHING WITH SUB-STRING EXTRACTION:
|
83 |
//
|
84 |
// You can supply extra pointer arguments to extract matched subpieces.
|
85 |
//
|
86 |
// Example: extracts "ruby" into "s" and 1234 into "i"
|
87 |
// int i;
|
88 |
// string s;
|
89 |
// pcrecpp::RE re("(\\w+):(\\d+)");
|
90 |
// re.FullMatch("ruby:1234", &s, &i);
|
91 |
//
|
92 |
// Example: does not try to extract any extra sub-patterns
|
93 |
// re.FullMatch("ruby:1234", &s);
|
94 |
//
|
95 |
// Example: does not try to extract into NULL
|
96 |
// re.FullMatch("ruby:1234", NULL, &i);
|
97 |
//
|
98 |
// Example: integer overflow causes failure
|
99 |
// !re.FullMatch("ruby:1234567891234", NULL, &i);
|
100 |
//
|
101 |
// Example: fails because there aren't enough sub-patterns:
|
102 |
// !pcrecpp::RE("\\w+:\\d+").FullMatch("ruby:1234", &s);
|
103 |
//
|
104 |
// Example: fails because string cannot be stored in integer
|
105 |
// !pcrecpp::RE("(.*)").FullMatch("ruby", &i);
|
106 |
//
|
107 |
// The provided pointer arguments can be pointers to any scalar numeric
|
108 |
// type, or one of
|
109 |
// string (matched piece is copied to string)
|
110 |
// StringPiece (StringPiece is mutated to point to matched piece)
|
111 |
// T (where "bool T::ParseFrom(const char*, int)" exists)
|
112 |
// NULL (the corresponding matched sub-pattern is not copied)
|
113 |
//
|
114 |
// -----------------------------------------------------------------------
|
115 |
// DO_MATCH
|
116 |
//
|
117 |
// The matching interface supports at most 16 arguments per call.
|
118 |
// If you need more, consider using the more general interface
|
119 |
// pcrecpp::RE::DoMatch(). See pcrecpp.h for the signature for DoMatch.
|
120 |
//
|
121 |
// -----------------------------------------------------------------------
|
122 |
// PARTIAL MATCHES
|
123 |
//
|
124 |
// You can use the "PartialMatch" operation when you want the pattern
|
125 |
// to match any substring of the text.
|
126 |
//
|
127 |
// Example: simple search for a string:
|
128 |
// pcrecpp::RE("ell").PartialMatch("hello");
|
129 |
//
|
130 |
// Example: find first number in a string:
|
131 |
// int number;
|
132 |
// pcrecpp::RE re("(\\d+)");
|
133 |
// re.PartialMatch("x*100 + 20", &number);
|
134 |
// assert(number == 100);
|
135 |
//
|
136 |
// -----------------------------------------------------------------------
|
137 |
// UTF-8 AND THE MATCHING INTERFACE:
|
138 |
//
|
139 |
// By default, pattern and text are plain text, one byte per character.
|
140 |
// The UTF8 flag, passed to the constructor, causes both pattern
|
141 |
// and string to be treated as UTF-8 text, still a byte stream but
|
142 |
// potentially multiple bytes per character. In practice, the text
|
143 |
// is likelier to be UTF-8 than the pattern, but the match returned
|
144 |
// may depend on the UTF8 flag, so always use it when matching
|
145 |
// UTF8 text. E.g., "." will match one byte normally but with UTF8
|
146 |
// set may match up to three bytes of a multi-byte character.
|
147 |
//
|
148 |
// Example:
|
149 |
// pcrecpp::RE_Options options;
|
150 |
// options.set_utf8();
|
151 |
// pcrecpp::RE re(utf8_pattern, options);
|
152 |
// re.FullMatch(utf8_string);
|
153 |
//
|
154 |
// Example: using the convenience function UTF8():
|
155 |
// pcrecpp::RE re(utf8_pattern, pcrecpp::UTF8());
|
156 |
// re.FullMatch(utf8_string);
|
157 |
//
|
158 |
// NOTE: The UTF8 option is ignored if pcre was not configured with the
|
159 |
// --enable-utf8 flag.
|
160 |
//
|
161 |
// -----------------------------------------------------------------------
|
162 |
// SCANNING TEXT INCREMENTALLY
|
163 |
//
|
164 |
// The "Consume" operation may be useful if you want to repeatedly
|
165 |
// match regular expressions at the front of a string and skip over
|
166 |
// them as they match. This requires use of the "StringPiece" type,
|
167 |
// which represents a sub-range of a real string. Like RE, StringPiece
|
168 |
// is defined in the pcrecpp namespace.
|
169 |
//
|
170 |
// Example: read lines of the form "var = value" from a string.
|
171 |
// string contents = ...; // Fill string somehow
|
172 |
// pcrecpp::StringPiece input(contents); // Wrap in a StringPiece
|
173 |
//
|
174 |
// string var;
|
175 |
// int value;
|
176 |
// pcrecpp::RE re("(\\w+) = (\\d+)\n");
|
177 |
// while (re.Consume(&input, &var, &value)) {
|
178 |
// ...;
|
179 |
// }
|
180 |
//
|
181 |
// Each successful call to "Consume" will set "var/value", and also
|
182 |
// advance "input" so it points past the matched text.
|
183 |
//
|
184 |
// The "FindAndConsume" operation is similar to "Consume" but does not
|
185 |
// anchor your match at the beginning of the string. For example, you
|
186 |
// could extract all words from a string by repeatedly calling
|
187 |
// pcrecpp::RE("(\\w+)").FindAndConsume(&input, &word)
|
188 |
//
|
189 |
// -----------------------------------------------------------------------
|
190 |
// PARSING HEX/OCTAL/C-RADIX NUMBERS
|
191 |
//
|
192 |
// By default, if you pass a pointer to a numeric value, the
|
193 |
// corresponding text is interpreted as a base-10 number. You can
|
194 |
// instead wrap the pointer with a call to one of the operators Hex(),
|
195 |
// Octal(), or CRadix() to interpret the text in another base. The
|
196 |
// CRadix operator interprets C-style "0" (base-8) and "0x" (base-16)
|
197 |
// prefixes, but defaults to base-10.
|
198 |
//
|
199 |
// Example:
|
200 |
// int a, b, c, d;
|
201 |
// pcrecpp::RE re("(.*) (.*) (.*) (.*)");
|
202 |
// re.FullMatch("100 40 0100 0x40",
|
203 |
// pcrecpp::Octal(&a), pcrecpp::Hex(&b),
|
204 |
// pcrecpp::CRadix(&c), pcrecpp::CRadix(&d));
|
205 |
// will leave 64 in a, b, c, and d.
|
206 |
//
|
207 |
// -----------------------------------------------------------------------
|
208 |
// REPLACING PARTS OF STRINGS
|
209 |
//
|
210 |
// You can replace the first match of "pattern" in "str" with
|
211 |
// "rewrite". Within "rewrite", backslash-escaped digits (\1 to \9)
|
212 |
// can be used to insert text matching corresponding parenthesized
|
213 |
// group from the pattern. \0 in "rewrite" refers to the entire
|
214 |
// matching text. E.g.,
|
215 |
//
|
216 |
// string s = "yabba dabba doo";
|
217 |
// pcrecpp::RE("b+").Replace("d", &s);
|
218 |
//
|
219 |
// will leave "s" containing "yada dabba doo". The result is true if
|
220 |
// the pattern matches and a replacement occurs, or false otherwise.
|
221 |
//
|
222 |
// GlobalReplace() is like Replace(), except that it replaces all
|
223 |
// occurrences of the pattern in the string with the rewrite.
|
224 |
// Replacements are not subject to re-matching. E.g.,
|
225 |
//
|
226 |
// string s = "yabba dabba doo";
|
227 |
// pcrecpp::RE("b+").GlobalReplace("d", &s);
|
228 |
//
|
229 |
// will leave "s" containing "yada dada doo". It returns the number
|
230 |
// of replacements made.
|
231 |
//
|
232 |
// Extract() is like Replace(), except that if the pattern matches,
|
233 |
// "rewrite" is copied into "out" (an additional argument) with
|
234 |
// substitutions. The non-matching portions of "text" are ignored.
|
235 |
// Returns true iff a match occurred and the extraction happened
|
236 |
// successfully. If no match occurs, the string is left unaffected.
|
237 |
|
238 |
|
239 |
#include <string>
|
240 |
// These aren't technically needed here, but we include them
|
241 |
// anyway so folks who include pcrecpp.h don't have to include
|
242 |
// all these other header files as well.
|
243 |
#include <pcre.h>
|
244 |
#include <pcre_stringpiece.h>
|
245 |
|
246 |
namespace pcrecpp {
|
247 |
|
248 |
// We convert user-passed pointers into special Arg objects
|
249 |
class Arg;
|
250 |
extern Arg no_arg;
|
251 |
|
252 |
/***** Compiling regular expressions: the RE class *****/
|
253 |
|
254 |
// RE_Options allow you to set options to be passed along to pcre,
|
255 |
// along with other options we put on top of pcre. Only UTF and
|
256 |
// match_limit are supported now. Setting match_limit
|
257 |
// to a non-zero value will limit the executation of pcre to
|
258 |
// keep it from doing bad things like blowing the stack or taking
|
259 |
// an eternity to return a result. A value of 5000 is good enough
|
260 |
// to stop stack blowup in a 2MB thread stack.
|
261 |
// Setting match_limit to zero will disable match limiting.
|
262 |
class RE_Options {
|
263 |
public:
|
264 |
// constructor
|
265 |
RE_Options() : match_limit_(0), utf8_(false) {}
|
266 |
// we're fine with the default destructor, copy constructor, etc.
|
267 |
|
268 |
// accessors and mutators
|
269 |
int match_limit() const { return match_limit_; };
|
270 |
void set_match_limit(int limit) {
|
271 |
match_limit_ = limit;
|
272 |
}
|
273 |
|
274 |
bool utf8() const { return utf8_; }
|
275 |
void set_utf8(bool u) {
|
276 |
utf8_ = u;
|
277 |
}
|
278 |
|
279 |
// TODO: add other pcre flags
|
280 |
|
281 |
private:
|
282 |
int match_limit_;
|
283 |
bool utf8_;
|
284 |
};
|
285 |
|
286 |
// These functions return some common RE_Options
|
287 |
static inline RE_Options UTF8() {
|
288 |
RE_Options options;
|
289 |
options.set_utf8(true);
|
290 |
return options;
|
291 |
}
|
292 |
|
293 |
|
294 |
// Interface for regular expression matching. Also corresponds to a
|
295 |
// pre-compiled regular expression. An "RE" object is safe for
|
296 |
// concurrent use by multiple threads.
|
297 |
class RE {
|
298 |
public:
|
299 |
// We provide implicit conversions from strings so that users can
|
300 |
// pass in a string or a "const char*" wherever an "RE" is expected.
|
301 |
RE(const char* pat) { Init(pat, NULL); }
|
302 |
RE(const char *pat, const RE_Options& option) { Init(pat, &option); }
|
303 |
RE(const string& pat) { Init(pat.c_str(), NULL); }
|
304 |
RE(const string& pat, const RE_Options& option) { Init(pat.c_str(), &option); }
|
305 |
|
306 |
~RE();
|
307 |
|
308 |
// The string specification for this RE. E.g.
|
309 |
// RE re("ab*c?d+");
|
310 |
// re.pattern(); // "ab*c?d+"
|
311 |
const string& pattern() const { return pattern_; }
|
312 |
|
313 |
// If RE could not be created properly, returns an error string.
|
314 |
// Else returns the empty string.
|
315 |
const string& error() const { return *error_; }
|
316 |
|
317 |
/***** The useful part: the matching interface *****/
|
318 |
|
319 |
// This is provided so one can do pattern.ReplaceAll() just as
|
320 |
// easily as ReplaceAll(pattern-text, ....)
|
321 |
|
322 |
bool FullMatch(const StringPiece& text,
|
323 |
const Arg& ptr1 = no_arg,
|
324 |
const Arg& ptr2 = no_arg,
|
325 |
const Arg& ptr3 = no_arg,
|
326 |
const Arg& ptr4 = no_arg,
|
327 |
const Arg& ptr5 = no_arg,
|
328 |
const Arg& ptr6 = no_arg,
|
329 |
const Arg& ptr7 = no_arg,
|
330 |
const Arg& ptr8 = no_arg,
|
331 |
const Arg& ptr9 = no_arg,
|
332 |
const Arg& ptr10 = no_arg,
|
333 |
const Arg& ptr11 = no_arg,
|
334 |
const Arg& ptr12 = no_arg,
|
335 |
const Arg& ptr13 = no_arg,
|
336 |
const Arg& ptr14 = no_arg,
|
337 |
const Arg& ptr15 = no_arg,
|
338 |
const Arg& ptr16 = no_arg) const;
|
339 |
|
340 |
bool PartialMatch(const StringPiece& text,
|
341 |
const Arg& ptr1 = no_arg,
|
342 |
const Arg& ptr2 = no_arg,
|
343 |
const Arg& ptr3 = no_arg,
|
344 |
const Arg& ptr4 = no_arg,
|
345 |
const Arg& ptr5 = no_arg,
|
346 |
const Arg& ptr6 = no_arg,
|
347 |
const Arg& ptr7 = no_arg,
|
348 |
const Arg& ptr8 = no_arg,
|
349 |
const Arg& ptr9 = no_arg,
|
350 |
const Arg& ptr10 = no_arg,
|
351 |
const Arg& ptr11 = no_arg,
|
352 |
const Arg& ptr12 = no_arg,
|
353 |
const Arg& ptr13 = no_arg,
|
354 |
const Arg& ptr14 = no_arg,
|
355 |
const Arg& ptr15 = no_arg,
|
356 |
const Arg& ptr16 = no_arg) const;
|
357 |
|
358 |
bool Consume(StringPiece* input,
|
359 |
const Arg& ptr1 = no_arg,
|
360 |
const Arg& ptr2 = no_arg,
|
361 |
const Arg& ptr3 = no_arg,
|
362 |
const Arg& ptr4 = no_arg,
|
363 |
const Arg& ptr5 = no_arg,
|
364 |
const Arg& ptr6 = no_arg,
|
365 |
const Arg& ptr7 = no_arg,
|
366 |
const Arg& ptr8 = no_arg,
|
367 |
const Arg& ptr9 = no_arg,
|
368 |
const Arg& ptr10 = no_arg,
|
369 |
const Arg& ptr11 = no_arg,
|
370 |
const Arg& ptr12 = no_arg,
|
371 |
const Arg& ptr13 = no_arg,
|
372 |
const Arg& ptr14 = no_arg,
|
373 |
const Arg& ptr15 = no_arg,
|
374 |
const Arg& ptr16 = no_arg) const;
|
375 |
|
376 |
bool FindAndConsume(StringPiece* input,
|
377 |
const Arg& ptr1 = no_arg,
|
378 |
const Arg& ptr2 = no_arg,
|
379 |
const Arg& ptr3 = no_arg,
|
380 |
const Arg& ptr4 = no_arg,
|
381 |
const Arg& ptr5 = no_arg,
|
382 |
const Arg& ptr6 = no_arg,
|
383 |
const Arg& ptr7 = no_arg,
|
384 |
const Arg& ptr8 = no_arg,
|
385 |
const Arg& ptr9 = no_arg,
|
386 |
const Arg& ptr10 = no_arg,
|
387 |
const Arg& ptr11 = no_arg,
|
388 |
const Arg& ptr12 = no_arg,
|
389 |
const Arg& ptr13 = no_arg,
|
390 |
const Arg& ptr14 = no_arg,
|
391 |
const Arg& ptr15 = no_arg,
|
392 |
const Arg& ptr16 = no_arg) const;
|
393 |
|
394 |
bool Replace(const StringPiece& rewrite,
|
395 |
string *str) const;
|
396 |
|
397 |
int GlobalReplace(const StringPiece& rewrite,
|
398 |
string *str) const;
|
399 |
|
400 |
bool Extract(const StringPiece &rewrite,
|
401 |
const StringPiece &text,
|
402 |
string *out) const;
|
403 |
|
404 |
/***** Generic matching interface *****/
|
405 |
|
406 |
// Type of match (TODO: Should be restructured as part of RE_Options)
|
407 |
enum Anchor {
|
408 |
UNANCHORED, // No anchoring
|
409 |
ANCHOR_START, // Anchor at start only
|
410 |
ANCHOR_BOTH // Anchor at start and end
|
411 |
};
|
412 |
|
413 |
// General matching routine. Stores the length of the match in
|
414 |
// "*consumed" if successful.
|
415 |
bool DoMatch(const StringPiece& text,
|
416 |
Anchor anchor,
|
417 |
int* consumed,
|
418 |
const Arg* const* args, int n) const;
|
419 |
|
420 |
// Return the number of capturing subpatterns, or -1 if the
|
421 |
// regexp wasn't valid on construction.
|
422 |
int NumberOfCapturingGroups();
|
423 |
|
424 |
private:
|
425 |
|
426 |
void Init(const char* pattern, const RE_Options* options);
|
427 |
|
428 |
// Match against "text", filling in "vec" (up to "vecsize" * 2/3) with
|
429 |
// pairs of integers for the beginning and end positions of matched
|
430 |
// text. The first pair corresponds to the entire matched text;
|
431 |
// subsequent pairs correspond, in order, to parentheses-captured
|
432 |
// matches. Returns the number of pairs (one more than the number of
|
433 |
// the last subpattern with a match) if matching was successful
|
434 |
// and zero if the match failed.
|
435 |
// I.e. for RE("(foo)|(bar)|(baz)") it will return 2, 3, and 4 when matching
|
436 |
// against "foo", "bar", and "baz" respectively.
|
437 |
// When matching RE("(foo)|hello") against "hello", it will return 1.
|
438 |
// But the values for all subpattern are filled in into "vec".
|
439 |
int TryMatch(const StringPiece& text,
|
440 |
int startpos,
|
441 |
Anchor anchor,
|
442 |
int *vec,
|
443 |
int vecsize) const;
|
444 |
|
445 |
// Append the "rewrite" string, with backslash subsitutions from "text"
|
446 |
// and "vec", to string "out".
|
447 |
bool Rewrite(string *out,
|
448 |
const StringPiece& rewrite,
|
449 |
const StringPiece& text,
|
450 |
int *vec,
|
451 |
int veclen) const;
|
452 |
|
453 |
// internal implementation for DoMatch
|
454 |
bool DoMatchImpl(const StringPiece& text,
|
455 |
Anchor anchor,
|
456 |
int* consumed,
|
457 |
const Arg* const args[],
|
458 |
int n,
|
459 |
int* vec,
|
460 |
int vecsize) const;
|
461 |
|
462 |
// Compile the regexp for the specified anchoring mode
|
463 |
pcre* Compile(Anchor anchor);
|
464 |
|
465 |
string pattern_;
|
466 |
RE_Options options_;
|
467 |
pcre* re_full_; // For full matches
|
468 |
pcre* re_partial_; // For partial matches
|
469 |
const string* error_; // Error indicator (or points to empty string)
|
470 |
int match_limit_; // limit on execution resources
|
471 |
|
472 |
// Don't allow the default copy or assignment constructors --
|
473 |
// they're expensive and too easy to do by accident.
|
474 |
RE(const RE&);
|
475 |
void operator=(const RE&);
|
476 |
};
|
477 |
|
478 |
|
479 |
/***** Implementation details *****/
|
480 |
|
481 |
// Hex/Octal/Binary?
|
482 |
|
483 |
// Special class for parsing into objects that define a ParseFrom() method
|
484 |
template <class T>
|
485 |
class _RE_MatchObject {
|
486 |
public:
|
487 |
static inline bool Parse(const char* str, int n, void* dest) {
|
488 |
T* object = reinterpret_cast<T*>(dest);
|
489 |
return object->ParseFrom(str, n);
|
490 |
}
|
491 |
};
|
492 |
|
493 |
class Arg {
|
494 |
public:
|
495 |
// Empty constructor so we can declare arrays of Arg
|
496 |
Arg();
|
497 |
|
498 |
// Constructor specially designed for NULL arguments
|
499 |
Arg(void*);
|
500 |
|
501 |
typedef bool (*Parser)(const char* str, int n, void* dest);
|
502 |
|
503 |
// Type-specific parsers
|
504 |
#define PCRE_MAKE_PARSER(type,name) \
|
505 |
Arg(type* p) : arg_(p), parser_(name) { } \
|
506 |
Arg(type* p, Parser parser) : arg_(p), parser_(parser) { }
|
507 |
|
508 |
|
509 |
PCRE_MAKE_PARSER(char, parse_char);
|
510 |
PCRE_MAKE_PARSER(unsigned char, parse_uchar);
|
511 |
PCRE_MAKE_PARSER(short, parse_short);
|
512 |
PCRE_MAKE_PARSER(unsigned short, parse_ushort);
|
513 |
PCRE_MAKE_PARSER(int, parse_int);
|
514 |
PCRE_MAKE_PARSER(unsigned int, parse_uint);
|
515 |
PCRE_MAKE_PARSER(long, parse_long);
|
516 |
PCRE_MAKE_PARSER(unsigned long, parse_ulong);
|
517 |
#if @pcre_has_long_long@
|
518 |
PCRE_MAKE_PARSER(long long, parse_longlong);
|
519 |
#endif
|
520 |
#if @pcre_has_ulong_long@
|
521 |
PCRE_MAKE_PARSER(unsigned long long, parse_ulonglong);
|
522 |
#endif
|
523 |
PCRE_MAKE_PARSER(float, parse_float);
|
524 |
PCRE_MAKE_PARSER(double, parse_double);
|
525 |
PCRE_MAKE_PARSER(string, parse_string);
|
526 |
PCRE_MAKE_PARSER(StringPiece, parse_stringpiece);
|
527 |
|
528 |
#undef PCRE_MAKE_PARSER
|
529 |
|
530 |
// Generic constructor
|
531 |
template <class T> Arg(T*, Parser parser);
|
532 |
// Generic constructor template
|
533 |
template <class T> Arg(T* p)
|
534 |
: arg_(p), parser_(_RE_MatchObject<T>::Parse) {
|
535 |
}
|
536 |
|
537 |
// Parse the data
|
538 |
bool Parse(const char* str, int n) const;
|
539 |
|
540 |
private:
|
541 |
void* arg_;
|
542 |
Parser parser_;
|
543 |
|
544 |
static bool parse_null (const char* str, int n, void* dest);
|
545 |
static bool parse_char (const char* str, int n, void* dest);
|
546 |
static bool parse_uchar (const char* str, int n, void* dest);
|
547 |
static bool parse_float (const char* str, int n, void* dest);
|
548 |
static bool parse_double (const char* str, int n, void* dest);
|
549 |
static bool parse_string (const char* str, int n, void* dest);
|
550 |
static bool parse_stringpiece (const char* str, int n, void* dest);
|
551 |
|
552 |
#define PCRE_DECLARE_INTEGER_PARSER(name) \
|
553 |
private: \
|
554 |
static bool parse_ ## name(const char* str, int n, void* dest); \
|
555 |
static bool parse_ ## name ## _radix( \
|
556 |
const char* str, int n, void* dest, int radix); \
|
557 |
public: \
|
558 |
static bool parse_ ## name ## _hex(const char* str, int n, void* dest); \
|
559 |
static bool parse_ ## name ## _octal(const char* str, int n, void* dest); \
|
560 |
static bool parse_ ## name ## _cradix(const char* str, int n, void* dest)
|
561 |
|
562 |
PCRE_DECLARE_INTEGER_PARSER(short);
|
563 |
PCRE_DECLARE_INTEGER_PARSER(ushort);
|
564 |
PCRE_DECLARE_INTEGER_PARSER(int);
|
565 |
PCRE_DECLARE_INTEGER_PARSER(uint);
|
566 |
PCRE_DECLARE_INTEGER_PARSER(long);
|
567 |
PCRE_DECLARE_INTEGER_PARSER(ulong);
|
568 |
PCRE_DECLARE_INTEGER_PARSER(longlong);
|
569 |
PCRE_DECLARE_INTEGER_PARSER(ulonglong);
|
570 |
|
571 |
#undef PCRE_DECLARE_INTEGER_PARSER
|
572 |
};
|
573 |
|
574 |
inline Arg::Arg() : arg_(NULL), parser_(parse_null) { }
|
575 |
inline Arg::Arg(void* p) : arg_(p), parser_(parse_null) { }
|
576 |
|
577 |
inline bool Arg::Parse(const char* str, int n) const {
|
578 |
return (*parser_)(str, n, arg_);
|
579 |
}
|
580 |
|
581 |
// This part of the parser, appropriate only for ints, deals with bases
|
582 |
#define MAKE_INTEGER_PARSER(type, name) \
|
583 |
inline Arg Hex(type* ptr) { \
|
584 |
return Arg(ptr, Arg::parse_ ## name ## _hex); } \
|
585 |
inline Arg Octal(type* ptr) { \
|
586 |
return Arg(ptr, Arg::parse_ ## name ## _octal); } \
|
587 |
inline Arg CRadix(type* ptr) { \
|
588 |
return Arg(ptr, Arg::parse_ ## name ## _cradix); }
|
589 |
|
590 |
MAKE_INTEGER_PARSER(short, short);
|
591 |
MAKE_INTEGER_PARSER(unsigned short, ushort);
|
592 |
MAKE_INTEGER_PARSER(int, int);
|
593 |
MAKE_INTEGER_PARSER(unsigned int, uint);
|
594 |
MAKE_INTEGER_PARSER(long, long);
|
595 |
MAKE_INTEGER_PARSER(unsigned long, ulong);
|
596 |
#if @pcre_has_long_long@
|
597 |
MAKE_INTEGER_PARSER(long long, longlong);
|
598 |
#endif
|
599 |
#if @pcre_has_ulong_long@
|
600 |
MAKE_INTEGER_PARSER(unsigned long long, ulonglong);
|
601 |
#endif
|
602 |
|
603 |
#undef MAKE_INTEGER_PARSER
|
604 |
|
605 |
} // namespace pcrecpp
|
606 |
|
607 |
#endif /* _PCRE_REGEXP_H */
|