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.
8 #include "re2/stringpiece.h"
11 #include "re2/regexp.h"
15 RE2::Set::Set(const RE2::Options& options, RE2::Anchor anchor) {
16 options_.Copy(options);
23 for (int i = 0; i < re_.size(); i++)
28 int RE2::Set::Add(const StringPiece& pattern, string* error) {
30 LOG(DFATAL) << "RE2::Set::Add after Compile";
34 Regexp::ParseFlags pf = static_cast<Regexp::ParseFlags>(
35 options_.ParseFlags());
38 re2::Regexp* re = Regexp::Parse(pattern, pf, &status);
41 *error = status.Text();
42 if (options_.log_errors())
43 LOG(ERROR) << "Error parsing '" << pattern << "': " << status.Text();
47 // Concatenate with match index and push on vector.
49 re2::Regexp* m = re2::Regexp::HaveMatch(n, pf);
50 if (re->op() == kRegexpConcat) {
51 int nsub = re->nsub();
52 re2::Regexp** sub = new re2::Regexp*[nsub + 1];
53 for (int i = 0; i < nsub; i++)
54 sub[i] = re->sub()[i]->Incref();
57 re = re2::Regexp::Concat(sub, nsub + 1, pf);
63 re = re2::Regexp::Concat(sub, 2, pf);
69 bool RE2::Set::Compile() {
71 LOG(DFATAL) << "RE2::Set::Compile multiple times";
76 Regexp::ParseFlags pf = static_cast<Regexp::ParseFlags>(
77 options_.ParseFlags());
78 re2::Regexp* re = re2::Regexp::Alternate(const_cast<re2::Regexp**>(&re_[0]),
81 re2::Regexp* sre = re->Simplify();
85 if (options_.log_errors())
86 LOG(ERROR) << "Error simplifying during Compile.";
90 prog_ = Prog::CompileSet(options_, anchor_, re);
94 bool RE2::Set::Match(const StringPiece& text, vector<int>* v) const {
96 LOG(DFATAL) << "RE2::Set::Match without Compile";
101 bool ret = prog_->SearchDFA(text, text, Prog::kAnchored,
102 Prog::kManyMatch, NULL, &failed, v);
104 LOG(DFATAL) << "RE2::Set::Match: DFA ran out of cache space";
108 if (v->size() == 0) {
109 LOG(DFATAL) << "RE2::Set::Match: match but unknown regexp set";