Skip to content

Commit

Permalink
Convert from QRegExp to QRegularExpression
Browse files Browse the repository at this point in the history
QRegExp is deprecated in Qt6, so make the change in prevision of a
future update.
  • Loading branch information
Gnurou committed Mar 16, 2024
1 parent 9068167 commit e9877ed
Show file tree
Hide file tree
Showing 25 changed files with 205 additions and 148 deletions.
10 changes: 3 additions & 7 deletions src/core/EntrySearcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@
*/

#include "core/EntrySearcher.h"
#include "core/EntryListCache.h"
#include "core/RelativeDate.h"

#include <QtDebug>

#include <QRegExp>
#include <QStringList>
#include <QtDebug>

PreferenceItem<bool> EntrySearcher::allowRomajiSearch("", "allowRomajiSearch", false);

EntrySearcher::EntrySearcher(EntryType entryType)
: commandMatch(SearchCommand::commandMatch().pattern()), _entryType(entryType) {
EntrySearcher::EntrySearcher(EntryType entryType) : _entryType(entryType) {
QueryBuilder::Join::addTablePriority("training", -100);
QueryBuilder::Join::addTablePriority("notes", -40);
QueryBuilder::Join::addTablePriority("notesText", -45);
Expand Down Expand Up @@ -211,7 +207,7 @@ QueryBuilder::Column EntrySearcher::canSort(const QString &sort,
bool EntrySearcher::searchToCommands(const QStringList &searches,
QList<SearchCommand> &commands) const {
foreach (const QString &search, searches) {
if (commandMatch.exactMatch(search)) {
if (SearchCommand::commandMatch().match(search).hasMatch()) {
SearchCommand command = SearchCommand::fromString(search);
if (validCommands.contains(command.command()))
commands << command;
Expand Down
2 changes: 1 addition & 1 deletion src/core/EntrySearcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "core/SearchCommand.h"

#include <QList>
#include <QRegularExpression>
#include <QStringList>

/**
Expand All @@ -32,7 +33,6 @@
*/
class EntrySearcher {
private:
QRegExp commandMatch;
EntryType _entryType;

protected:
Expand Down
21 changes: 13 additions & 8 deletions src/core/EntrySearcherManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@

#include "core/EntrySearcherManager.h"
#include "core/Preferences.h"
#include <qregularexpression.h>

EntrySearcherManager *EntrySearcherManager::_instance = 0;
PreferenceItem<bool> EntrySearcherManager::studiedEntriesFirst("mainWindow/resultsView",
"studiedEntriesFirst", true);

const int resultsPerPage = 50;

EntrySearcherManager::EntrySearcherManager()
: quotedWordsMatch(SearchCommand::quotedWordsMatch().pattern()),
validSearchCompoundMatch(SearchCommand::commandMatch().pattern() + "|" +
Expand All @@ -34,17 +33,23 @@ EntrySearcherManager::EntrySearcherManager()
}

QStringList EntrySearcherManager::splitSearchString(const QString &searchString) {
if (!validSearchMatch.exactMatch(searchString))
if (!validSearchMatch
.match(searchString, 0, QRegularExpression::NormalMatch,
QRegularExpression::AnchoredMatchOption)
.hasMatch())
return QStringList();
int pos = 0;
QStringList res;
while ((pos = validSearchCompoundMatch.indexIn(searchString, pos)) != -1) {
QString compound = validSearchCompoundMatch.cap();
QRegularExpressionMatchIterator matches = validSearchCompoundMatch.globalMatch(searchString);
while (matches.hasNext()) {
QRegularExpressionMatch match = matches.next();
QString compound = match.captured();
// Special handling for quoted words
if (quotedWordsMatch.exactMatch(compound))
if (quotedWordsMatch
.match(compound, 0, QRegularExpression::NormalMatch,
QRegularExpression::AnchoredMatchOption)
.hasMatch())
compound.remove(compound.size() - 1, 1).remove(0, 1);
res << compound;
pos += validSearchCompoundMatch.matchedLength();
}
return res;
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/EntrySearcherManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
#include "core/Preferences.h"
#include "core/QueryBuilder.h"

#include <QRegExp>
#include <QRegularExpression>

class EntrySearcherManager {
private:
static EntrySearcherManager *_instance;
QList<EntrySearcher *> _instances;

QRegExp quotedWordsMatch;
QRegExp validSearchCompoundMatch;
QRegExp validSearchMatch;
QRegularExpression quotedWordsMatch;
QRegularExpression validSearchCompoundMatch;
QRegularExpression validSearchMatch;

public:
EntrySearcherManager();
Expand Down
2 changes: 1 addition & 1 deletion src/core/QueryBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void QueryBuilder::Statement::autoJoin() {
// Now do the same for where statements
// TODO would be nice to analyze where statements as well!
/* foreach (const Where &where, wheres()) {
QRegExp regexp("[a-zA-Z][\w.]*\w* *[^\\(]");
QRegularExpression regexp("[a-zA-Z][\w.]*\w* *[^\\(]");
}*/
}

Expand Down
38 changes: 26 additions & 12 deletions src/core/RelativeDate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "core/RelativeDate.h"
#include "core/Preferences.h"

#include <QRegExp>
#include <QRegularExpression>

PreferenceItem<int> RelativeDate::firstDayOfWeek("", "firstDayOfWeek", Qt::Monday);

Expand Down Expand Up @@ -61,16 +61,23 @@ QString RelativeDate::dateString() const {
}

void RelativeDate::setDateString(const QString &date) {
static QRegExp isoDatePattern("\\d\\d\\d\\d-\\d\\d-\\d\\d");
static QRegExp relativeDatePattern("(\\d+) (days|weeks|months|years) ago");
static QRegExp naturalReadingPattern("(this|last) (week|month|year)");
static QRegularExpression isoDatePattern(
QRegularExpression::anchoredPattern("\\d\\d\\d\\d-\\d\\d-\\d\\d"));
static QRegularExpression relativeDatePattern(
QRegularExpression::anchoredPattern("(\\d+) (days|weeks|months|years) ago"));
static QRegularExpression naturalReadingPattern(
QRegularExpression::anchoredPattern("(this|last) (week|month|year)"));

if (isoDatePattern.exactMatch(date)) {
if (isoDatePattern.match(date).hasMatch()) {
setDateType(AbsoluteDate);
setAbsoluteDate(QDate::fromString(date, Qt::ISODate));
} else if (naturalReadingPattern.exactMatch(date)) {
QString when(naturalReadingPattern.cap(1));
QString what(naturalReadingPattern.cap(2));
return;
}

QRegularExpressionMatch match = naturalReadingPattern.match(date);
if (match.hasMatch()) {
QString when(match.captured(1));
QString what(match.captured(2));
if (when == "this")
setAgo(0);
else
Expand All @@ -81,9 +88,13 @@ void RelativeDate::setDateString(const QString &date) {
setDateType(MonthsAgo);
else if (what == "year")
setDateType(YearsAgo);
} else if (relativeDatePattern.exactMatch(date)) {
setAgo(relativeDatePattern.cap(1).toInt());
QString what(relativeDatePattern.cap(2));
return;
}

match = relativeDatePattern.match(date);
if (match.hasMatch()) {
setAgo(match.captured(1).toInt());
QString what(match.captured(2));
if (what == "days")
setDateType(DaysAgo);
else if (what == "weeks")
Expand All @@ -92,7 +103,10 @@ void RelativeDate::setDateString(const QString &date) {
setDateType(MonthsAgo);
else if (what == "years")
setDateType(YearsAgo);
} else if (date == "today" || date == "yesterday") {
return;
}

if (date == "today" || date == "yesterday") {
setDateType(DaysAgo);
if (date == "today")
setAgo(0);
Expand Down
34 changes: 21 additions & 13 deletions src/core/SearchCommand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,37 @@ static QString singleWordString = "(-?[\\w\\*\\?\\-\\._:]+)";
static QString quotedWordsString = "\"([^\"]*)\"";
static QString argString = singleWordString + "|" + quotedWordsString;

QRegExp SearchCommand::_singleWordMatch(singleWordString);
QRegExp SearchCommand::_quotedWordsMatch(quotedWordsString);
QRegExp SearchCommand::_argMatch(argString);
QRegExp SearchCommand::_commandMatch(":(\\w+)(?:=(?:(?:" + argString +
QString::fromUtf8(")(?:[,、](?:") + argString + "))*))?");
QRegularExpression SearchCommand::_singleWordMatch(singleWordString);
QRegularExpression SearchCommand::_quotedWordsMatch(quotedWordsString);
QRegularExpression SearchCommand::_argMatch(argString);
QRegularExpression SearchCommand::_commandMatch(":(\\w+)(?:=(?:(?:" + argString +
QString::fromUtf8(")(?:[,、](?:") + argString +
"))*))?");

SearchCommand SearchCommand::fromString(const QString &string) {
if (!_commandMatch.exactMatch(string)) {
QRegularExpressionMatch match = _commandMatch.match(string, 0, QRegularExpression::NormalMatch,
QRegularExpression::AnchoredMatchOption);

if (!match.hasMatch()) {
qDebug("Cannot match command string!");
return SearchCommand::invalid();
}

QStringList caps;
SearchCommand ret(_commandMatch.cap(1));
SearchCommand ret(match.captured(1));
int pos = string.indexOf('=');
if (pos != -1)
while ((pos = _argMatch.indexIn(string, pos)) != -1) {
if (pos != -1) {
QRegularExpressionMatchIterator i = _argMatch.globalMatch(string.mid(pos));

while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
// Whether we matched a single word or a string
if (_argMatch.cap(1) == "")
ret.addArgument(_argMatch.cap(2));
if (match.captured(1) == "")
ret.addArgument(match.captured(2));
else
ret.addArgument(_argMatch.cap(1));
pos += _argMatch.matchedLength();
ret.addArgument(match.captured(1));
}
}
return ret;
}

Expand Down
18 changes: 9 additions & 9 deletions src/core/SearchCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef __CORE_SEARCHCOMMAND_H
#define __CORE_SEARCHCOMMAND_H

#include <QRegExp>
#include <QRegularExpression>
#include <QStringList>

/**
Expand All @@ -31,16 +31,16 @@ class SearchCommand {
QStringList _args;
static SearchCommand _invalid;

static QRegExp _singleWordMatch;
static QRegExp _quotedWordsMatch;
static QRegExp _argMatch;
static QRegExp _commandMatch;
static QRegularExpression _singleWordMatch;
static QRegularExpression _quotedWordsMatch;
static QRegularExpression _argMatch;
static QRegularExpression _commandMatch;

public:
static const QRegExp singleWordMatch() { return _singleWordMatch; }
static const QRegExp quotedWordsMatch() { return _quotedWordsMatch; }
static const QRegExp argMatch() { return _argMatch; }
static const QRegExp commandMatch() { return _commandMatch; }
static const QRegularExpression &singleWordMatch() { return _singleWordMatch; }
static const QRegularExpression &quotedWordsMatch() { return _quotedWordsMatch; }
static const QRegularExpression &argMatch() { return _argMatch; }
static const QRegularExpression &commandMatch() { return _commandMatch; }

public:
SearchCommand() {}
Expand Down
5 changes: 4 additions & 1 deletion src/core/Tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QDateTime>
#include <QHash>
#include <QList>
#include <QRegularExpression>
#include <QString>
#include <QStringList>

Expand All @@ -41,7 +42,9 @@ class TagsListModel : public QAbstractListModel {
QVariant data(const QModelIndex &index, int role) const;
bool contains(const QString &str) const { return _data.contains(str, Qt::CaseInsensitive); }
bool containsMatch(const QString &str) const {
return _data.indexOf(QRegExp(str, Qt::CaseInsensitive, QRegExp::Wildcard)) != -1;
return _data.indexOf(
QRegularExpression(QRegularExpression::wildcardToRegularExpression(str),
QRegularExpression::CaseInsensitiveOption)) != -1;
}
const QStringList &contents() const { return _data; }

Expand Down
12 changes: 7 additions & 5 deletions src/core/jmdict/JMdictEntrySearcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "core/TextTools.h"
#include "core/jmdict/JMdictEntry.h"
#include "core/jmdict/JMdictPlugin.h"
#include "sqlite/SQLite.h"

PreferenceItem<QString> JMdictEntrySearcher::miscPropertiesFilter("jmdict", "miscPropertiesFilter",
"arch,obs");
Expand Down Expand Up @@ -64,7 +63,7 @@ SearchCommand JMdictEntrySearcher::commandFromWord(const QString &word) const {

QString checkString(word);
// Remove all wild cards
checkString.remove(QRegExp("[\\?\\*]"));
checkString.remove(QRegularExpression("[\\?\\*]"));
// Only wildcards? Lets assume romaji
if (checkString.size() == 0)
return SearchCommand::fromString(QString(":mean=\"%1\"").arg(word));
Expand All @@ -88,7 +87,7 @@ SearchCommand JMdictEntrySearcher::commandFromWord(const QString &word) const {
}

static QString buildTextSearchCondition(const QStringList &words, const QString &table) {
static QRegExp regExpChars = QRegExp("[\\?\\*]");
static QRegularExpression regExpChars = QRegularExpression("[\\?\\*]");
static QString ftsMatch("jmdict%3.%2Text.reading MATCH '%1'");
static QString regexpMatch("jmdict%3.%2Text.reading REGEXP '%1'");
static QString glossRegexpMatch("{{leftcolumn}} in (select id from jmdict_%2.glosses where "
Expand All @@ -109,8 +108,11 @@ static QString buildTextSearchCondition(const QStringList &words, const QString
// First check if we can optimize by using the FTS index (i.e.
// the first character is not a wildcard)
int wildcardIdx = 0;
while (!regExpChars.exactMatch(w[wildcardIdx]))
QRegularExpressionMatch match = regExpChars.match(w[wildcardIdx]);
while (!match.hasMatch()) {
wildcardIdx++;
match = regExpChars.match(w[wildcardIdx]);
}
if (wildcardIdx != 0)
fts << "\"" + w.mid(0, wildcardIdx) + "*\"";
// If the wildcard we found is the last character and a star,
Expand Down Expand Up @@ -495,4 +497,4 @@ QVector<quint64> JMdictEntrySearcher::miscFilterMask() {

ret.append(miscMask);
return ret;
}
}
12 changes: 8 additions & 4 deletions src/core/jmdict/JMdictParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

#include "core/jmdict/JMdictParser.h"

QRegExp JMdictParser::versionRegExp(" JMdict created: (\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d) ");
QRegularExpression
JMdictParser::versionRegExp(" JMdict created: (\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d) ");

JMdictParser::JMdictParser(const QStringList &langs)
: languages(langs), gotVersion(false), posBitFieldsCount(0), fieldBitFieldsCount(0),
Expand Down Expand Up @@ -182,9 +183,12 @@ bool JMdictParser::parse(QXmlStreamReader &reader) {
// We are not interested in senses for which there is no gloss!
if (sense.gloss.isEmpty()) entry.senses.removeLast();
DONE TAG_POST if (!onItemParsed(entry)) return false;
DONE ENDTAG COMMENT if (!gotVersion && versionRegExp.exactMatch(TEXT)) {
_dictVersion = versionRegExp.capturedTexts()[1];
gotVersion = true;
DONE ENDTAG COMMENT if (!gotVersion) {
QRegularExpressionMatch match = versionRegExp.match(TEXT);
if (match.hasMatch()) {
_dictVersion = match.captured(1);
gotVersion = true;
}
}
DONE DOCUMENT_END
}
6 changes: 2 additions & 4 deletions src/core/jmdict/JMdictParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include <QHash>
#include <QMap>
#include <QRegExp>
#include <QRegularExpression>
#include <QSet>
#include <QStringList>

Expand Down Expand Up @@ -80,9 +80,7 @@ class JMdictDeletedItem {

class JMdictParser {
protected:
static QRegExp versionRegExp;
static QRegExp deletedItemRegExp;
static QRegExp mergedItemRegExp;
static QRegularExpression versionRegExp;

QStringList languages;
bool gotVersion;
Expand Down
Loading

0 comments on commit e9877ed

Please sign in to comment.