set.h 2.4 KB

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