/***************************************************************************
- * Copyright (C) 2005-2018 by the Quassel Project *
+ * Copyright (C) 2005-2019 by the Quassel Project *
* devel@quassel-irc.org *
* *
* This program is free software; you can redistribute it and/or modify *
#pragma once
-#include <QString>
-#include <QStringList>
+#include "common-export.h"
-#if QT_VERSION >= 0x050000
#include <QRegularExpression>
-#else
-#include <QRegExp>
-#endif
+#include <QString>
+#include <QStringList>
/**
* Expression matcher with multiple modes of operation and automatic caching for performance
*/
-class ExpressionMatch
+class COMMON_EXPORT ExpressionMatch
{
-
public:
/// Expression matching mode
-#if QT_VERSION >= 0x050000
- enum class MatchMode {
-#else
- enum MatchMode {
-#endif
- MatchPhrase, ///< Match phrase as specified, no special handling
- MatchMultiPhrase, ///< Match phrase as specified, split on \n only
- MatchWildcard, ///< Match wildcards, "!" at start inverts, "\" escapes
- MatchMultiWildcard, ///< Match wildcards, split ; or \n, "!" at start inverts, "\" escapes
- MatchRegEx ///< Match as regular expression, "!..." invert regex, "\" escapes
+ enum class MatchMode
+ {
+ MatchPhrase, ///< Match phrase as specified, no special handling
+ MatchMultiPhrase, ///< Match phrase as specified, split on \n only
+ MatchWildcard, ///< Match wildcards, "!" at start inverts, "\" escapes
+ MatchMultiWildcard, ///< Match wildcards, split ; or \n, "!" at start inverts, "\" escapes
+ MatchRegEx ///< Match as regular expression, "!..." invert regex, "\" escapes
};
/**
* Construct an empty ExpressionMatch
*/
- ExpressionMatch() {}
+ ExpressionMatch() = default;
/**
* Construct an Expression match with the given parameters
* @endparblock
* @param caseSensitive If true, match case-sensitively, otherwise ignore case when matching
*/
- ExpressionMatch(const QString &expression, MatchMode mode, bool caseSensitive);
+ ExpressionMatch(const QString& expression, MatchMode mode, bool caseSensitive);
/**
* Check if the given string matches the stored expression
* @param matchEmpty If true, always match when the expression is empty, otherwise never match
* @return True if match found, otherwise false
*/
- bool match(const QString &string, bool matchEmpty = false) const;
+ bool match(const QString& string, bool matchEmpty = false) const;
/**
* Gets if the source expression is empty
*
* @return True if given expression is valid, otherwise false
*/
- inline bool isValid() const {
+ inline bool isValid() const
+ {
// Either this must be empty, or normal or inverted rules must be valid and active
- return (_sourceExpressionEmpty
- || (_matchRegExActive && _matchRegEx.isValid())
+ return (_sourceExpressionEmpty || (_matchRegExActive && _matchRegEx.isValid())
|| (_matchInvertRegExActive && _matchInvertRegEx.isValid()));
}
*
* @param expression A phrase, wildcard expression, or regular expression
*/
- void setSourceExpression(const QString &expression) {
+ void setSourceExpression(const QString& expression)
+ {
if (_sourceExpression != expression) {
_sourceExpression = expression;
cacheRegEx();
* @see ExpressionMatch::MatchMode
* @endparblock
*/
- void setSourceMode(MatchMode mode) {
+ void setSourceMode(MatchMode mode)
+ {
if (_sourceMode != mode) {
_sourceMode = mode;
cacheRegEx();
*
* @param caseSensitive If true, match case-sensitively, otherwise ignore case when matching
*/
- void setSourceCaseSensitive(bool caseSensitive) {
+ void setSourceCaseSensitive(bool caseSensitive)
+ {
if (_sourceCaseSensitive != caseSensitive) {
_sourceCaseSensitive = caseSensitive;
cacheRegEx();
}
}
- bool operator!=(const ExpressionMatch &other) const
+ bool operator!=(const ExpressionMatch& other) const
{
- return (_sourceExpression != other._sourceExpression ||
- _sourceMode != other._sourceMode ||
- _sourceCaseSensitive != other._sourceCaseSensitive);
+ return (_sourceExpression != other._sourceExpression || _sourceMode != other._sourceMode
+ || _sourceCaseSensitive != other._sourceCaseSensitive);
}
/**
* @param originalRule MultiWildcard rule list, ";"-separated
* @return Trimmed MultiWildcard rule list
*/
- static QString trimMultiWildcardWhitespace(const QString &originalRule);
+ static QString trimMultiWildcardWhitespace(const QString& originalRule);
private:
/**
*
* @param regExString Regular expression string
* @param caseSensitive If true, match case-sensitively, otherwise ignore case when matching
- * @return Configured QRegExp class on Qt 4, QRegularExpression on Qt 5
+ * @return Configured QRegularExpression
*/
-#if QT_VERSION >= 0x050000
- static QRegularExpression regExFactory(const QString ®ExString, bool caseSensitive);
-#else
- static QRegExp regExFactory(const QString ®ExString, bool caseSensitive);
-#endif
+ static QRegularExpression regExFactory(const QString& regExString, bool caseSensitive);
/**
* Escapes any regular expression characters in a string so they have no special meaning
* @param phrase String containing potential regular expression special characters
* @return QString with all regular expression characters escaped
*/
- static QString regExEscape(const QString &phrase);
+ static QString regExEscape(const QString& phrase);
/**
* Converts a multiple-phrase rule into a regular expression
* @param originalRule MultiPhrase rule list, "\n"-separated
* @return A regular expression matching the given phrases
*/
- static QString convertFromMultiPhrase(const QString &originalRule);
+ static QString convertFromMultiPhrase(const QString& originalRule);
/**
* Internally converts a wildcard rule into regular expressions
* @param originalRule MultiWildcard rule list, ";"-separated
* @param caseSensitive If true, match case-sensitively, otherwise ignore case when matching
*/
- void generateFromMultiWildcard(const QString &originalRule, bool caseSensitive);
+ void generateFromMultiWildcard(const QString& originalRule, bool caseSensitive);
/**
* Converts a wildcard expression into a regular expression
* @see ExpressionMatch::convertFromWildcard()
* @return QString with all regular expression characters escaped
*/
- static QString wildcardToRegEx(const QString &expression);
+ static QString wildcardToRegEx(const QString& expression);
// Original/source components
QString _sourceExpression = {}; ///< Expression match string given on creation
bool _sourceCaseSensitive = false; ///< Expression case sensitive on creation
// Derived components
- bool _sourceExpressionEmpty = false; ///< Cached expression match string is empty
+ bool _sourceExpressionEmpty = false; ///< Cached expression match string is empty
/// Underlying regular expression matching instance for normal (noninverted) rules
-#if QT_VERSION >= 0x050000
QRegularExpression _matchRegEx = {};
-#else
- QRegExp _matchRegEx = {};
-#endif
- bool _matchRegExActive = false; ///< If true, use normal expression in matching
+ bool _matchRegExActive = false; ///< If true, use normal expression in matching
/// Underlying regular expression matching instance for inverted rules
-#if QT_VERSION >= 0x050000
QRegularExpression _matchInvertRegEx = {};
-#else
- QRegExp _matchInvertRegEx = {};
-#endif
- bool _matchInvertRegExActive = false; ///< If true, use invert expression in matching
+ bool _matchInvertRegExActive = false; ///< If true, use invert expression in matching
};