Skip to content

Commit

Permalink
Improve RE2::Set and FilteredRE2 move semantics.
Browse files Browse the repository at this point in the history
Change-Id: I59ad2893e2900a238edf7ecfcf68ca7473215e09
Reviewed-on: https://code-review.googlesource.com/c/re2/+/57430
Reviewed-by: Paul Wankadia <[email protected]>
  • Loading branch information
junyer committed Jun 18, 2020
1 parent e6613e9 commit f294a9f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
18 changes: 16 additions & 2 deletions re2/filtered_re2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <stddef.h>
#include <string>
#include <utility>

#include "util/util.h"
#include "util/logging.h"
Expand All @@ -29,8 +30,21 @@ FilteredRE2::~FilteredRE2() {
delete re2_vec_[i];
}

FilteredRE2::FilteredRE2(FilteredRE2&&) = default;
FilteredRE2& FilteredRE2::operator=(FilteredRE2&&) = default;
FilteredRE2::FilteredRE2(FilteredRE2&& other)
: re2_vec_(std::move(other.re2_vec_)),
compiled_(other.compiled_),
prefilter_tree_(std::move(other.prefilter_tree_)) {
other.re2_vec_.clear();
other.re2_vec_.shrink_to_fit();
other.compiled_ = false;
other.prefilter_tree_.reset(new PrefilterTree());
}

FilteredRE2& FilteredRE2::operator=(FilteredRE2&& other) {
this->~FilteredRE2();
(void) new (this) FilteredRE2(std::move(other));
return *this;
}

RE2::ErrorCode FilteredRE2::Add(const StringPiece& pattern,
const RE2::Options& options, int* id) {
Expand Down
4 changes: 2 additions & 2 deletions re2/filtered_re2.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class FilteredRE2 {
FilteredRE2(const FilteredRE2&) = delete;
FilteredRE2& operator=(const FilteredRE2&) = delete;
// Movable.
FilteredRE2(FilteredRE2&&);
FilteredRE2& operator=(FilteredRE2&&);
FilteredRE2(FilteredRE2&& other);
FilteredRE2& operator=(FilteredRE2&& other);

// Uses RE2 constructor to create a RE2 object (re). Returns
// re->error_code(). If error_code is other than NoError, then re is
Expand Down
22 changes: 20 additions & 2 deletions re2/set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <stddef.h>
#include <algorithm>
#include <memory>
#include <utility>

#include "util/util.h"
#include "util/logging.h"
Expand All @@ -31,8 +32,25 @@ RE2::Set::~Set() {
elem_[i].second->Decref();
}

RE2::Set::Set(Set&&) = default;
RE2::Set& RE2::Set::operator=(Set&&) = default;
RE2::Set::Set(Set&& other)
: options_(other.options_),
anchor_(other.anchor_),
elem_(std::move(other.elem_)),
compiled_(other.compiled_),
size_(other.size_),
prog_(std::move(other.prog_)) {
other.elem_.clear();
other.elem_.shrink_to_fit();
other.compiled_ = false;
other.size_ = 0;
other.prog_.reset();
}

RE2::Set& RE2::Set::operator=(Set&& other) {
this->~Set();
(void) new (this) Set(std::move(other));
return *this;
}

int RE2::Set::Add(const StringPiece& pattern, std::string* error) {
if (compiled_) {
Expand Down
4 changes: 2 additions & 2 deletions re2/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class RE2::Set {
Set(const Set&) = delete;
Set& operator=(const Set&) = delete;
// Movable.
Set(Set&&);
Set& operator=(Set&&);
Set(Set&& other);
Set& operator=(Set&& other);

// 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(),
Expand Down

0 comments on commit f294a9f

Please sign in to comment.