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 5 #ifndef RE2_SET_H_ 6 #define RE2_SET_H_ 7 8 #include <memory> 9 #include <string> 10 #include <utility> 11 #include <vector> 12 13 #include "absl/strings/string_view.h" 14 #include "re2/re2.h" 15 16 namespace re2 { 17 class Prog; 18 class Regexp; 19 } // namespace re2 20 21 namespace re2 { 22 23 // An RE2::Set represents a collection of regexps that can 24 // be searched for simultaneously. 25 class RE2::Set { 26 public: 27 enum ErrorKind { 28 kNoError = 0, 29 kNotCompiled, // The set is not compiled. 30 kOutOfMemory, // The DFA ran out of memory. 31 kInconsistent, // The result is inconsistent. This should never happen. 32 }; 33 34 struct ErrorInfo { 35 ErrorKind kind; 36 }; 37 38 Set(const RE2::Options& options, RE2::Anchor anchor); 39 ~Set(); 40 41 // Not copyable. 42 Set(const Set&) = delete; 43 Set& operator=(const Set&) = delete; 44 // Movable. 45 Set(Set&& other); 46 Set& operator=(Set&& other); 47 48 // Adds pattern to the set using the options passed to the constructor. 49 // Returns the index that will identify the regexp in the output of Match(), 50 // or -1 if the regexp cannot be parsed. 51 // Indices are assigned in sequential order starting from 0. 52 // Errors do not increment the index; if error is not NULL, *error will hold 53 // the error message from the parser. 54 int Add(absl::string_view pattern, std::string* error); 55 56 // Compiles the set in preparation for matching. 57 // Returns false if the compiler runs out of memory. 58 // Add() must not be called again after Compile(). 59 // Compile() must be called before Match(). 60 bool Compile(); 61 62 // Returns true if text matches at least one of the regexps in the set. 63 // Fills v (if not NULL) with the indices of the matching regexps. 64 // Callers must not expect v to be sorted. 65 bool Match(absl::string_view text, std::vector<int>* v) const; 66 67 // As above, but populates error_info (if not NULL) when none of the regexps 68 // in the set matched. This can inform callers when DFA execution fails, for 69 // example, because they might wish to handle that case differently. 70 bool Match(absl::string_view text, std::vector<int>* v, 71 ErrorInfo* error_info) const; 72 73 private: 74 typedef std::pair<std::string, re2::Regexp*> Elem; 75 76 RE2::Options options_; 77 RE2::Anchor anchor_; 78 std::vector<Elem> elem_; 79 bool compiled_; 80 int size_; 81 std::unique_ptr<re2::Prog> prog_; 82 }; 83 84 } // namespace re2 85 86 #endif // RE2_SET_H_ 87