1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(messages): Replace QRegExp with QRegularExpression

This commit is contained in:
Mick Sayson 2019-06-19 18:06:37 -07:00
parent fef89d70f9
commit c779d52aef
2 changed files with 16 additions and 10 deletions

View File

@ -24,9 +24,11 @@
void MessageProcessor::SharedParams::onUserNameSet(const QString& username)
{
QString sanename = username;
sanename.remove(QRegExp("[\\t\\n\\v\\f\\r\\x0000]"));
nameMention = QRegExp("\\b" + QRegExp::escape(username) + "\\b", Qt::CaseInsensitive);
sanitizedNameMention = QRegExp("\\b" + QRegExp::escape(sanename) + "\\b", Qt::CaseInsensitive);
sanename.remove(QRegularExpression("[\\t\\n\\v\\f\\r\\x0000]"));
nameMention = QRegularExpression("\\b" + QRegularExpression::escape(username) + "\\b",
QRegularExpression::CaseInsensitiveOption);
sanitizedNameMention = QRegularExpression("\\b" + QRegularExpression::escape(sanename) + "\\b",
QRegularExpression::CaseInsensitiveOption);
}
MessageProcessor::MessageProcessor(const MessageProcessor::SharedParams& sharedParams)
@ -73,12 +75,15 @@ Message MessageProcessor::processIncomingMessage(bool isAction, QString const& m
auto sanitizedNameMention = sharedParams.GetSanitizedNameMention();
for (auto const& mention : {nameMention, sanitizedNameMention}) {
if (mention.indexIn(ret.content) == -1) {
auto matchIt = mention.globalMatch(ret.content);
if (!matchIt.hasNext()) {
continue;
}
auto pos = static_cast<size_t>(mention.pos(0));
auto length = static_cast<size_t>(mention.matchedLength());
auto match = matchIt.next();
auto pos = static_cast<size_t>(match.capturedStart());
auto length = static_cast<size_t>(match.capturedLength());
ret.metadata.push_back({MessageMetadataType::selfMention, pos, pos + length});
break;

View File

@ -21,6 +21,7 @@
#define MESSAGE_H
#include <QDateTime>
#include <QRegularExpression>
#include <QString>
#include <vector>
@ -66,19 +67,19 @@ public:
{
public:
QRegExp GetNameMention() const
QRegularExpression GetNameMention() const
{
return nameMention;
}
QRegExp GetSanitizedNameMention() const
QRegularExpression GetSanitizedNameMention() const
{
return sanitizedNameMention;
}
void onUserNameSet(const QString& username);
private:
QRegExp nameMention;
QRegExp sanitizedNameMention;
QRegularExpression nameMention;
QRegularExpression sanitizedNameMention;
};
MessageProcessor(const SharedParams& sharedParams);