From 75bfb8f48ff9aebc8f06fe50ec36427eaf2435cb Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 6 Oct 2022 13:54:20 +0200 Subject: [PATCH] Core: replace QRegExp with QRegularExpression --- src/Gui/Application.cpp | 12 +++--- src/Gui/CallTips.cpp | 8 ++-- src/Gui/CommandPyImp.cpp | 7 ++-- src/Gui/CommandStd.cpp | 10 +++-- src/Gui/DlgCreateNewPreferencePackImp.cpp | 2 + src/Gui/DlgSettingsImageImp.cpp | 27 ++++++++---- src/Gui/FileDialog.cpp | 50 +++++++++++++---------- src/Gui/MDIView.cpp | 16 ++++---- src/Gui/MainWindow.cpp | 11 +++-- src/Gui/Splashscreen.cpp | 14 ++++--- src/Gui/TextEdit.cpp | 4 +- 11 files changed, 99 insertions(+), 62 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index 48c43db412..c5cfd2bd0f 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -32,6 +32,8 @@ # include # include # include +# include +# include # include # include # include @@ -1403,13 +1405,13 @@ bool Application::activateWorkbench(const char* name) catch (Py::Exception&) { Base::PyException e; // extract the Python error text QString msg = QString::fromUtf8(e.what()); - QRegExp rx; + QRegularExpression rx; // ignore '' prefixes rx.setPattern(QLatin1String("^\\s*:\\s*")); - int pos = rx.indexIn(msg); - while ( pos != -1 ) { - msg = msg.mid(rx.matchedLength()); - pos = rx.indexIn(msg); + auto match = rx.match(msg); + while (match.hasMatch()) { + msg = msg.mid(match.capturedLength()); + match = rx.match(msg); } Base::Console().Error("%s\n", (const char*)msg.toUtf8()); diff --git a/src/Gui/CallTips.cpp b/src/Gui/CallTips.cpp index 5046dbdabf..2aa7245a75 100644 --- a/src/Gui/CallTips.cpp +++ b/src/Gui/CallTips.cpp @@ -27,6 +27,8 @@ # include # include # include +# include +# include # include # include #endif @@ -706,9 +708,9 @@ void CallTipsList::callTipItemActivated(QListWidgetItem *item) * Try to find out if call needs arguments. * For this we search the description for appropriate hints ... */ - QRegExp argumentMatcher( QRegExp::escape( callTip.name ) + QLatin1String("\\s*\\(\\s*\\w+.*\\)") ); - argumentMatcher.setMinimal( true ); //< set regex non-greedy! - if (argumentMatcher.indexIn( callTip.description ) != -1) + QRegularExpression argumentMatcher( QRegularExpression::escape( callTip.name ) + QLatin1String("\\s*\\(\\s*\\w+.*\\)") ); + argumentMatcher.setPatternOptions( QRegularExpression::InvertedGreedinessOption ); //< set regex non-greedy! + if (argumentMatcher.match( callTip.description ).hasMatch()) { // if arguments are needed, we just move the cursor one left, to between the parentheses. cursor.movePosition( QTextCursor::Left, QTextCursor::MoveAnchor, 1 ); diff --git a/src/Gui/CommandPyImp.cpp b/src/Gui/CommandPyImp.cpp index 45402d36c0..9f0f2ad4d6 100644 --- a/src/Gui/CommandPyImp.cpp +++ b/src/Gui/CommandPyImp.cpp @@ -23,6 +23,8 @@ #include "PreCompiled.h" #ifndef _PreComp_ # include +# include +# include #endif #include "Command.h" @@ -97,15 +99,14 @@ PyObject* CommandPy::listByShortcut(PyObject *args) if (action) { QString spc = QString::fromLatin1(" "); if (Base::asBoolean(bIsRegularExp)) { - QRegExp re = QRegExp(QString::fromLatin1(shortcut_to_find)); - re.setCaseSensitivity(Qt::CaseInsensitive); + QRegularExpression re(QString::fromLatin1(shortcut_to_find), QRegularExpression::CaseInsensitiveOption); if (!re.isValid()) { std::stringstream str; str << "Invalid regular expression:" << ' ' << shortcut_to_find; throw Py::RuntimeError(str.str()); } - if (re.indexIn(action->shortcut().toString().remove(spc).toUpper()) != -1) { + if (re.match(action->shortcut().toString().remove(spc).toUpper()).hasMatch()) { matches.emplace_back(c->getName()); } } diff --git a/src/Gui/CommandStd.cpp b/src/Gui/CommandStd.cpp index 238996a5bb..72478d003a 100644 --- a/src/Gui/CommandStd.cpp +++ b/src/Gui/CommandStd.cpp @@ -24,6 +24,8 @@ #ifndef _PreComp_ # include # include +# include +# include # include #endif @@ -89,11 +91,11 @@ void StdCmdWorkbench::activated(int i) catch(const Base::PyException& e) { QString msg(QLatin1String(e.what())); // ignore '' prefixes - QRegExp rx; + QRegularExpression rx; rx.setPattern(QLatin1String("^\\s*:\\s*")); - int pos = rx.indexIn(msg); - if (pos != -1) - msg = msg.mid(rx.matchedLength()); + auto match = rx.match(msg); + if (match.hasMatch()) + msg = msg.mid(match.capturedLength()); QMessageBox::critical(getMainWindow(), QObject::tr("Cannot load workbench"), msg); } catch(...) { diff --git a/src/Gui/DlgCreateNewPreferencePackImp.cpp b/src/Gui/DlgCreateNewPreferencePackImp.cpp index 1680305a01..e32efbae5b 100644 --- a/src/Gui/DlgCreateNewPreferencePackImp.cpp +++ b/src/Gui/DlgCreateNewPreferencePackImp.cpp @@ -25,6 +25,8 @@ #ifndef _PreComp_ # include # include +# include +# include #endif #include "DlgCreateNewPreferencePackImp.h" diff --git a/src/Gui/DlgSettingsImageImp.cpp b/src/Gui/DlgSettingsImageImp.cpp index c2b3b7409b..2a2729d766 100644 --- a/src/Gui/DlgSettingsImageImp.cpp +++ b/src/Gui/DlgSettingsImageImp.cpp @@ -21,6 +21,10 @@ ***************************************************************************/ #include "PreCompiled.h" +#ifndef _PreComp_ +# include +# include +#endif #include "DlgSettingsImageImp.h" #include "ui_DlgSettingsImage.h" @@ -215,15 +219,22 @@ void DlgSettingsImageImp::on_standardSizeBox_activated(int index) else { // try to extract from the string QString text = ui->standardSizeBox->itemText(index); - QRegExp rx(QLatin1String("\\b\\d{2,5}\\b")); + QRegularExpression rx(QLatin1String("\\b\\d{2,5}\\b")); int pos = 0; - pos = rx.indexIn(text, pos); - QString w = text.mid(pos, rx.matchedLength()); - ui->spinWidth->setValue(w.toInt()); - pos += rx.matchedLength(); - pos = rx.indexIn(text, pos); - QString h = text.mid(pos, rx.matchedLength()); - ui->spinHeight->setValue(h.toInt()); + auto match = rx.match(text, pos); + if (match.hasMatch()) { + pos = match.capturedStart(); + QString width = text.mid(pos, match.capturedLength()); + ui->spinWidth->setValue(width.toInt()); + pos += match.capturedLength(); + } + + match = rx.match(text, pos); + if (match.hasMatch()) { + pos = match.capturedStart(); + QString height = text.mid(pos, match.capturedLength()); + ui->spinHeight->setValue(height.toInt()); + } } } diff --git a/src/Gui/FileDialog.cpp b/src/Gui/FileDialog.cpp index 36a4f5a0d5..be7023e222 100644 --- a/src/Gui/FileDialog.cpp +++ b/src/Gui/FileDialog.cpp @@ -33,6 +33,8 @@ # include # include # include +# include +# include # include # include # include @@ -85,10 +87,11 @@ FileDialog::~FileDialog() void FileDialog::onSelectedFilter(const QString& /*filter*/) { - QRegExp rx(QLatin1String("\\(\\*.(\\w+)")); + QRegularExpression rx(QLatin1String("\\(\\*.(\\w+)")); QString suf = selectedNameFilter(); - if (rx.indexIn(suf) >= 0) { - suf = rx.cap(1); + auto match = rx.match(suf); + if (match.hasMatch()) { + suf = match.captured(1); setDefaultSuffix(suf); } } @@ -116,11 +119,10 @@ QList FileDialog::fetchSidebarUrls() bool FileDialog::hasSuffix(const QString& ext) const { - QRegExp rx(QString::fromLatin1("\\*.(%1)\\W").arg(ext)); - rx.setCaseSensitivity(Qt::CaseInsensitive); + QRegularExpression rx(QString::fromLatin1("\\*.(%1)\\W").arg(ext), QRegularExpression::CaseInsensitiveOption); QStringList filters = nameFilters(); for (const auto & str : filters) { - if (rx.indexIn(str) != -1) { + if (rx.match(str).hasMatch()) { return true; } } @@ -182,12 +184,17 @@ QString FileDialog::getSaveFileName (QWidget * parent, const QString & caption, else { filterToSearch = &filter; } - QRegExp rx; + + QRegularExpression rx; rx.setPattern(QLatin1String("\\s(\\(\\*\\.\\w{1,})\\W")); - int index = rx.indexIn(*filterToSearch); - if (index != -1) { - // get the suffix with the leading dot - QString suffix = filterToSearch->mid(index+3, rx.matchedLength()-4); + auto match = rx.match(*filterToSearch); + if (match.hasMatch()) { + int index = match.capturedStart(); + int length = match.capturedLength(); + // get the suffix with the leading dot but ignore the surrounding ' (*' and ')' + int offsetStart = 3; + int offsetEnd = 4; + QString suffix = filterToSearch->mid(index + offsetStart, length - offsetEnd); if (fi.suffix().isEmpty()) dirName += suffix; } @@ -491,10 +498,11 @@ void FileOptionsDialog::accept() else if (!fn.isEmpty()) { QFileInfo fi(fn); QString ext = fi.completeSuffix(); - QRegExp rx(QLatin1String("\\(\\*.(\\w+)")); + QRegularExpression rx(QLatin1String("\\(\\*.(\\w+)")); QString suf = selectedNameFilter(); - if (rx.indexIn(suf) >= 0) - suf = rx.cap(1); + auto match = rx.match(suf); + if (match.hasMatch()) + suf = match.captured(1); if (ext.isEmpty()) setDefaultSuffix(suf); else if (ext.toLower() != suf.toLower()) { @@ -858,22 +866,22 @@ SelectModule::SelectModule (const QString& type, const SelectModule::Dict& types for (SelectModule::Dict::const_iterator it = types.begin(); it != types.end(); ++it) { auto button = new QRadioButton(groupBox); - QRegExp rx; + QRegularExpression rx; QString filter = it.key(); QString module = it.value(); // ignore file types in (...) rx.setPattern(QLatin1String("\\s+\\([\\w\\*\\s\\.]+\\)$")); - int pos = rx.indexIn(filter); - if (pos != -1) { - filter = filter.left(pos); + auto match = rx.match(filter); + if (match.hasMatch()) { + filter = filter.left(match.capturedStart()); } // ignore Gui suffix in module name rx.setPattern(QLatin1String("Gui$")); - pos = rx.indexIn(module); - if (pos != -1) { - module = module.left(pos); + match = rx.match(module); + if (match.hasMatch()) { + module = module.left(match.capturedStart()); } button->setText(QString::fromLatin1("%1 (%2)").arg(filter, module)); diff --git a/src/Gui/MDIView.cpp b/src/Gui/MDIView.cpp index 4be588a8d2..b803b3287f 100644 --- a/src/Gui/MDIView.cpp +++ b/src/Gui/MDIView.cpp @@ -25,12 +25,13 @@ #ifndef _PreComp_ # include # include -# include # include # include # include # include # include +# include +# include #endif #include @@ -144,16 +145,17 @@ void MDIView::onRelabel(Gui::Document *pDoc) // Try to separate document name and view number if there is one QString cap = windowTitle(); // Either with dirty flag ... - QRegExp rx(QLatin1String("(\\s\\:\\s\\d+\\[\\*\\])$")); - int pos = rx.lastIndexIn(cap); - if (pos == -1) { + QRegularExpression rx(QLatin1String("(\\s\\:\\s\\d+\\[\\*\\])$")); + QRegularExpressionMatch match; + int pos = cap.lastIndexOf(rx, -1, &match); + if (!match.hasMatch()) { // ... or not rx.setPattern(QLatin1String("(\\s\\:\\s\\d+)$")); - pos = rx.lastIndexIn(cap); + pos = cap.lastIndexOf(rx, -1, &match); } - if (pos != -1) { + if (match.hasMatch()) { cap = QString::fromUtf8(pDoc->getDocument()->Label.getValue()); - cap += rx.cap(); + cap += match.captured(); setWindowTitle(cap); } else { diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp index 696ab51602..7e31ab3bae 100644 --- a/src/Gui/MainWindow.cpp +++ b/src/Gui/MainWindow.cpp @@ -39,6 +39,8 @@ # include # include # include +# include +# include # include # include # include @@ -1609,10 +1611,11 @@ QPixmap MainWindow::splashImage() const int v = QtTools::horizontalAdvance(metricVer, version); int x = -1, y = -1; - QRegExp rx(QLatin1String("(\\d+).(\\d+)")); - if (rx.indexIn(position) != -1) { - x = rx.cap(1).toInt(); - y = rx.cap(2).toInt(); + QRegularExpression rx(QLatin1String("(\\d+).(\\d+)")); + auto match = rx.match(position); + if (match.hasMatch()) { + x = match.captured(1).toInt(); + y = match.captured(2).toInt(); } else { x = w - (l + v + 10); diff --git a/src/Gui/Splashscreen.cpp b/src/Gui/Splashscreen.cpp index 11fc8f4066..6f14084f3e 100644 --- a/src/Gui/Splashscreen.cpp +++ b/src/Gui/Splashscreen.cpp @@ -28,6 +28,8 @@ # include # include # include +# include +# include # include # include # include @@ -119,18 +121,18 @@ public: void Log (const char * s) { QString msg(QString::fromUtf8(s)); - QRegExp rx; + QRegularExpression rx; // ignore 'Init:' and 'Mod:' prefixes rx.setPattern(QLatin1String("^\\s*(Init:|Mod:)\\s*")); - int pos = rx.indexIn(msg); - if (pos != -1) { - msg = msg.mid(rx.matchedLength()); + auto match = rx.match(msg); + if (match.hasMatch()) { + msg = msg.mid(match.capturedLength()); } else { // ignore activation of commands rx.setPattern(QLatin1String("^\\s*(\\+App::|Create|CmdC:|CmdG:|Act:)\\s*")); - pos = rx.indexIn(msg); - if (pos == 0) + match = rx.match(msg); + if (match.hasMatch() && match.capturedStart() == 0) return; } diff --git a/src/Gui/TextEdit.cpp b/src/Gui/TextEdit.cpp index 6b247d5ff3..10e603dbf5 100644 --- a/src/Gui/TextEdit.cpp +++ b/src/Gui/TextEdit.cpp @@ -25,6 +25,8 @@ #ifndef _PreComp_ # include # include +# include +# include # include # include #endif @@ -111,7 +113,7 @@ void TextEdit::complete() if (wordPrefix.isEmpty()) return; - QStringList list = toPlainText().split(QRegExp(QLatin1String("\\W+"))); + QStringList list = toPlainText().split(QRegularExpression(QLatin1String("\\W+"))); QMap map; QStringList::Iterator it = list.begin(); while (it != list.end()) {