Chatterino
Nickname.hpp
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include <QString>
8 #include <pajlada/serialize.hpp>
9 
10 #include <memory>
11 
12 namespace chatterino {
13 
14 class Nickname
15 {
16 public:
17  Nickname(const QString &name, const QString &replace, const bool isRegex,
18  const bool isCaseSensitive)
19  : name_(name)
20  , replace_(replace)
21  , isRegex_(isRegex)
22  , isCaseSensitive_(isCaseSensitive)
23  , caseSensitivity_(this->isCaseSensitive_ ? Qt::CaseSensitive
24  : Qt::CaseInsensitive)
25  {
26  if (this->isRegex())
27  {
28  this->regex_ = QRegularExpression(
29  name, QRegularExpression::UseUnicodePropertiesOption |
30  (this->isCaseSensitive()
31  ? QRegularExpression::NoPatternOption
32  : QRegularExpression::CaseInsensitiveOption));
33  }
34  }
35 
36  [[nodiscard]] const QString &name() const
37  {
38  return this->name_;
39  }
40 
41  [[nodiscard]] const QString &replace() const
42  {
43  return this->replace_;
44  }
45 
46  [[nodiscard]] bool isRegex() const
47  {
48  return this->isRegex_;
49  }
50 
51  [[nodiscard]] Qt::CaseSensitivity caseSensitivity() const
52  {
53  return this->caseSensitivity_;
54  }
55 
56  [[nodiscard]] const bool &isCaseSensitive() const
57  {
58  return this->isCaseSensitive_;
59  }
60 
61  [[nodiscard]] bool match(QString &usernameText) const
62  {
63  if (this->isRegex())
64  {
65  if (!this->regex_.isValid())
66  {
67  return false;
68  }
69  if (this->name().isEmpty())
70  {
71  return false;
72  }
73 
74  auto workingCopy = usernameText;
75  workingCopy.replace(this->regex_, this->replace());
76  if (workingCopy != usernameText)
77  {
78  usernameText = workingCopy;
79  return true;
80  }
81  }
82  else
83  {
84  auto res =
85  this->name().compare(usernameText, this->caseSensitivity());
86  if (res == 0)
87  {
88  usernameText = this->replace();
89  return true;
90  }
91  }
92 
93  return false;
94  }
95 
96 private:
97  QString name_;
98  QString replace_;
99  bool isRegex_;
100  bool isCaseSensitive_;
101  Qt::CaseSensitivity caseSensitivity_;
102  QRegularExpression regex_{};
103 };
104 
105 } // namespace chatterino
106 
107 namespace pajlada {
108 
109 template <>
110 struct Serialize<chatterino::Nickname> {
111  static rapidjson::Value get(const chatterino::Nickname &value,
112  rapidjson::Document::AllocatorType &a)
113  {
114  rapidjson::Value ret(rapidjson::kObjectType);
115 
116  chatterino::rj::set(ret, "name", value.name(), a);
117  chatterino::rj::set(ret, "replace", value.replace(), a);
118  chatterino::rj::set(ret, "isRegex", value.isRegex(), a);
119  chatterino::rj::set(ret, "isCaseSensitive", value.isCaseSensitive(), a);
120 
121  return ret;
122  }
123 };
124 
125 template <>
126 struct Deserialize<chatterino::Nickname> {
127  static chatterino::Nickname get(const rapidjson::Value &value,
128  bool *error = nullptr)
129  {
130  if (!value.IsObject())
131  {
132  PAJLADA_REPORT_ERROR(error)
133  return chatterino::Nickname(QString(), QString(), false, false);
134  }
135 
136  QString _name;
137  QString _replace;
138  bool _isRegex;
139  bool _isCaseSensitive;
140 
141  chatterino::rj::getSafe(value, "name", _name);
142  chatterino::rj::getSafe(value, "replace", _replace);
143  chatterino::rj::getSafe(value, "isRegex", _isRegex);
144  chatterino::rj::getSafe(value, "isCaseSensitive", _isCaseSensitive);
145 
146  return chatterino::Nickname(_name, _replace, _isRegex,
147  _isCaseSensitive);
148  }
149 };
150 
151 } // namespace pajlada
const QString & replace() const
Definition: Nickname.hpp:41
Definition: Application.cpp:48
const bool & isCaseSensitive() const
Definition: Nickname.hpp:56
bool match(QString &usernameText) const
Definition: Nickname.hpp:61
void set(rapidjson::Value &obj, const char *key, const Type &value, rapidjson::Document::AllocatorType &a)
Definition: RapidjsonHelpers.hpp:22
Qt::CaseSensitivity caseSensitivity() const
Definition: Nickname.hpp:51
Definition: Command.hpp:25
bool isRegex() const
Definition: Nickname.hpp:46
bool getSafe(const rapidjson::Value &obj, const char *key, Type &out)
Definition: RapidjsonHelpers.hpp:73
Nickname(const QString &name, const QString &replace, const bool isRegex, const bool isCaseSensitive)
Definition: Nickname.hpp:17
Definition: Nickname.hpp:14
const QString & name() const
Definition: Nickname.hpp:36