re2/re2/set.cc
Go to the documentation of this file.
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.
4 
5 #include "re2/set.h"
6 
7 #include <stddef.h>
8 #include <algorithm>
9 #include <memory>
10 #include <utility>
11 
12 #include "util/util.h"
13 #include "util/logging.h"
14 #include "re2/pod_array.h"
15 #include "re2/prog.h"
16 #include "re2/re2.h"
17 #include "re2/regexp.h"
18 #include "re2/stringpiece.h"
19 
20 namespace re2 {
21 
22 RE2::Set::Set(const RE2::Options& options, RE2::Anchor anchor)
23  : options_(options),
24  anchor_(anchor),
25  compiled_(false),
26  size_(0) {
27  options_.set_never_capture(true); // might unblock some optimisations
28 }
29 
31  for (size_t i = 0; i < elem_.size(); i++)
32  elem_[i].second->Decref();
33 }
34 
36  : options_(other.options_),
37  anchor_(other.anchor_),
38  elem_(std::move(other.elem_)),
39  compiled_(other.compiled_),
40  size_(other.size_),
41  prog_(std::move(other.prog_)) {
42  other.elem_.clear();
43  other.elem_.shrink_to_fit();
44  other.compiled_ = false;
45  other.size_ = 0;
46  other.prog_.reset();
47 }
48 
50  this->~Set();
51  (void) new (this) Set(std::move(other));
52  return *this;
53 }
54 
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>(
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());
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_) {
96  LOG(DFATAL) << "RE2::Set::Compile() called more than once";
97  return false;
98  }
99  compiled_ = true;
100  size_ = static_cast<int>(elem_.size());
101 
102  // Sort the elements by their patterns. This is good enough for now
103  // until we have a Regexp comparison function. (Maybe someday...)
104  std::sort(elem_.begin(), elem_.end(),
105  [](const Elem& a, const Elem& b) -> bool {
106  return a.first < b.first;
107  });
108 
109  PODArray<re2::Regexp*> sub(size_);
110  for (int i = 0; i < size_; i++)
111  sub[i] = elem_[i].second;
112  elem_.clear();
113  elem_.shrink_to_fit();
114 
115  Regexp::ParseFlags pf = static_cast<Regexp::ParseFlags>(
116  options_.ParseFlags());
117  re2::Regexp* re = re2::Regexp::Alternate(sub.data(), size_, pf);
118 
119  prog_.reset(Prog::CompileSet(re, anchor_, options_.max_mem()));
120  re->Decref();
121  return prog_ != nullptr;
122 }
123 
124 bool RE2::Set::Match(const StringPiece& text, std::vector<int>* v) const {
125  return Match(text, v, NULL);
126 }
127 
128 bool RE2::Set::Match(const StringPiece& text, std::vector<int>* v,
129  ErrorInfo* error_info) const {
130  if (!compiled_) {
131  LOG(DFATAL) << "RE2::Set::Match() called before compiling";
132  if (error_info != NULL)
133  error_info->kind = kNotCompiled;
134  return false;
135  }
136 #ifdef RE2_HAVE_THREAD_LOCAL
137  hooks::context = NULL;
138 #endif
139  bool dfa_failed = false;
140  std::unique_ptr<SparseSet> matches;
141  if (v != NULL) {
142  matches.reset(new SparseSet(size_));
143  v->clear();
144  }
146  NULL, &dfa_failed, matches.get());
147  if (dfa_failed) {
148  if (options_.log_errors())
149  LOG(ERROR) << "DFA out of memory: "
150  << "program size " << prog_->size() << ", "
151  << "list count " << prog_->list_count() << ", "
152  << "bytemap range " << prog_->bytemap_range();
153  if (error_info != NULL)
154  error_info->kind = kOutOfMemory;
155  return false;
156  }
157  if (ret == false) {
158  if (error_info != NULL)
159  error_info->kind = kNoError;
160  return false;
161  }
162  if (v != NULL) {
163  if (matches->empty()) {
164  LOG(DFATAL) << "RE2::Set::Match() matched, but no matches returned?!";
165  if (error_info != NULL)
166  error_info->kind = kInconsistent;
167  return false;
168  }
169  v->assign(matches->begin(), matches->end());
170  }
171  if (error_info != NULL)
172  error_info->kind = kNoError;
173  return true;
174 }
175 
176 } // namespace re2
re2::SparseSet
SparseSetT< void > SparseSet
Definition: bloaty/third_party/re2/util/sparse_set.h:260
re2::RE2::Set
Definition: bloaty/third_party/re2/re2/set.h:23
re2::Prog::list_count
int list_count()
Definition: bloaty/third_party/re2/re2/prog.h:207
re2::Regexp::Decref
void Decref()
Definition: bloaty/third_party/re2/re2/regexp.cc:115
re2::RE2::options_
Options options_
Definition: bloaty/third_party/re2/re2/re2.h:741
false
#define false
Definition: setup_once.h:323
options
double_dict options[]
Definition: capstone_test.c:55
re2::Regexp
Definition: bloaty/third_party/re2/re2/regexp.h:274
re2::Regexp::nsub
int nsub()
Definition: bloaty/third_party/re2/re2/regexp.h:322
re2::RE2::Set::Match
bool Match(const StringPiece &text, std::vector< int > *v) const
Definition: bloaty/third_party/re2/re2/set.cc:105
re2::RE2::Options::ParseFlags
int ParseFlags() const
Definition: bloaty/third_party/re2/re2/re2.cc:123
re2::RE2::Set::operator=
Set & operator=(const Set &)=delete
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
re2::Prog::size_
int size_
Definition: bloaty/third_party/re2/re2/prog.h:403
pod_array.h
status
absl::Status status
Definition: rls.cc:251
re2::RE2::error
const std::string & error() const
Definition: bloaty/third_party/re2/re2/re2.h:275
re2
Definition: bloaty/third_party/re2/re2/bitmap256.h:17
re2::RegexpStatus
Definition: bloaty/third_party/re2/re2/regexp.h:190
re2::Regexp::Concat
static Regexp * Concat(Regexp **subs, int nsubs, ParseFlags flags)
Definition: bloaty/third_party/re2/re2/regexp.cc:286
re2::Prog::bytemap_range
int bytemap_range()
Definition: bloaty/third_party/re2/re2/prog.h:218
second
StrT second
Definition: cxa_demangle.cpp:4885
re2::RE2::Options::log_errors
bool log_errors() const
Definition: bloaty/third_party/re2/re2/re2.h:646
gen_server_registered_method_bad_client_test_body.text
def text
Definition: gen_server_registered_method_bad_client_test_body.py:50
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
re2::kRegexpConcat
@ kRegexpConcat
Definition: bloaty/third_party/re2/re2/regexp.h:116
LOG
#define LOG(severity)
Definition: bloaty/third_party/re2/util/logging.h:53
re2::hooks::context
const thread_local RE2 * context
Definition: istio_echo_server_lib.cc:61
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
re2::RE2::Set::Set
Set(const RE2::Options &options, RE2::Anchor anchor)
Definition: bloaty/third_party/re2/re2/set.cc:21
re2::Regexp::Parse
static Regexp * Parse(const StringPiece &s, ParseFlags flags, RegexpStatus *status)
Definition: bloaty/third_party/re2/re2/parse.cc:2200
re2::Regexp::sub
Regexp ** sub()
Definition: bloaty/third_party/re2/re2/regexp.h:327
re2::Prog::kAnchored
@ kAnchored
Definition: bloaty/third_party/re2/re2/prog.h:177
re2::Prog::size
int size()
Definition: bloaty/third_party/re2/re2/prog.h:204
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
re2::Prog::kManyMatch
@ kManyMatch
Definition: bloaty/third_party/re2/re2/prog.h:196
re2::RE2::Options::set_never_capture
void set_never_capture(bool b)
Definition: bloaty/third_party/re2/re2/re2.h:662
re2::Regexp::ParseFlags
ParseFlags
Definition: bloaty/third_party/re2/re2/regexp.h:278
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
google::protobuf::ERROR
static const LogLevel ERROR
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/googletest.h:70
re2::Prog::SearchDFA
bool SearchDFA(const StringPiece &text, const StringPiece &context, Anchor anchor, MatchKind kind, StringPiece *match0, bool *failed, SparseSet *matches)
Definition: bloaty/third_party/re2/re2/dfa.cc:1861
re2::RE2::Match
bool Match(const StringPiece &text, size_t startpos, size_t endpos, Anchor re_anchor, StringPiece *submatch, int nsubmatch) const
Definition: bloaty/third_party/re2/re2/re2.cc:572
re2::Regexp::op
RegexpOp op()
Definition: bloaty/third_party/re2/re2/regexp.h:321
re2::RE2::Set::options_
RE2::Options options_
Definition: bloaty/third_party/re2/re2/set.h:67
re2::RE2::Set::Compile
bool Compile()
Definition: bloaty/third_party/re2/re2/set.cc:75
size_
size_t size_
Definition: memory_allocator.cc:56
re2::RE2::Set::~Set
~Set()
Definition: bloaty/third_party/re2/re2/set.cc:30
options_
DebugStringOptions options_
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:2390
re2::Regexp::Incref
Regexp * Incref()
Definition: bloaty/third_party/re2/re2/regexp.cc:89
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
re2::RE2::pattern
const std::string & pattern() const
Definition: bloaty/third_party/re2/re2/re2.h:271
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
re2::Prog::CompileSet
static Prog * CompileSet(Regexp *re, RE2::Anchor anchor, int64_t max_mem)
Definition: bloaty/third_party/re2/re2/compile.cc:1275
re2::Regexp::HaveMatch
static Regexp * HaveMatch(int match_id, ParseFlags flags)
Definition: bloaty/third_party/re2/re2/regexp.cc:187
re2::RE2::Options::max_mem
int64_t max_mem() const
Definition: bloaty/third_party/re2/re2/re2.h:649
re2::RE2::Set::Add
int Add(const StringPiece &pattern, std::string *error)
Definition: bloaty/third_party/re2/re2/set.cc:36
re2::RE2::Anchor
Anchor
Definition: bloaty/third_party/re2/re2/re2.h:472
regress.m
m
Definition: regress/regress.py:25
re2::StringPiece
Definition: bloaty/third_party/re2/re2/stringpiece.h:39
re2::RE2::prog_
re2::Prog * prog_
Definition: bloaty/third_party/re2/re2/re2.h:746
re2::Regexp::Alternate
static Regexp * Alternate(Regexp **subs, int nsubs, ParseFlags flags)
Definition: bloaty/third_party/re2/re2/regexp.cc:290
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:12