pcrecpp.h 26 KB

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