mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor: change QRegExp on QRegularExpression for some search functions
This commit is contained in:
parent
74468fde32
commit
60a2c5e651
|
@ -71,7 +71,7 @@ void Text::selectText(const QString& txt, const std::pair<int, int>& point)
|
||||||
selectText(cursor, point);
|
selectText(cursor, point);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Text::selectText(const QRegExp &exp, const std::pair<int, int>& point)
|
void Text::selectText(const QRegularExpression &exp, const std::pair<int, int>& point)
|
||||||
{
|
{
|
||||||
regenerate();
|
regenerate();
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ public:
|
||||||
|
|
||||||
void setText(const QString& txt);
|
void setText(const QString& txt);
|
||||||
void selectText(const QString& txt, const std::pair<int, int>& point);
|
void selectText(const QString& txt, const std::pair<int, int>& point);
|
||||||
void selectText(const QRegExp& exp, const std::pair<int, int>& point);
|
void selectText(const QRegularExpression& exp, const std::pair<int, int>& point);
|
||||||
void deselectText();
|
void deselectText();
|
||||||
|
|
||||||
virtual void setWidth(qreal width) final;
|
virtual void setWidth(qreal width) final;
|
||||||
|
|
|
@ -28,7 +28,6 @@
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QMetaObject>
|
#include <QMetaObject>
|
||||||
#include <QMutexLocker>
|
#include <QMutexLocker>
|
||||||
#include <QRegularExpression>
|
|
||||||
|
|
||||||
/// The two following defines are required to use SQLCipher
|
/// The two following defines are required to use SQLCipher
|
||||||
/// They are used by the sqlite3.h header
|
/// They are used by the sqlite3.h header
|
||||||
|
@ -726,7 +725,7 @@ QVariant RawDatabase::extractData(sqlite3_stmt* stmt, int col)
|
||||||
*/
|
*/
|
||||||
void RawDatabase::regexpInsensitive(sqlite3_context* ctx, int argc, sqlite3_value** argv)
|
void RawDatabase::regexpInsensitive(sqlite3_context* ctx, int argc, sqlite3_value** argv)
|
||||||
{
|
{
|
||||||
regexp(ctx, argc, argv, Qt::CaseInsensitive);
|
regexp(ctx, argc, argv, QRegularExpression::CaseInsensitiveOption | QRegularExpression::UseUnicodePropertiesOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -737,17 +736,17 @@ void RawDatabase::regexpInsensitive(sqlite3_context* ctx, int argc, sqlite3_valu
|
||||||
*/
|
*/
|
||||||
void RawDatabase::regexpSensitive(sqlite3_context* ctx, int argc, sqlite3_value** argv)
|
void RawDatabase::regexpSensitive(sqlite3_context* ctx, int argc, sqlite3_value** argv)
|
||||||
{
|
{
|
||||||
regexp(ctx, argc, argv, Qt::CaseSensitive);
|
regexp(ctx, argc, argv, QRegularExpression::UseUnicodePropertiesOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RawDatabase::regexp(sqlite3_context* ctx, int argc, sqlite3_value** argv, const Qt::CaseSensitivity cs)
|
void RawDatabase::regexp(sqlite3_context* ctx, int argc, sqlite3_value** argv, const QRegularExpression::PatternOptions cs)
|
||||||
{
|
{
|
||||||
QRegExp regex;
|
QRegularExpression regex;
|
||||||
QString str1((const char*)sqlite3_value_text(argv[0]));
|
QString str1((const char*)sqlite3_value_text(argv[0]));
|
||||||
QString str2((const char*)sqlite3_value_text(argv[1]));
|
QString str2((const char*)sqlite3_value_text(argv[1]));
|
||||||
|
|
||||||
regex.setPattern(str1);
|
regex.setPattern(str1);
|
||||||
regex.setCaseSensitivity(cs);
|
regex.setPatternOptions(cs);
|
||||||
|
|
||||||
bool b = str2.contains(regex);
|
bool b = str2.contains(regex);
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
#include <QRegularExpression>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
@ -91,7 +92,7 @@ protected:
|
||||||
static void regexpSensitive(sqlite3_context* ctx, int argc, sqlite3_value** argv);
|
static void regexpSensitive(sqlite3_context* ctx, int argc, sqlite3_value** argv);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void regexp(sqlite3_context* ctx, int argc, sqlite3_value** argv, const Qt::CaseSensitivity cs);
|
static void regexp(sqlite3_context* ctx, int argc, sqlite3_value** argv, const QRegularExpression::PatternOptions cs);
|
||||||
|
|
||||||
struct Transaction
|
struct Transaction
|
||||||
{
|
{
|
||||||
|
|
|
@ -537,7 +537,14 @@ void ChatForm::onSearchUp(const QString& phrase, const ParameterSearch& paramete
|
||||||
QVector<ChatLine::Ptr> lines = chatWidget->getLines();
|
QVector<ChatLine::Ptr> lines = chatWidget->getLines();
|
||||||
int numLines = lines.size();
|
int numLines = lines.size();
|
||||||
|
|
||||||
int startLine = numLines - searchPoint.x();
|
int startLine;
|
||||||
|
|
||||||
|
if (searchAfterLoadHistory) {
|
||||||
|
startLine = 1;
|
||||||
|
searchAfterLoadHistory = false;
|
||||||
|
} else {
|
||||||
|
startLine = numLines - searchPoint.x();
|
||||||
|
}
|
||||||
|
|
||||||
if (startLine == 0 && loadHistory(phrase, parameter)) {
|
if (startLine == 0 && loadHistory(phrase, parameter)) {
|
||||||
return;
|
return;
|
||||||
|
@ -746,7 +753,7 @@ void ChatForm::loadHistoryDefaultNum(bool processUndelivered)
|
||||||
QString pk = f->getPublicKey().toString();
|
QString pk = f->getPublicKey().toString();
|
||||||
QList<History::HistMessage> msgs = history->getChatHistoryDefaultNum(pk);
|
QList<History::HistMessage> msgs = history->getChatHistoryDefaultNum(pk);
|
||||||
if (!msgs.isEmpty()) {
|
if (!msgs.isEmpty()) {
|
||||||
earliestMessage = msgs.back().timestamp;
|
earliestMessage = msgs.first().timestamp;
|
||||||
}
|
}
|
||||||
handleLoadedMessages(msgs, processUndelivered);
|
handleLoadedMessages(msgs, processUndelivered);
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,7 @@
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
#include <QKeyEvent>
|
#include <QKeyEvent>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QRegExp>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class GenericChatForm
|
* @class GenericChatForm
|
||||||
|
@ -585,8 +585,6 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch&
|
||||||
|
|
||||||
int numLines = lines.size();
|
int numLines = lines.size();
|
||||||
|
|
||||||
auto d = QDate::currentDate();
|
|
||||||
|
|
||||||
int startLine = numLines - searchPoint.x();
|
int startLine = numLines - searchPoint.x();
|
||||||
if (parameter.period == PeriodSearch::WithTheFirst) {
|
if (parameter.period == PeriodSearch::WithTheFirst) {
|
||||||
startLine = 0;
|
startLine = 0;
|
||||||
|
@ -632,25 +630,29 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch&
|
||||||
QString txt = content->getText();
|
QString txt = content->getText();
|
||||||
|
|
||||||
bool find = false;
|
bool find = false;
|
||||||
QRegExp exp;
|
QRegularExpression exp;
|
||||||
|
QRegularExpressionMatch match;
|
||||||
|
|
||||||
|
auto flagIns = QRegularExpression::CaseInsensitiveOption | QRegularExpression::UseUnicodePropertiesOption;
|
||||||
|
auto flag = QRegularExpression::UseUnicodePropertiesOption;
|
||||||
switch (parameter.filter) {
|
switch (parameter.filter) {
|
||||||
case FilterSearch::Register:
|
case FilterSearch::Register:
|
||||||
find = txt.contains(phrase, Qt::CaseSensitive);
|
find = txt.contains(phrase, Qt::CaseSensitive);
|
||||||
break;
|
break;
|
||||||
case FilterSearch::WordsOnly:
|
case FilterSearch::WordsOnly:
|
||||||
exp = QRegExp(QString("\\b%1\\b").arg(phrase), Qt::CaseInsensitive);
|
exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flagIns);
|
||||||
find = txt.contains(exp);
|
find = txt.contains(exp);
|
||||||
break;
|
break;
|
||||||
case FilterSearch::RegisterAndWordsOnly:
|
case FilterSearch::RegisterAndWordsOnly:
|
||||||
exp = QRegExp(QString("\\b%1\\b").arg(phrase));
|
exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flag);
|
||||||
find = txt.contains(exp);
|
find = txt.contains(exp);
|
||||||
break;
|
break;
|
||||||
case FilterSearch::RegisterAndRegular:
|
case FilterSearch::RegisterAndRegular:
|
||||||
exp = QRegExp(phrase);
|
exp = QRegularExpression(phrase, flag);
|
||||||
find = txt.contains(exp);
|
find = txt.contains(exp);
|
||||||
break;
|
break;
|
||||||
case FilterSearch::Regular:
|
case FilterSearch::Regular:
|
||||||
exp = QRegExp(phrase, Qt::CaseInsensitive);
|
exp = QRegularExpression(phrase, flagIns);
|
||||||
find = txt.contains(exp);
|
find = txt.contains(exp);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -670,7 +672,7 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch&
|
||||||
chatWidget->scrollToLine(l);
|
chatWidget->scrollToLine(l);
|
||||||
text->deselectText();
|
text->deselectText();
|
||||||
|
|
||||||
if (exp.isEmpty()) {
|
if (exp.pattern().isEmpty()) {
|
||||||
text->selectText(phrase, point);
|
text->selectText(phrase, point);
|
||||||
} else {
|
} else {
|
||||||
text->selectText(exp, point);
|
text->selectText(exp, point);
|
||||||
|
@ -688,9 +690,12 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch&
|
||||||
|
|
||||||
std::pair<int, int> GenericChatForm::indexForSearchInLine(const QString& txt, const QString& phrase, const ParameterSearch& parameter, bool searchUp)
|
std::pair<int, int> GenericChatForm::indexForSearchInLine(const QString& txt, const QString& phrase, const ParameterSearch& parameter, bool searchUp)
|
||||||
{
|
{
|
||||||
int index = 0;
|
int index = -1;
|
||||||
QRegExp exp;
|
int size = 0;
|
||||||
|
|
||||||
|
QRegularExpression exp;
|
||||||
|
auto flagIns = QRegularExpression::CaseInsensitiveOption | QRegularExpression::UseUnicodePropertiesOption;
|
||||||
|
auto flag = QRegularExpression::UseUnicodePropertiesOption;
|
||||||
if (searchUp) {
|
if (searchUp) {
|
||||||
int startIndex = -1;
|
int startIndex = -1;
|
||||||
if (searchPoint.y() > -1) {
|
if (searchPoint.y() > -1) {
|
||||||
|
@ -702,25 +707,42 @@ 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 = QRegExp(QString("\\b%1\\b").arg(phrase), Qt::CaseInsensitive);
|
exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flagIns);
|
||||||
index = exp.lastIndexIn(txt, startIndex);
|
|
||||||
break;
|
break;
|
||||||
case FilterSearch::RegisterAndWordsOnly:
|
case FilterSearch::RegisterAndWordsOnly:
|
||||||
exp = QRegExp(QString("\\b%1\\b").arg(phrase));
|
exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flag);
|
||||||
index = exp.lastIndexIn(txt, startIndex);
|
|
||||||
break;
|
break;
|
||||||
case FilterSearch::RegisterAndRegular:
|
case FilterSearch::RegisterAndRegular:
|
||||||
exp = QRegExp(phrase);
|
exp = QRegularExpression(phrase, flag);
|
||||||
index = exp.lastIndexIn(txt, startIndex);
|
|
||||||
break;
|
break;
|
||||||
case FilterSearch::Regular:
|
case FilterSearch::Regular:
|
||||||
exp = QRegExp(phrase, Qt::CaseInsensitive);
|
exp = QRegularExpression(phrase, flagIns);
|
||||||
index = exp.lastIndexIn(txt, startIndex);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
index = txt.lastIndexOf(phrase, startIndex, Qt::CaseInsensitive);
|
index = txt.lastIndexOf(phrase, startIndex, Qt::CaseInsensitive);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!exp.pattern().isEmpty()) {
|
||||||
|
auto matchIt = exp.globalMatch(txt);
|
||||||
|
|
||||||
|
while (matchIt.hasNext()) {
|
||||||
|
auto match = matchIt.next();
|
||||||
|
|
||||||
|
int sizeItem = match.capturedLength();
|
||||||
|
int indexItem = match.capturedStart();
|
||||||
|
|
||||||
|
if (startIndex == -1 || indexItem < startIndex) {
|
||||||
|
index = indexItem;
|
||||||
|
size = sizeItem;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
size = phrase.size();
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
int startIndex = 0;
|
int startIndex = 0;
|
||||||
if (searchPoint.y() > -1) {
|
if (searchPoint.y() > -1) {
|
||||||
|
@ -732,37 +754,30 @@ 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 = QRegExp(QString("\\b%1\\b").arg(phrase), Qt::CaseInsensitive);
|
exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flagIns);
|
||||||
index = exp.indexIn(txt, startIndex);
|
|
||||||
break;
|
break;
|
||||||
case FilterSearch::RegisterAndWordsOnly:
|
case FilterSearch::RegisterAndWordsOnly:
|
||||||
exp = QRegExp(QString("\\b%1\\b").arg(phrase));
|
exp = QRegularExpression(QString("\\b%1\\b").arg(phrase), flag);
|
||||||
index = exp.indexIn(txt, startIndex);
|
|
||||||
break;
|
break;
|
||||||
case FilterSearch::RegisterAndRegular:
|
case FilterSearch::RegisterAndRegular:
|
||||||
exp = QRegExp(phrase);
|
exp = QRegularExpression(phrase, flag);
|
||||||
index = exp.indexIn(txt, startIndex);
|
|
||||||
break;
|
break;
|
||||||
case FilterSearch::Regular:
|
case FilterSearch::Regular:
|
||||||
exp = QRegExp(phrase, Qt::CaseInsensitive);
|
exp = QRegularExpression(phrase, flagIns);
|
||||||
index = exp.indexIn(txt, startIndex);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
index = txt.indexOf(phrase, startIndex, Qt::CaseInsensitive);
|
index = txt.indexOf(phrase, startIndex, Qt::CaseInsensitive);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int size = 0;
|
if (!exp.pattern().isEmpty()) {
|
||||||
if (index > -1) {
|
auto match = exp.match(txt, startIndex);
|
||||||
if (exp.isEmpty()) {
|
if (match.hasMatch()) {
|
||||||
size = phrase.size();
|
size = match.capturedLength(0);
|
||||||
} else {
|
index = match.capturedEnd() - size;
|
||||||
auto lExp = exp.capturedTexts();
|
|
||||||
|
|
||||||
if (!lExp.isEmpty()) {
|
|
||||||
size = lExp[0].size();
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
size = phrase.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -936,9 +951,8 @@ void GenericChatForm::onContinueSearch()
|
||||||
QString phrase = searchForm->getSearchPhrase();
|
QString phrase = searchForm->getSearchPhrase();
|
||||||
ParameterSearch parameter = searchForm->getParameterSearch();
|
ParameterSearch parameter = searchForm->getParameterSearch();
|
||||||
if (!phrase.isEmpty() && searchAfterLoadHistory) {
|
if (!phrase.isEmpty() && searchAfterLoadHistory) {
|
||||||
searchAfterLoadHistory = false;
|
|
||||||
|
|
||||||
if (parameter.period == PeriodSearch::WithTheFirst || parameter.period == PeriodSearch::AfterDate) {
|
if (parameter.period == PeriodSearch::WithTheFirst || parameter.period == PeriodSearch::AfterDate) {
|
||||||
|
searchAfterLoadHistory = false;
|
||||||
onSearchDown(phrase, parameter);
|
onSearchDown(phrase, parameter);
|
||||||
} else {
|
} else {
|
||||||
onSearchUp(phrase, parameter);
|
onSearchUp(phrase, parameter);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user