Add new search predicate to enable searching for messages matching a regex (#3282)

Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
LosFarmosCTL
2021-10-17 14:36:44 +02:00
committed by GitHub
parent c1a3814b7c
commit 06245f3713
6 changed files with 109 additions and 39 deletions
+22
View File
@@ -0,0 +1,22 @@
#include "RegexPredicate.hpp"
namespace chatterino {
RegexPredicate::RegexPredicate(const QString &regex)
: regex_(regex, QRegularExpression::CaseInsensitiveOption)
{
}
bool RegexPredicate::appliesTo(const Message &message)
{
if (!regex_.isValid())
{
return false;
}
QRegularExpressionMatch match = regex_.match(message.messageText);
return match.hasMatch();
}
} // namespace chatterino
+42
View File
@@ -0,0 +1,42 @@
#pragma once
#include "QRegularExpression"
#include "messages/search/MessagePredicate.hpp"
namespace chatterino {
/**
* @brief MessagePredicate checking whether the message matches a given regex.
*
* This predicate will only allow messages whose `messageText` match the given
* regex.
*/
class RegexPredicate : public MessagePredicate
{
public:
/**
* @brief Create a RegexPredicate with a regex to match the message against.
*
* The message is being matched case-insensitively.
*
* @param regex the regex to match the message against
*/
RegexPredicate(const QString &regex);
/**
* @brief Checks whether the message matches the regex passed in the
* constructor
*
* The check is done case-insensitively.
*
* @param message the message to check
* @return true if the message matches the regex, false otherwise
*/
bool appliesTo(const Message &message);
private:
/// Holds the regular expression to match the message against
QRegularExpression regex_;
};
} // namespace chatterino