| 53 | } |
| 54 | |
| 55 | int RE2::Set::Add(const StringPiece& pattern, std::string* error) { |
| 56 | if (compiled_) { |
| 57 | LOG(DFATAL) << "RE2::Set::Add() called after compiling"; |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | Regexp::ParseFlags pf = static_cast<Regexp::ParseFlags>( |
| 62 | options_.ParseFlags()); |
| 63 | RegexpStatus status; |
| 64 | re2::Regexp* re = Regexp::Parse(pattern, pf, &status); |
| 65 | if (re == NULL) { |
| 66 | if (error != NULL) |
| 67 | *error = status.Text(); |
| 68 | if (options_.log_errors()) |
| 69 | LOG(ERROR) << "Error parsing '" << pattern << "': " << status.Text(); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | // Concatenate with match index and push on vector. |
| 74 | int n = static_cast<int>(elem_.size()); |
| 75 | re2::Regexp* m = re2::Regexp::HaveMatch(n, pf); |
| 76 | if (re->op() == kRegexpConcat) { |
| 77 | int nsub = re->nsub(); |
| 78 | PODArray<re2::Regexp*> sub(nsub + 1); |
| 79 | for (int i = 0; i < nsub; i++) |
| 80 | sub[i] = re->sub()[i]->Incref(); |
| 81 | sub[nsub] = m; |
| 82 | re->Decref(); |
| 83 | re = re2::Regexp::Concat(sub.data(), nsub + 1, pf); |
| 84 | } else { |
| 85 | re2::Regexp* sub[2]; |
| 86 | sub[0] = re; |
| 87 | sub[1] = m; |
| 88 | re = re2::Regexp::Concat(sub, 2, pf); |
| 89 | } |
| 90 | elem_.emplace_back(std::string(pattern), re); |
| 91 | return n; |
| 92 | } |
| 93 | |
| 94 | bool RE2::Set::Compile() { |
| 95 | if (compiled_) { |