1// Copyright 2009 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#ifndef RE2_PREFILTER_H_
6#define RE2_PREFILTER_H_
7
8// Prefilter is the class used to extract string guards from regexps.
9// Rather than using Prefilter class directly, use FilteredRE2.
10// See filtered_re2.h
11
12#include <set>
13#include <string>
14#include <vector>
15
16#include "util/logging.h"
17
18namespace re2 {
19
20class RE2;
21
22class Regexp;
23
24class Prefilter {
25 // Instead of using Prefilter directly, use FilteredRE2; see filtered_re2.h
26 public:
27 enum Op {
28 ALL = 0, // Everything matches
29 NONE, // Nothing matches
30 ATOM, // The string atom() must match
31 AND, // All in subs() must match
32 OR, // One of subs() must match
33 };
34
35 explicit Prefilter(Op op);
36 ~Prefilter();
37
38 Op op() { return op_; }
39 const std::string& atom() const { return atom_; }
40 void set_unique_id(int id) { unique_id_ = id; }
41 int unique_id() const { return unique_id_; }
42
43 // The children of the Prefilter node.
44 std::vector<Prefilter*>* subs() {
45 DCHECK(op_ == AND || op_ == OR);
46 return subs_;
47 }
48
49 // Set the children vector. Prefilter takes ownership of subs and
50 // subs_ will be deleted when Prefilter is deleted.
51 void set_subs(std::vector<Prefilter*>* subs) { subs_ = subs; }
52
53 // Given a RE2, return a Prefilter. The caller takes ownership of
54 // the Prefilter and should deallocate it. Returns NULL if Prefilter
55 // cannot be formed.
56 static Prefilter* FromRE2(const RE2* re2);
57
58 // Returns a readable debug string of the prefilter.
59 std::string DebugString() const;
60
61 private:
62 class Info;
63
64 // Combines two prefilters together to create an AND. The passed
65 // Prefilters will be part of the returned Prefilter or deleted.
66 static Prefilter* And(Prefilter* a, Prefilter* b);
67
68 // Combines two prefilters together to create an OR. The passed
69 // Prefilters will be part of the returned Prefilter or deleted.
70 static Prefilter* Or(Prefilter* a, Prefilter* b);
71
72 // Generalized And/Or
73 static Prefilter* AndOr(Op op, Prefilter* a, Prefilter* b);
74
75 static Prefilter* FromRegexp(Regexp* a);
76
77 static Prefilter* FromString(const std::string& str);
78
79 static Prefilter* OrStrings(std::set<std::string>* ss);
80
81 static Info* BuildInfo(Regexp* re);
82
83 Prefilter* Simplify();
84
85 // Kind of Prefilter.
86 Op op_;
87
88 // Sub-matches for AND or OR Prefilter.
89 std::vector<Prefilter*>* subs_;
90
91 // Actual string to match in leaf node.
92 std::string atom_;
93
94 // If different prefilters have the same string atom, or if they are
95 // structurally the same (e.g., OR of same atom strings) they are
96 // considered the same unique nodes. This is the id for each unique
97 // node. This field is populated with a unique id for every node,
98 // and -1 for duplicate nodes.
99 int unique_id_;
100
101 Prefilter(const Prefilter&) = delete;
102 Prefilter& operator=(const Prefilter&) = delete;
103};
104
105} // namespace re2
106
107#endif // RE2_PREFILTER_H_
108