PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/navigator/Regex.cc

https://github.com/zhongxingzhi/sourceweb
C++ | 80 lines | 63 code | 14 blank | 3 comment | 4 complexity | 4e0a95f379a9cd5699aaa43b16489600 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #include "Regex.h"
  2. #include <cassert>
  3. #include <string>
  4. #include <re2/re2.h>
  5. using re2::RE2;
  6. namespace Nav {
  7. // Constructs an invalid Regex object.
  8. Regex::Regex()
  9. {
  10. initWithPattern("");
  11. }
  12. Regex::Regex(const std::string &pattern)
  13. {
  14. initWithPattern(pattern);
  15. }
  16. Regex::Regex(const Regex &other)
  17. {
  18. initWithPattern(other.re2().pattern());
  19. }
  20. Regex &Regex::operator=(const Regex &other)
  21. {
  22. initWithPattern(other.re2().pattern());
  23. return *this;
  24. }
  25. void Regex::initWithPattern(const std::string &pattern)
  26. {
  27. bool caseSensitive = false;
  28. for (unsigned char ch : pattern) {
  29. if (isupper(ch)) {
  30. caseSensitive = true;
  31. break;
  32. }
  33. }
  34. RE2::Options options;
  35. options.set_case_sensitive(caseSensitive);
  36. options.set_posix_syntax(true);
  37. options.set_log_errors(false);
  38. options.set_one_line(false);
  39. options.set_perl_classes(true);
  40. options.set_word_boundary(true);
  41. options.set_never_capture(true);
  42. m_re2 = std::unique_ptr<RE2>(new RE2(pattern, options));
  43. }
  44. // ~Regex needs to be defined here in Regex.cc because Regex.h leaves re2::RE2
  45. // an incomplete type (i.e. it does not include re2/re2.h).
  46. Regex::~Regex()
  47. {
  48. }
  49. bool Regex::valid() const
  50. {
  51. return m_re2->ok();
  52. }
  53. bool Regex::empty() const
  54. {
  55. return !m_re2->ok() || m_re2->pattern().empty();
  56. }
  57. bool Regex::match(const char *text) const
  58. {
  59. assert(m_re2->ok());
  60. return m_re2->Match(text, 0, strlen(text), RE2::UNANCHORED, NULL, 0);
  61. }
  62. bool operator==(const Regex &x, const Regex &y)
  63. {
  64. return x.re2().pattern() == y.re2().pattern();
  65. }
  66. } // namespace Nav