12ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// Copyright 2010 The RE2 Authors.  All Rights Reserved.
22ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// Use of this source code is governed by a BSD-style
32ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// license that can be found in the LICENSE file.
42ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
52ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#ifndef RE2_SET_H
62ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define RE2_SET_H
72ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
82ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#include <utility>
92ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#include <vector>
102ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
112ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#include "re2/re2.h"
122ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
132ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodsonnamespace re2 {
142ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodsonusing std::vector;
152ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
162ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// An RE2::Set represents a collection of regexps that can
172ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// be searched for simultaneously.
182ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodsonclass RE2::Set {
192ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson public:
202ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  Set(const RE2::Options& options, RE2::Anchor anchor);
212ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  ~Set();
222ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
232ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Add adds regexp pattern to the set, interpreted using the RE2 options.
242ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // (The RE2 constructor's default options parameter is RE2::UTF8.)
252ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Add returns the regexp index that will be used to identify
262ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // it in the result of Match, or -1 if the regexp cannot be parsed.
272ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Indices are assigned in sequential order starting from 0.
282ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Error returns do not increment the index.
292ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // If an error occurs and error != NULL, *error will hold an error message.
302ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  int Add(const StringPiece& pattern, string* error);
312ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
322ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Compile prepares the Set for matching.
332ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Add must not be called again after Compile.
342ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Compile must be called before FullMatch or PartialMatch.
352ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Compile may return false if it runs out of memory.
362ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  bool Compile();
372ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
382ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Match returns true if text matches any of the regexps in the set.
392ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // If so, it fills v with the indices of the matching regexps.
402ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  bool Match(const StringPiece& text, vector<int>* v) const;
412ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
422ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson private:
432ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  RE2::Options options_;
442ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  RE2::Anchor anchor_;
452ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  vector<re2::Regexp*> re_;
462ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  re2::Prog* prog_;
472ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  bool compiled_;
482ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  //DISALLOW_EVIL_CONSTRUCTORS(Set);
492ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  Set(const Set&);
502ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  void operator=(const Set&);
512ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson};
522ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
532ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson}  // namespace re2
542ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
552ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#endif  // RE2_SET_H
56