From 17a97f1ff68af50588012bf3b9b31a61cdf2b194 Mon Sep 17 00:00:00 2001 From: TriKriSta Date: Sun, 8 Jul 2018 12:33:37 +0300 Subject: [PATCH] feat: add function for generating a filter for search word only --- src/persistence/history.cpp | 6 +++--- src/widget/form/genericchatform.cpp | 12 ++++++------ src/widget/searchtypes.h | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/persistence/history.cpp b/src/persistence/history.cpp index 2eb5bffca..6daf32a37 100644 --- a/src/persistence/history.cpp +++ b/src/persistence/history.cpp @@ -339,10 +339,10 @@ QDateTime History::getDateWhereFindPhrase(const QString& friendPk, const QDateTi message = QStringLiteral("message LIKE '%%1%'").arg(phrase); break; case FilterSearch::WordsOnly: - message = QStringLiteral("message REGEXP '\\b%1\\b'").arg(phrase.toLower()); + message = QStringLiteral("message REGEXP '%1'").arg(SearchExtraFunctions::generateFilterWordsOnly(phrase).toLower()); break; case FilterSearch::RegisterAndWordsOnly: - message = QStringLiteral("REGEXPSENSITIVE(message, '\\b%1\\b')").arg(phrase); + message = QStringLiteral("REGEXPSENSITIVE(message, '%1')").arg(SearchExtraFunctions::generateFilterWordsOnly(phrase)); break; case FilterSearch::Regular: message = QStringLiteral("message REGEXP '%1'").arg(phrase); @@ -388,7 +388,7 @@ QDateTime History::getDateWhereFindPhrase(const QString& friendPk, const QDateTi .arg(message) .arg(period); - + qDebug() << "mes:" << queryText; db->execNow({queryText, rowCallback}); if (!counts.isEmpty()) { return counts[0]; diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index 911db2a13..6007ea357 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -640,11 +640,11 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch& find = txt.contains(phrase, Qt::CaseSensitive); break; case FilterSearch::WordsOnly: - exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flagIns); + exp = QRegularExpression(SearchExtraFunctions::generateFilterWordsOnly(phrase), flagIns); find = txt.contains(exp); break; case FilterSearch::RegisterAndWordsOnly: - exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flag); + exp = QRegularExpression(SearchExtraFunctions::generateFilterWordsOnly(phrase), flag); find = txt.contains(exp); break; case FilterSearch::RegisterAndRegular: @@ -707,10 +707,10 @@ std::pair GenericChatForm::indexForSearchInLine(const QString& txt, co index = txt.lastIndexOf(phrase, startIndex, Qt::CaseSensitive); break; case FilterSearch::WordsOnly: - exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flagIns); + exp = QRegularExpression(SearchExtraFunctions::generateFilterWordsOnly(phrase), flagIns); break; case FilterSearch::RegisterAndWordsOnly: - exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flag); + exp = QRegularExpression(SearchExtraFunctions::generateFilterWordsOnly(phrase), flag); break; case FilterSearch::RegisterAndRegular: exp = QRegularExpression(phrase, flag); @@ -754,10 +754,10 @@ std::pair GenericChatForm::indexForSearchInLine(const QString& txt, co index = txt.indexOf(phrase, startIndex, Qt::CaseSensitive); break; case FilterSearch::WordsOnly: - exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flagIns); + exp = QRegularExpression(SearchExtraFunctions::generateFilterWordsOnly(phrase), flagIns); break; case FilterSearch::RegisterAndWordsOnly: - exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flag); + exp = QRegularExpression(SearchExtraFunctions::generateFilterWordsOnly(phrase), flag); break; case FilterSearch::RegisterAndRegular: exp = QRegularExpression(phrase, flag); diff --git a/src/widget/searchtypes.h b/src/widget/searchtypes.h index 4f3b70b74..dfceb442f 100644 --- a/src/widget/searchtypes.h +++ b/src/widget/searchtypes.h @@ -53,4 +53,26 @@ struct ParameterSearch { } }; +class SearchExtraFunctions { +public: + static QString generateFilterWordsOnly(const QString &phrase) { + QString filter = phrase; + + if (filter.contains("\\")) { + filter.replace("\\", "\\\\"); + + if (filter.front() != '\\') { + filter = "\\b" + filter; + } + if (filter.back() != '\\') { + filter += "\\b"; + } + } else { + filter = QStringLiteral("\\b%1\\b").arg(filter); + } + + return filter; + }; +}; + #endif //SEARCHTYPES_H