You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

set.h 2.8 kB

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