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.
5 // Prefilter is the class used to extract string guards from regexps.
6 // Rather than using Prefilter class directly, use FilteredRE2.
9 #ifndef RE2_PREFILTER_H_
10 #define RE2_PREFILTER_H_
12 #include "util/util.h"
21 // Instead of using Prefilter directly, use FilteredRE2; see filtered_re2.h
24 ALL
= 0, // Everything matches
25 NONE
, // Nothing matches
26 ATOM
, // The string atom() must match
27 AND
, // All in subs() must match
28 OR
, // One of subs() must match
31 explicit Prefilter(Op op
);
34 Op
op() { return op_
; }
35 const string
& atom() const { return atom_
; }
36 void set_unique_id(int id
) { unique_id_
= id
; }
37 int unique_id() const { return unique_id_
; }
39 // The children of the Prefilter node.
40 vector
<Prefilter
*>* subs() {
41 CHECK(op_
== AND
|| op_
== OR
);
45 // Set the children vector. Prefilter takes ownership of subs and
46 // subs_ will be deleted when Prefilter is deleted.
47 void set_subs(vector
<Prefilter
*>* subs
) { subs_
= subs
; }
49 // Given a RE2, return a Prefilter. The caller takes ownership of
50 // the Prefilter and should deallocate it. Returns NULL if Prefilter
52 static Prefilter
* FromRE2(const RE2
* re2
);
54 // Returns a readable debug string of the prefilter.
55 string
DebugString() const;
60 // Combines two prefilters together to create an AND. The passed
61 // Prefilters will be part of the returned Prefilter or deleted.
62 static Prefilter
* And(Prefilter
* a
, Prefilter
* b
);
64 // Combines two prefilters together to create an OR. The passed
65 // Prefilters will be part of the returned Prefilter or deleted.
66 static Prefilter
* Or(Prefilter
* a
, Prefilter
* b
);
69 static Prefilter
* AndOr(Op op
, Prefilter
* a
, Prefilter
* b
);
71 static Prefilter
* FromRegexp(Regexp
* a
);
73 static Prefilter
* FromString(const string
& str
);
75 static Prefilter
* OrStrings(set
<string
>* ss
);
77 static Info
* BuildInfo(Regexp
* re
);
79 Prefilter
* Simplify();
84 // Sub-matches for AND or OR Prefilter.
85 vector
<Prefilter
*>* subs_
;
87 // Actual string to match in leaf node.
90 // If different prefilters have the same string atom, or if they are
91 // structurally the same (e.g., OR of same atom strings) they are
92 // considered the same unique nodes. This is the id for each unique
93 // node. This field is populated with a unique id for every node,
94 // and -1 for duplicate nodes.
97 // Used for debugging, helps in tracking memory leaks.
100 DISALLOW_EVIL_CONSTRUCTORS(Prefilter
);
105 #endif // RE2_PREFILTER_H_