Make RE2::Set and FilteredRE2 movable.
Change-Id: I2f5d431f59fc4ba015ada3a19ba40e7e4f4df2d3
Reviewed-on: https://code-review.googlesource.com/c/re2/+/57410
Reviewed-by: Paul Wankadia <junyer@google.com>
diff --git a/re2/filtered_re2.cc b/re2/filtered_re2.cc
index e5d8de5..79b15b6 100644
--- a/re2/filtered_re2.cc
+++ b/re2/filtered_re2.cc
@@ -27,9 +27,11 @@
FilteredRE2::~FilteredRE2() {
for (size_t i = 0; i < re2_vec_.size(); i++)
delete re2_vec_[i];
- delete prefilter_tree_;
}
+FilteredRE2::FilteredRE2(FilteredRE2&&) = default;
+FilteredRE2& FilteredRE2::operator=(FilteredRE2&&) = default;
+
RE2::ErrorCode FilteredRE2::Add(const StringPiece& pattern,
const RE2::Options& options, int* id) {
RE2* re = new RE2(pattern, options);
@@ -38,7 +40,7 @@
if (!re->ok()) {
if (options.log_errors()) {
LOG(ERROR) << "Couldn't compile regular expression, skipping: "
- << re << " due to error " << re->error();
+ << pattern << " due to error " << re->error();
}
delete re;
} else {
diff --git a/re2/filtered_re2.h b/re2/filtered_re2.h
index 86fa586..fbf44e4 100644
--- a/re2/filtered_re2.h
+++ b/re2/filtered_re2.h
@@ -21,6 +21,7 @@
// or AllMatches with a vector of indices of strings that were found
// in the text to get the actual regexp matches.
+#include <memory>
#include <string>
#include <vector>
@@ -36,12 +37,19 @@
explicit FilteredRE2(int min_atom_len);
~FilteredRE2();
+ // Not copyable.
+ FilteredRE2(const FilteredRE2&) = delete;
+ FilteredRE2& operator=(const FilteredRE2&) = delete;
+ // Movable.
+ FilteredRE2(FilteredRE2&&);
+ FilteredRE2& operator=(FilteredRE2&&);
+
// Uses RE2 constructor to create a RE2 object (re). Returns
// re->error_code(). If error_code is other than NoError, then re is
// deleted and not added to re2_vec_.
RE2::ErrorCode Add(const StringPiece& pattern,
const RE2::Options& options,
- int *id);
+ int* id);
// Prepares the regexps added by Add for filtering. Returns a set
// of strings that the caller should check for in candidate texts.
@@ -98,10 +106,7 @@
bool compiled_;
// An AND-OR tree of string atoms used for filtering regexps.
- PrefilterTree* prefilter_tree_;
-
- FilteredRE2(const FilteredRE2&) = delete;
- FilteredRE2& operator=(const FilteredRE2&) = delete;
+ std::unique_ptr<PrefilterTree> prefilter_tree_;
};
} // namespace re2
diff --git a/re2/set.cc b/re2/set.cc
index 87db6b7..056e736 100644
--- a/re2/set.cc
+++ b/re2/set.cc
@@ -18,21 +18,22 @@
namespace re2 {
-RE2::Set::Set(const RE2::Options& options, RE2::Anchor anchor) {
- options_.Copy(options);
+RE2::Set::Set(const RE2::Options& options, RE2::Anchor anchor)
+ : options_(options),
+ anchor_(anchor),
+ compiled_(false),
+ size_(0) {
options_.set_never_capture(true); // might unblock some optimisations
- anchor_ = anchor;
- prog_ = NULL;
- compiled_ = false;
- size_ = 0;
}
RE2::Set::~Set() {
for (size_t i = 0; i < elem_.size(); i++)
elem_[i].second->Decref();
- delete prog_;
}
+RE2::Set::Set(Set&&) = default;
+RE2::Set& RE2::Set::operator=(Set&&) = default;
+
int RE2::Set::Add(const StringPiece& pattern, std::string* error) {
if (compiled_) {
LOG(DFATAL) << "RE2::Set::Add() called after compiling";
@@ -97,9 +98,9 @@
options_.ParseFlags());
re2::Regexp* re = re2::Regexp::Alternate(sub.data(), size_, pf);
- prog_ = Prog::CompileSet(re, anchor_, options_.max_mem());
+ prog_.reset(Prog::CompileSet(re, anchor_, options_.max_mem()));
re->Decref();
- return prog_ != NULL;
+ return prog_ != nullptr;
}
bool RE2::Set::Match(const StringPiece& text, std::vector<int>* v) const {
diff --git a/re2/set.h b/re2/set.h
index 59733fd..e81e70f 100644
--- a/re2/set.h
+++ b/re2/set.h
@@ -5,6 +5,7 @@
#ifndef RE2_SET_H_
#define RE2_SET_H_
+#include <memory>
#include <string>
#include <utility>
#include <vector>
@@ -36,6 +37,13 @@
Set(const RE2::Options& options, RE2::Anchor anchor);
~Set();
+ // Not copyable.
+ Set(const Set&) = delete;
+ Set& operator=(const Set&) = delete;
+ // Movable.
+ Set(Set&&);
+ Set& operator=(Set&&);
+
// Adds pattern to the set using the options passed to the constructor.
// Returns the index that will identify the regexp in the output of Match(),
// or -1 if the regexp cannot be parsed.
@@ -67,12 +75,9 @@
RE2::Options options_;
RE2::Anchor anchor_;
std::vector<Elem> elem_;
- re2::Prog* prog_;
bool compiled_;
int size_;
-
- Set(const Set&) = delete;
- Set& operator=(const Set&) = delete;
+ std::unique_ptr<re2::Prog> prog_;
};
} // namespace re2