1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 ***************************************************************************/
23 #include <QRegularExpression>
25 #include <QStringList>
28 * Expression matcher with multiple modes of operation and automatic caching for performance
34 /// Expression matching mode
35 enum class MatchMode {
36 MatchPhrase, ///< Match phrase as specified, no special handling
37 MatchMultiPhrase, ///< Match phrase as specified, split on \n only
38 MatchWildcard, ///< Match wildcards, "!" at start inverts, "\" escapes
39 MatchMultiWildcard, ///< Match wildcards, split ; or \n, "!" at start inverts, "\" escapes
40 MatchRegEx ///< Match as regular expression, "!..." invert regex, "\" escapes
44 * Construct an empty ExpressionMatch
49 * Construct an Expression match with the given parameters
51 * @param expression A phrase, wildcard expression, or regular expression
54 * Expression matching mode
55 * @see ExpressionMatch::MatchMode
57 * @param caseSensitive If true, match case-sensitively, otherwise ignore case when matching
59 ExpressionMatch(const QString &expression, MatchMode mode, bool caseSensitive);
62 * Check if the given string matches the stored expression
64 * @param string String to check
65 * @param matchEmpty If true, always match when the expression is empty, otherwise never match
66 * @return True if match found, otherwise false
68 bool match(const QString &string, bool matchEmpty = false) const;
71 * Gets if the source expression is empty
73 * @return True if source expression is empty, otherwise false
75 inline bool isEmpty() const { return (_sourceExpressionEmpty); }
78 * Gets if the source expression and parameters resulted in a valid expression matcher
80 * @return True if given expression is valid, otherwise false
82 inline bool isValid() const {
83 // Either this must be empty, or normal or inverted rules must be valid and active
84 return (_sourceExpressionEmpty
85 || (_matchRegExActive && _matchRegEx.isValid())
86 || (_matchInvertRegExActive && _matchInvertRegEx.isValid()));
90 * Gets the original expression match string
92 * @return QString of the source expression match string
94 inline QString sourceExpression() const { return _sourceExpression; }
97 * Sets the expression match string
99 * @param expression A phrase, wildcard expression, or regular expression
101 void setSourceExpression(const QString &expression) {
102 if (_sourceExpression != expression) {
103 _sourceExpression = expression;
109 * Gets the original expression match mode
111 * @return MatchMode of the source expression
113 inline MatchMode sourceMode() const { return _sourceMode; }
116 * Sets the expression match mode
120 * Expression matching mode
121 * @see ExpressionMatch::MatchMode
124 void setSourceMode(MatchMode mode) {
125 if (_sourceMode != mode) {
132 * Gets the original expression case-sensitivity
134 * @return True if case-sensitive, otherwise false
136 inline bool sourceCaseSensitive() const { return _sourceCaseSensitive; }
139 * Sets the expression match as case sensitive or not
141 * @param caseSensitive If true, match case-sensitively, otherwise ignore case when matching
143 void setSourceCaseSensitive(bool caseSensitive) {
144 if (_sourceCaseSensitive != caseSensitive) {
145 _sourceCaseSensitive = caseSensitive;
150 bool operator!=(const ExpressionMatch &other) const
152 return (_sourceExpression != other._sourceExpression ||
153 _sourceMode != other._sourceMode ||
154 _sourceCaseSensitive != other._sourceCaseSensitive);
158 * Trim extraneous whitespace from individual rules within a given MultiWildcard expression
160 * This respects the ";" escaping rules with "\". It is safe to call this multiple times; a
161 * trimmed string should remain unchanged.
163 * @see ExpressionMatch::MatchMode::MatchMultiWildcard
165 * @param originalRule MultiWildcard rule list, ";"-separated
166 * @return Trimmed MultiWildcard rule list
168 static QString trimMultiWildcardWhitespace(const QString &originalRule);
172 * Calculates internal regular expressions
174 * Will always run when called, no cache validity checks performed.
179 * Creates a regular expression object of appropriate type and case-sensitivity
181 * @param regExString Regular expression string
182 * @param caseSensitive If true, match case-sensitively, otherwise ignore case when matching
183 * @return Configured QRegularExpression
185 static QRegularExpression regExFactory(const QString ®ExString, bool caseSensitive);
188 * Escapes any regular expression characters in a string so they have no special meaning
190 * @param phrase String containing potential regular expression special characters
191 * @return QString with all regular expression characters escaped
193 static QString regExEscape(const QString &phrase);
196 * Converts a multiple-phrase rule into a regular expression
198 * Unconditionally splits phrases on "\n", whitespace is preserved
200 * @param originalRule MultiPhrase rule list, "\n"-separated
201 * @return A regular expression matching the given phrases
203 static QString convertFromMultiPhrase(const QString &originalRule);
206 * Internally converts a wildcard rule into regular expressions
208 * Splits wildcards on ";" and "\n", "!..." inverts section, "\" escapes
210 * @param originalRule MultiWildcard rule list, ";"-separated
211 * @param caseSensitive If true, match case-sensitively, otherwise ignore case when matching
213 void generateFromMultiWildcard(const QString &originalRule, bool caseSensitive);
216 * Converts a wildcard expression into a regular expression
218 * NOTE: Does not handle Quassel's extended scope matching and splitting.
220 * @see ExpressionMatch::convertFromWildcard()
221 * @return QString with all regular expression characters escaped
223 static QString wildcardToRegEx(const QString &expression);
225 // Original/source components
226 QString _sourceExpression = {}; ///< Expression match string given on creation
227 MatchMode _sourceMode = MatchMode::MatchPhrase; ///< Expression match mode given on creation
228 bool _sourceCaseSensitive = false; ///< Expression case sensitive on creation
230 // Derived components
231 bool _sourceExpressionEmpty = false; ///< Cached expression match string is empty
233 /// Underlying regular expression matching instance for normal (noninverted) rules
234 QRegularExpression _matchRegEx = {};
235 bool _matchRegExActive = false; ///< If true, use normal expression in matching
237 /// Underlying regular expression matching instance for inverted rules
238 QRegularExpression _matchInvertRegEx = {};
239 bool _matchInvertRegExActive = false; ///< If true, use invert expression in matching