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#ifndef RE2_SET_H_
6#define RE2_SET_H_
7
8#include <string>
9#include <utility>
10#include <vector>
11
12#include "re2/re2.h"
13
14namespace re2 {
15class Prog;
16class Regexp;
17} // namespace re2
18
19namespace re2 {
20
21// An RE2::Set represents a collection of regexps that can
22// be searched for simultaneously.
23class RE2::Set {
24 public:
25 enum ErrorKind {
26 kNoError = 0,
27 kNotCompiled, // The set is not compiled.
28 kOutOfMemory, // The DFA ran out of memory.
29 kInconsistent, // The result is inconsistent. This should never happen.
30 };
31
32 struct ErrorInfo {
33 ErrorKind kind;
34 };
35
36 Set(const RE2::Options& options, RE2::Anchor anchor);
37 ~Set();
38
39 // Adds pattern to the set using the options passed to the constructor.
40 // Returns the index that will identify the regexp in the output of Match(),
41 // or -1 if the regexp cannot be parsed.
42 // Indices are assigned in sequential order starting from 0.
43 // Errors do not increment the index; if error is not NULL, *error will hold
44 // the error message from the parser.
45 int Add(const StringPiece& pattern, std::string* error);
46
47 // Compiles the set in preparation for matching.
48 // Returns false if the compiler runs out of memory.
49 // Add() must not be called again after Compile().
50 // Compile() must be called before Match().
51 bool Compile();
52
53 // Returns true if text matches at least one of the regexps in the set.
54 // Fills v (if not NULL) with the indices of the matching regexps.
55 // Callers must not expect v to be sorted.
56 bool Match(const StringPiece& text, std::vector<int>* v) const;
57
58 // As above, but populates error_info (if not NULL) when none of the regexps
59 // in the set matched. This can inform callers when DFA execution fails, for
60 // example, because they might wish to handle that case differently.
61 bool Match(const StringPiece& text, std::vector<int>* v,
62 ErrorInfo* error_info) const;
63
64 private:
65 typedef std::pair<std::string, re2::Regexp*> Elem;
66
67 RE2::Options options_;
68 RE2::Anchor anchor_;
69 std::vector<Elem> elem_;
70 re2::Prog* prog_;
71 bool compiled_;
72 int size_;
73
74 Set(const Set&) = delete;
75 Set& operator=(const Set&) = delete;
76};
77
78} // namespace re2
79
80#endif // RE2_SET_H_
81