Russ Cox | c334dcc | 2010-07-15 18:48:48 -0700 | [diff] [blame] | 1 | // Copyright 2010 The RE2 Authors. All Rights Reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Paul Wankadia | 89a5907 | 2016-07-26 21:09:42 +1000 | [diff] [blame] | 5 | #ifndef RE2_SET_H_ |
| 6 | #define RE2_SET_H_ |
Russ Cox | c334dcc | 2010-07-15 18:48:48 -0700 | [diff] [blame] | 7 | |
Paul Wankadia | 0029946 | 2016-08-02 20:26:18 +1000 | [diff] [blame] | 8 | #include <string> |
Paul Wankadia | ed5be2c | 2017-10-11 21:23:13 +1100 | [diff] [blame] | 9 | #include <utility> |
Russ Cox | c334dcc | 2010-07-15 18:48:48 -0700 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
| 12 | #include "re2/re2.h" |
| 13 | |
| 14 | namespace re2 { |
Paul Wankadia | 998584d | 2016-08-02 16:54:50 +1000 | [diff] [blame] | 15 | class Prog; |
| 16 | class Regexp; |
| 17 | } // namespace re2 |
| 18 | |
| 19 | namespace re2 { |
Russ Cox | c334dcc | 2010-07-15 18:48:48 -0700 | [diff] [blame] | 20 | |
| 21 | // An RE2::Set represents a collection of regexps that can |
| 22 | // be searched for simultaneously. |
| 23 | class RE2::Set { |
| 24 | public: |
Paul Wankadia | ee52f03 | 2017-11-16 20:57:41 +1100 | [diff] [blame] | 25 | enum ErrorKind { |
| 26 | kNoError = 0, |
| 27 | kNotCompiled, // The set is not compiled. |
| 28 | kOutOfMemory, // The DFA ran out of memory. |
| 29 | kInconsistent, // The result is inconsistent. This should never happen. |
| 30 | }; |
| 31 | |
| 32 | struct ErrorInfo { |
| 33 | ErrorKind kind; |
| 34 | }; |
| 35 | |
Russ Cox | c334dcc | 2010-07-15 18:48:48 -0700 | [diff] [blame] | 36 | Set(const RE2::Options& options, RE2::Anchor anchor); |
| 37 | ~Set(); |
| 38 | |
Paul Wankadia | ee52f03 | 2017-11-16 20:57:41 +1100 | [diff] [blame] | 39 | // Adds pattern to the set using the options passed to the constructor. |
| 40 | // Returns the index that will identify the regexp in the output of Match(), |
| 41 | // or -1 if the regexp cannot be parsed. |
Russ Cox | c334dcc | 2010-07-15 18:48:48 -0700 | [diff] [blame] | 42 | // Indices are assigned in sequential order starting from 0. |
Paul Wankadia | ee52f03 | 2017-11-16 20:57:41 +1100 | [diff] [blame] | 43 | // Errors do not increment the index; if error is not NULL, *error will hold |
| 44 | // the error message from the parser. |
Russ Cox | c334dcc | 2010-07-15 18:48:48 -0700 | [diff] [blame] | 45 | int Add(const StringPiece& pattern, string* error); |
| 46 | |
Paul Wankadia | ee52f03 | 2017-11-16 20:57:41 +1100 | [diff] [blame] | 47 | // Compiles the set in preparation for matching. |
| 48 | // Returns false if the compiler runs out of memory. |
| 49 | // Add() must not be called again after Compile(). |
| 50 | // Compile() must be called before Match(). |
Russ Cox | c334dcc | 2010-07-15 18:48:48 -0700 | [diff] [blame] | 51 | bool Compile(); |
| 52 | |
Paul Wankadia | ee52f03 | 2017-11-16 20:57:41 +1100 | [diff] [blame] | 53 | // Returns true if text matches at least one of the regexps in the set. |
| 54 | // Fills v (if not NULL) with the indices of the matching regexps. |
Paul Wankadia | 4bb66d4 | 2016-11-22 00:10:13 +1100 | [diff] [blame] | 55 | // Callers must not expect v to be sorted. |
Paul Wankadia | ee55a8f | 2016-08-02 21:49:57 +1000 | [diff] [blame] | 56 | bool Match(const StringPiece& text, std::vector<int>* v) const; |
Russ Cox | c334dcc | 2010-07-15 18:48:48 -0700 | [diff] [blame] | 57 | |
Paul Wankadia | ee52f03 | 2017-11-16 20:57:41 +1100 | [diff] [blame] | 58 | // As above, but populates error_info (if not NULL) when none of the regexps |
| 59 | // in the set matched. This can inform callers when DFA execution fails, for |
| 60 | // example, because they might wish to handle that case differently. |
| 61 | bool Match(const StringPiece& text, std::vector<int>* v, |
| 62 | ErrorInfo* error_info) const; |
| 63 | |
Russ Cox | c334dcc | 2010-07-15 18:48:48 -0700 | [diff] [blame] | 64 | private: |
Paul Wankadia | ed5be2c | 2017-10-11 21:23:13 +1100 | [diff] [blame] | 65 | typedef std::pair<string, re2::Regexp*> Elem; |
| 66 | |
Russ Cox | c334dcc | 2010-07-15 18:48:48 -0700 | [diff] [blame] | 67 | RE2::Options options_; |
| 68 | RE2::Anchor anchor_; |
Paul Wankadia | ed5be2c | 2017-10-11 21:23:13 +1100 | [diff] [blame] | 69 | std::vector<Elem> elem_; |
Russ Cox | c334dcc | 2010-07-15 18:48:48 -0700 | [diff] [blame] | 70 | re2::Prog* prog_; |
| 71 | bool compiled_; |
Paul Wankadia | 295316b | 2017-10-04 00:33:16 +1100 | [diff] [blame] | 72 | int size_; |
Paul Wankadia | f408be0 | 2016-08-16 23:42:11 +1000 | [diff] [blame] | 73 | |
| 74 | Set(const Set&) = delete; |
| 75 | Set& operator=(const Set&) = delete; |
Russ Cox | c334dcc | 2010-07-15 18:48:48 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace re2 |
| 79 | |
Paul Wankadia | 89a5907 | 2016-07-26 21:09:42 +1000 | [diff] [blame] | 80 | #endif // RE2_SET_H_ |