1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
24 #include <QStringList>
26 #if QT_VERSION >= 0x050000
27 #include <QRegularExpression>
33 * Expression matcher with multiple modes of operation and automatic caching for performance
39 /// Expression matching mode
40 #if QT_VERSION >= 0x050000
41 enum class MatchMode {
45 MatchPhrase, ///< Match phrase as specified, no special handling
46 MatchMultiPhrase, ///< Match phrase as specified, split on \n only
47 MatchWildcard, ///< Match wildcards, "!" at start inverts, "\" escapes
48 MatchMultiWildcard, ///< Match wildcards, split ; or \n, "!" at start inverts, "\" escapes
49 MatchRegEx ///< Match as regular expression, "!..." invert regex, "\" escapes
53 * Construct an empty ExpressionMatch
58 * Construct an Expression match with the given parameters
60 * @param expression A phrase, wildcard expression, or regular expression
63 * Expression matching mode
64 * @see ExpressionMatch::MatchMode
66 * @param caseSensitive If true, match case-sensitively, otherwise ignore case when matching
68 ExpressionMatch(const QString &expression, MatchMode mode, bool caseSensitive);
71 * Check if the given string matches the stored expression
73 * @param string String to check
74 * @param matchEmpty If true, always match when the expression is empty, otherwise never match
75 * @return True if match found, otherwise false
77 bool match(const QString &string, bool matchEmpty = false) const;
80 * Gets if the source expression is empty
82 * @return True if source expression is empty, otherwise false
84 inline bool isEmpty() const { return (_sourceExpressionEmpty); }
87 * Gets if the source expression and parameters resulted in a valid expression matcher
89 * @return True if given expression is valid, otherwise false
91 inline bool isValid() const {
92 // Either this must be empty, or normal or inverted rules must be valid and active
93 return (_sourceExpressionEmpty
94 || (_matchRegExActive && _matchRegEx.isValid())
95 || (_matchInvertRegExActive && _matchInvertRegEx.isValid()));
99 * Gets the original expression match string
101 * @return QString of the source expression match string
103 inline QString sourceExpression() const { return _sourceExpression; }
106 * Sets the expression match string
108 * @param expression A phrase, wildcard expression, or regular expression
110 void setSourceExpression(const QString &expression) {
111 if (_sourceExpression != expression) {
112 _sourceExpression = expression;
118 * Gets the original expression match mode
120 * @return MatchMode of the source expression
122 inline MatchMode sourceMode() const { return _sourceMode; }
125 * Sets the expression match mode
129 * Expression matching mode
130 * @see ExpressionMatch::MatchMode
133 void setSourceMode(MatchMode mode) {
134 if (_sourceMode != mode) {
141 * Gets the original expression case-sensitivity
143 * @return True if case-sensitive, otherwise false
145 inline bool sourceCaseSensitive() const { return _sourceCaseSensitive; }
148 * Sets the expression match as case sensitive or not
150 * @param caseSensitive If true, match case-sensitively, otherwise ignore case when matching
152 void setSourceCaseSensitive(bool caseSensitive) {
153 if (_sourceCaseSensitive != caseSensitive) {
154 _sourceCaseSensitive = caseSensitive;
159 bool operator!=(const ExpressionMatch &other) const
161 return (_sourceExpression != other._sourceExpression ||
162 _sourceMode != other._sourceMode ||
163 _sourceCaseSensitive != other._sourceCaseSensitive);
167 * Trim extraneous whitespace from individual rules within a given MultiWildcard expression
169 * This respects the ";" escaping rules with "\". It is safe to call this multiple times; a
170 * trimmed string should remain unchanged.
172 * @see ExpressionMatch::MatchMode::MatchMultiWildcard
174 * @param originalRule MultiWildcard rule list, ";"-separated
175 * @return Trimmed MultiWildcard rule list
177 static QString trimMultiWildcardWhitespace(const QString &originalRule);
181 * Calculates internal regular expressions
183 * Will always run when called, no cache validity checks performed.
188 * Creates a regular expression object of appropriate type and case-sensitivity
190 * @param regExString Regular expression string
191 * @param caseSensitive If true, match case-sensitively, otherwise ignore case when matching
192 * @return Configured QRegExp class on Qt 4, QRegularExpression on Qt 5
194 #if QT_VERSION >= 0x050000
195 static QRegularExpression regExFactory(const QString ®ExString, bool caseSensitive);
197 static QRegExp regExFactory(const QString ®ExString, bool caseSensitive);
201 * Escapes any regular expression characters in a string so they have no special meaning
203 * @param phrase String containing potential regular expression special characters
204 * @return QString with all regular expression characters escaped
206 static QString regExEscape(const QString &phrase);
209 * Converts a multiple-phrase rule into a regular expression
211 * Unconditionally splits phrases on "\n", whitespace is preserved
213 * @param originalRule MultiPhrase rule list, "\n"-separated
214 * @return A regular expression matching the given phrases
216 static QString convertFromMultiPhrase(const QString &originalRule);
219 * Internally converts a wildcard rule into regular expressions
221 * Splits wildcards on ";" and "\n", "!..." inverts section, "\" escapes
223 * @param originalRule MultiWildcard rule list, ";"-separated
224 * @param caseSensitive If true, match case-sensitively, otherwise ignore case when matching
226 void generateFromMultiWildcard(const QString &originalRule, bool caseSensitive);
229 * Converts a wildcard expression into a regular expression
231 * NOTE: Does not handle Quassel's extended scope matching and splitting.
233 * @see ExpressionMatch::convertFromWildcard()
234 * @return QString with all regular expression characters escaped
236 static QString wildcardToRegEx(const QString &expression);
238 // Original/source components
239 QString _sourceExpression = {}; ///< Expression match string given on creation
240 MatchMode _sourceMode = MatchMode::MatchPhrase; ///< Expression match mode given on creation
241 bool _sourceCaseSensitive = false; ///< Expression case sensitive on creation
243 // Derived components
244 bool _sourceExpressionEmpty = false; ///< Cached expression match string is empty
246 /// Underlying regular expression matching instance for normal (noninverted) rules
247 #if QT_VERSION >= 0x050000
248 QRegularExpression _matchRegEx = {};
250 QRegExp _matchRegEx = {};
252 bool _matchRegExActive = false; ///< If true, use normal expression in matching
254 /// Underlying regular expression matching instance for inverted rules
255 #if QT_VERSION >= 0x050000
256 QRegularExpression _matchInvertRegEx = {};
258 QRegExp _matchInvertRegEx = {};
260 bool _matchInvertRegExActive = false; ///< If true, use invert expression in matching