Chatterino
HighlightBlacklistUser.hpp
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #include <QRegularExpression>
7 #include <QString>
8 #include <pajlada/serialize.hpp>
9 
10 #include <memory>
11 
12 namespace chatterino {
13 
15 {
16 public:
17  bool operator==(const HighlightBlacklistUser &other) const
18  {
19  return std::tie(this->pattern_, this->isRegex_) ==
20  std::tie(other.pattern_, other.isRegex_);
21  }
22 
23  HighlightBlacklistUser(const QString &pattern, bool isRegex = false)
24  : pattern_(pattern)
25  , isRegex_(isRegex)
26  , regex_(isRegex ? pattern : "",
27  QRegularExpression::CaseInsensitiveOption |
28  QRegularExpression::UseUnicodePropertiesOption)
29  {
30  }
31 
32  const QString &getPattern() const
33  {
34  return this->pattern_;
35  }
36 
37  bool isRegex() const
38  {
39  return this->isRegex_;
40  }
41 
42  bool isValidRegex() const
43  {
44  return this->isRegex() && this->regex_.isValid();
45  }
46 
47  bool isMatch(const QString &subject) const
48  {
49  if (this->isRegex())
50  {
51  if (this->isValidRegex())
52  {
53  return this->regex_.match(subject).hasMatch();
54  }
55 
56  return false;
57  }
58 
59  return subject.toLower() == this->pattern_.toLower();
60  }
61 
62 private:
63  QString pattern_;
64  bool isRegex_;
65  QRegularExpression regex_;
66 };
67 
68 } // namespace chatterino
69 
70 namespace pajlada {
71 
72 template <>
73 struct Serialize<chatterino::HighlightBlacklistUser> {
74  static rapidjson::Value get(const chatterino::HighlightBlacklistUser &value,
75  rapidjson::Document::AllocatorType &a)
76  {
77  rapidjson::Value ret(rapidjson::kObjectType);
78 
79  chatterino::rj::set(ret, "pattern", value.getPattern(), a);
80  chatterino::rj::set(ret, "regex", value.isRegex(), a);
81 
82  return ret;
83  }
84 };
85 
86 template <>
87 struct Deserialize<chatterino::HighlightBlacklistUser> {
88  static chatterino::HighlightBlacklistUser get(const rapidjson::Value &value,
89  bool *error = nullptr)
90  {
91  QString pattern;
92  bool isRegex = false;
93 
94  if (!value.IsObject())
95  {
96  PAJLADA_REPORT_ERROR(error)
97  return chatterino::HighlightBlacklistUser(pattern, isRegex);
98  }
99 
100  chatterino::rj::getSafe(value, "pattern", pattern);
101  chatterino::rj::getSafe(value, "regex", isRegex);
102 
103  return chatterino::HighlightBlacklistUser(pattern, isRegex);
104  }
105 };
106 
107 } // namespace pajlada
bool operator==(const HighlightBlacklistUser &other) const
Definition: HighlightBlacklistUser.hpp:17
Definition: HighlightBlacklistUser.hpp:14
bool isRegex() const
Definition: HighlightBlacklistUser.hpp:37
Definition: Application.cpp:48
HighlightBlacklistUser(const QString &pattern, bool isRegex=false)
Definition: HighlightBlacklistUser.hpp:23
bool isMatch(const QString &subject) const
Definition: HighlightBlacklistUser.hpp:47
void set(rapidjson::Value &obj, const char *key, const Type &value, rapidjson::Document::AllocatorType &a)
Definition: RapidjsonHelpers.hpp:22
Definition: Command.hpp:25
bool getSafe(const rapidjson::Value &obj, const char *key, Type &out)
Definition: RapidjsonHelpers.hpp:73
bool isValidRegex() const
Definition: HighlightBlacklistUser.hpp:42
const QString & getPattern() const
Definition: HighlightBlacklistUser.hpp:32