bloaty/third_party/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 
11 #include "util/util.h"
12 #include "util/logging.h"
13 #include "util/pod_array.h"
14 #include "re2/stringpiece.h"
15 #include "re2/prog.h"
16 #include "re2/re2.h"
17 #include "re2/regexp.h"
18 
19 namespace re2 {
20 
23  options_.set_never_capture(true); // might unblock some optimisations
24  anchor_ = anchor;
25  prog_ = NULL;
26  compiled_ = false;
27  size_ = 0;
28 }
29 
31  for (size_t i = 0; i < elem_.size(); i++)
32  elem_[i].second->Decref();
33  delete prog_;
34 }
35 
37  if (compiled_) {
38  LOG(DFATAL) << "RE2::Set::Add() called after compiling";
39  return -1;
40  }
41 
42  Regexp::ParseFlags pf = static_cast<Regexp::ParseFlags>(
46  if (re == NULL) {
47  if (error != NULL)
48  *error = status.Text();
49  if (options_.log_errors())
50  LOG(ERROR) << "Error parsing '" << pattern << "': " << status.Text();
51  return -1;
52  }
53 
54  // Concatenate with match index and push on vector.
55  int n = static_cast<int>(elem_.size());
57  if (re->op() == kRegexpConcat) {
58  int nsub = re->nsub();
59  PODArray<re2::Regexp*> sub(nsub + 1);
60  for (int i = 0; i < nsub; i++)
61  sub[i] = re->sub()[i]->Incref();
62  sub[nsub] = m;
63  re->Decref();
64  re = re2::Regexp::Concat(sub.data(), nsub + 1, pf);
65  } else {
66  re2::Regexp* sub[2];
67  sub[0] = re;
68  sub[1] = m;
69  re = re2::Regexp::Concat(sub, 2, pf);
70  }
71  elem_.emplace_back(std::string(pattern), re);
72  return n;
73 }
74 
76  if (compiled_) {
77  LOG(DFATAL) << "RE2::Set::Compile() called more than once";
78  return false;
79  }
80  compiled_ = true;
81  size_ = static_cast<int>(elem_.size());
82 
83  // Sort the elements by their patterns. This is good enough for now
84  // until we have a Regexp comparison function. (Maybe someday...)
85  std::sort(elem_.begin(), elem_.end(),
86  [](const Elem& a, const Elem& b) -> bool {
87  return a.first < b.first;
88  });
89 
91  for (int i = 0; i < size_; i++)
92  sub[i] = elem_[i].second;
93  elem_.clear();
94  elem_.shrink_to_fit();
95 
96  Regexp::ParseFlags pf = static_cast<Regexp::ParseFlags>(
98  re2::Regexp* re = re2::Regexp::Alternate(sub.data(), size_, pf);
99 
100  prog_ = Prog::CompileSet(re, anchor_, options_.max_mem());
101  re->Decref();
102  return prog_ != NULL;
103 }
104 
105 bool RE2::Set::Match(const StringPiece& text, std::vector<int>* v) const {
106  return Match(text, v, NULL);
107 }
108 
109 bool RE2::Set::Match(const StringPiece& text, std::vector<int>* v,
110  ErrorInfo* error_info) const {
111  if (!compiled_) {
112  LOG(DFATAL) << "RE2::Set::Match() called before compiling";
113  if (error_info != NULL)
114  error_info->kind = kNotCompiled;
115  return false;
116  }
117  bool dfa_failed = false;
118  std::unique_ptr<SparseSet> matches;
119  if (v != NULL) {
120  matches.reset(new SparseSet(size_));
121  v->clear();
122  }
124  NULL, &dfa_failed, matches.get());
125  if (dfa_failed) {
126  if (options_.log_errors())
127  LOG(ERROR) << "DFA out of memory: size " << prog_->size() << ", "
128  << "bytemap range " << prog_->bytemap_range() << ", "
129  << "list count " << prog_->list_count();
130  if (error_info != NULL)
131  error_info->kind = kOutOfMemory;
132  return false;
133  }
134  if (ret == false) {
135  if (error_info != NULL)
136  error_info->kind = kNoError;
137  return false;
138  }
139  if (v != NULL) {
140  if (matches->empty()) {
141  LOG(DFATAL) << "RE2::Set::Match() matched, but no matches returned?!";
142  if (error_info != NULL)
143  error_info->kind = kInconsistent;
144  return false;
145  }
146  v->assign(matches->begin(), matches->end());
147  }
148  if (error_info != NULL)
149  error_info->kind = kNoError;
150  return true;
151 }
152 
153 } // namespace re2
re2::SparseSet
SparseSetT< void > SparseSet
Definition: bloaty/third_party/re2/util/sparse_set.h:260
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
pod_array.h
re2::RE2::options_
Options options_
Definition: bloaty/third_party/re2/re2/re2.h:741
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::Set::size_
int size_
Definition: bloaty/third_party/re2/re2/set.h:72
re2::RE2::Options::ParseFlags
int ParseFlags() const
Definition: bloaty/third_party/re2/re2/re2.cc:123
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
re2::PODArray::data
T * data() const
Definition: bloaty/third_party/re2/util/pod_array.h:24
status
absl::Status status
Definition: rls.cc:251
re2::RE2::Set::prog_
re2::Prog * prog_
Definition: bloaty/third_party/re2/re2/set.h:70
re2::RE2::error
const std::string & error() const
Definition: bloaty/third_party/re2/re2/re2.h:275
re2::RE2::Set::Elem
std::pair< std::string, re2::Regexp * > Elem
Definition: bloaty/third_party/re2/re2/set.h:65
re2
Definition: bloaty/third_party/re2/re2/bitmap256.h:17
re2::RegexpStatus
Definition: bloaty/third_party/re2/re2/regexp.h:190
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
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
re2::RE2::Set::compiled_
bool compiled_
Definition: bloaty/third_party/re2/re2/set.h:71
gen_server_registered_method_bad_client_test_body.text
def text
Definition: gen_server_registered_method_bad_client_test_body.py:50
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::RE2::options
const Options & options() const
Definition: bloaty/third_party/re2/re2/re2.h:699
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::anchor_
RE2::Anchor anchor_
Definition: bloaty/third_party/re2/re2/set.h:68
re2::RE2::Options
Definition: bloaty/third_party/re2/re2/re2.h:548
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
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
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::ErrorInfo
Definition: bloaty/third_party/re2/re2/set.h:32
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::RE2::Set::ErrorInfo::kind
ErrorKind kind
Definition: bloaty/third_party/re2/re2/set.h:33
re2::StringPiece
Definition: bloaty/third_party/re2/re2/stringpiece.h:39
re2::PODArray
Definition: bloaty/third_party/re2/util/pod_array.h:14
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
re2::RE2::Options::Copy
void Copy(const Options &src)
Definition: bloaty/third_party/re2/re2/re2.h:676


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