1 // Copyright 2009 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 #include <string> 6 #include "util/util.h" 7 #include "re2/filtered_re2.h" 8 #include "re2/prefilter.h" 9 #include "re2/prefilter_tree.h" 10 11 namespace re2 { 12 13 FilteredRE2::FilteredRE2() 14 : compiled_(false), 15 prefilter_tree_(new PrefilterTree()) { 16 } 17 18 FilteredRE2::~FilteredRE2() { 19 for (int i = 0; i < re2_vec_.size(); i++) 20 delete re2_vec_[i]; 21 delete prefilter_tree_; 22 } 23 24 RE2::ErrorCode FilteredRE2::Add(const StringPiece& pattern, 25 const RE2::Options& options, int* id) { 26 RE2* re = new RE2(pattern, options); 27 RE2::ErrorCode code = re->error_code(); 28 29 if (!re->ok()) { 30 LOG(ERROR) << "Couldn't compile regular expression, skipping: " 31 << re << " due to error " << re->error(); 32 delete re; 33 } else { 34 *id = re2_vec_.size(); 35 re2_vec_.push_back(re); 36 } 37 38 return code; 39 } 40 41 void FilteredRE2::Compile(vector<string>* atoms) { 42 if (compiled_ || re2_vec_.size() == 0) { 43 LOG(INFO) << "C: " << compiled_ << " S:" << re2_vec_.size(); 44 return; 45 } 46 47 for (int i = 0; i < re2_vec_.size(); i++) { 48 Prefilter* prefilter = Prefilter::FromRE2(re2_vec_[i]); 49 prefilter_tree_->Add(prefilter); 50 } 51 atoms->clear(); 52 prefilter_tree_->Compile(atoms); 53 compiled_ = true; 54 } 55 56 int FilteredRE2::SlowFirstMatch(const StringPiece& text) const { 57 for (int i = 0; i < re2_vec_.size(); i++) 58 if (RE2::PartialMatch(text, *re2_vec_[i])) 59 return i; 60 return -1; 61 } 62 63 int FilteredRE2::FirstMatch(const StringPiece& text, 64 const vector<int>& atoms) const { 65 if (!compiled_) { 66 LOG(DFATAL) << "FirstMatch called before Compile"; 67 return -1; 68 } 69 vector<int> regexps; 70 prefilter_tree_->RegexpsGivenStrings(atoms, ®exps); 71 for (int i = 0; i < regexps.size(); i++) 72 if (RE2::PartialMatch(text, *re2_vec_[regexps[i]])) 73 return regexps[i]; 74 return -1; 75 } 76 77 bool FilteredRE2::AllMatches( 78 const StringPiece& text, 79 const vector<int>& atoms, 80 vector<int>* matching_regexps) const { 81 matching_regexps->clear(); 82 vector<int> regexps; 83 prefilter_tree_->RegexpsGivenStrings(atoms, ®exps); 84 for (int i = 0; i < regexps.size(); i++) 85 if (RE2::PartialMatch(text, *re2_vec_[regexps[i]])) 86 matching_regexps->push_back(regexps[i]); 87 return !matching_regexps->empty(); 88 } 89 90 void FilteredRE2::RegexpsGivenStrings(const vector<int>& matched_atoms, 91 vector<int>* passed_regexps) { 92 prefilter_tree_->RegexpsGivenStrings(matched_atoms, passed_regexps); 93 } 94 95 96 void FilteredRE2::PrintPrefilter(int regexpid) { 97 prefilter_tree_->PrintPrefilter(regexpid); 98 } 99 100 } // namespace re2 101