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

feat: add function for generating a filter for search word only

This commit is contained in:
TriKriSta 2018-07-08 12:33:37 +03:00
parent 60a2c5e651
commit 17a97f1ff6
3 changed files with 31 additions and 9 deletions

View File

@ -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];

View File

@ -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<int, int> 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<int, int> 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);

View File

@ -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