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:
@@ -0,0 +1,22 @@
|
||||
#include "RegexPredicate.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
RegexPredicate::RegexPredicate(const QString ®ex)
|
||||
: 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
|
||||
@@ -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 ®ex);
|
||||
|
||||
/**
|
||||
* @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
|
||||
Reference in New Issue
Block a user