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); message = QStringLiteral("message LIKE '%%1%'").arg(phrase);
break; break;
case FilterSearch::WordsOnly: case FilterSearch::WordsOnly:
message = QStringLiteral("message REGEXP '\\b%1\\b'").arg(phrase.toLower()); message = QStringLiteral("message REGEXP '%1'").arg(SearchExtraFunctions::generateFilterWordsOnly(phrase).toLower());
break; break;
case FilterSearch::RegisterAndWordsOnly: case FilterSearch::RegisterAndWordsOnly:
message = QStringLiteral("REGEXPSENSITIVE(message, '\\b%1\\b')").arg(phrase); message = QStringLiteral("REGEXPSENSITIVE(message, '%1')").arg(SearchExtraFunctions::generateFilterWordsOnly(phrase));
break; break;
case FilterSearch::Regular: case FilterSearch::Regular:
message = QStringLiteral("message REGEXP '%1'").arg(phrase); message = QStringLiteral("message REGEXP '%1'").arg(phrase);
@ -388,7 +388,7 @@ QDateTime History::getDateWhereFindPhrase(const QString& friendPk, const QDateTi
.arg(message) .arg(message)
.arg(period); .arg(period);
qDebug() << "mes:" << queryText;
db->execNow({queryText, rowCallback}); db->execNow({queryText, rowCallback});
if (!counts.isEmpty()) { if (!counts.isEmpty()) {
return counts[0]; return counts[0];

View File

@ -640,11 +640,11 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch&
find = txt.contains(phrase, Qt::CaseSensitive); find = txt.contains(phrase, Qt::CaseSensitive);
break; break;
case FilterSearch::WordsOnly: case FilterSearch::WordsOnly:
exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flagIns); exp = QRegularExpression(SearchExtraFunctions::generateFilterWordsOnly(phrase), flagIns);
find = txt.contains(exp); find = txt.contains(exp);
break; break;
case FilterSearch::RegisterAndWordsOnly: case FilterSearch::RegisterAndWordsOnly:
exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flag); exp = QRegularExpression(SearchExtraFunctions::generateFilterWordsOnly(phrase), flag);
find = txt.contains(exp); find = txt.contains(exp);
break; break;
case FilterSearch::RegisterAndRegular: case FilterSearch::RegisterAndRegular:
@ -707,10 +707,10 @@ std::pair<int, int> GenericChatForm::indexForSearchInLine(const QString& txt, co
index = txt.lastIndexOf(phrase, startIndex, Qt::CaseSensitive); index = txt.lastIndexOf(phrase, startIndex, Qt::CaseSensitive);
break; break;
case FilterSearch::WordsOnly: case FilterSearch::WordsOnly:
exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flagIns); exp = QRegularExpression(SearchExtraFunctions::generateFilterWordsOnly(phrase), flagIns);
break; break;
case FilterSearch::RegisterAndWordsOnly: case FilterSearch::RegisterAndWordsOnly:
exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flag); exp = QRegularExpression(SearchExtraFunctions::generateFilterWordsOnly(phrase), flag);
break; break;
case FilterSearch::RegisterAndRegular: case FilterSearch::RegisterAndRegular:
exp = QRegularExpression(phrase, flag); 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); index = txt.indexOf(phrase, startIndex, Qt::CaseSensitive);
break; break;
case FilterSearch::WordsOnly: case FilterSearch::WordsOnly:
exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flagIns); exp = QRegularExpression(SearchExtraFunctions::generateFilterWordsOnly(phrase), flagIns);
break; break;
case FilterSearch::RegisterAndWordsOnly: case FilterSearch::RegisterAndWordsOnly:
exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flag); exp = QRegularExpression(SearchExtraFunctions::generateFilterWordsOnly(phrase), flag);
break; break;
case FilterSearch::RegisterAndRegular: case FilterSearch::RegisterAndRegular:
exp = QRegularExpression(phrase, flag); 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 #endif //SEARCHTYPES_H