blob: a8c2caa4a877c64a6eff31982ad536978234da8f [file] [log] [blame]
Russ Coxc334dcc2010-07-15 18:48:48 -07001// 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
Paul Wankadia89a59072016-07-26 21:09:42 +10005#ifndef RE2_SET_H_
6#define RE2_SET_H_
Russ Coxc334dcc2010-07-15 18:48:48 -07007
Paul Wankadia00299462016-08-02 20:26:18 +10008#include <string>
Paul Wankadiaed5be2c2017-10-11 21:23:13 +11009#include <utility>
Russ Coxc334dcc2010-07-15 18:48:48 -070010#include <vector>
11
12#include "re2/re2.h"
13
14namespace re2 {
Paul Wankadia998584d2016-08-02 16:54:50 +100015class Prog;
16class Regexp;
17} // namespace re2
18
19namespace re2 {
Russ Coxc334dcc2010-07-15 18:48:48 -070020
21// An RE2::Set represents a collection of regexps that can
22// be searched for simultaneously.
23class RE2::Set {
24 public:
Paul Wankadiaee52f032017-11-16 20:57:41 +110025 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
Russ Coxc334dcc2010-07-15 18:48:48 -070036 Set(const RE2::Options& options, RE2::Anchor anchor);
37 ~Set();
38
Paul Wankadiaee52f032017-11-16 20:57:41 +110039 // 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.
Russ Coxc334dcc2010-07-15 18:48:48 -070042 // Indices are assigned in sequential order starting from 0.
Paul Wankadiaee52f032017-11-16 20:57:41 +110043 // Errors do not increment the index; if error is not NULL, *error will hold
44 // the error message from the parser.
Russ Coxc334dcc2010-07-15 18:48:48 -070045 int Add(const StringPiece& pattern, string* error);
46
Paul Wankadiaee52f032017-11-16 20:57:41 +110047 // 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().
Russ Coxc334dcc2010-07-15 18:48:48 -070051 bool Compile();
52
Paul Wankadiaee52f032017-11-16 20:57:41 +110053 // 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.
Paul Wankadia4bb66d42016-11-22 00:10:13 +110055 // Callers must not expect v to be sorted.
Paul Wankadiaee55a8f2016-08-02 21:49:57 +100056 bool Match(const StringPiece& text, std::vector<int>* v) const;
Russ Coxc334dcc2010-07-15 18:48:48 -070057
Paul Wankadiaee52f032017-11-16 20:57:41 +110058 // 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
Russ Coxc334dcc2010-07-15 18:48:48 -070064 private:
Paul Wankadiaed5be2c2017-10-11 21:23:13 +110065 typedef std::pair<string, re2::Regexp*> Elem;
66
Russ Coxc334dcc2010-07-15 18:48:48 -070067 RE2::Options options_;
68 RE2::Anchor anchor_;
Paul Wankadiaed5be2c2017-10-11 21:23:13 +110069 std::vector<Elem> elem_;
Russ Coxc334dcc2010-07-15 18:48:48 -070070 re2::Prog* prog_;
71 bool compiled_;
Paul Wankadia295316b2017-10-04 00:33:16 +110072 int size_;
Paul Wankadiaf408be02016-08-16 23:42:11 +100073
74 Set(const Set&) = delete;
75 Set& operator=(const Set&) = delete;
Russ Coxc334dcc2010-07-15 18:48:48 -070076};
77
78} // namespace re2
79
Paul Wankadia89a59072016-07-26 21:09:42 +100080#endif // RE2_SET_H_