Core: replace QRegExp with QRegularExpression

This commit is contained in:
wmayer
2022-10-06 13:54:20 +02:00
parent 4ca1b838a9
commit 75bfb8f48f
11 changed files with 99 additions and 62 deletions

View File

@@ -32,6 +32,8 @@
# include <QLocale>
# include <QMessageBox>
# include <QMessageLogContext>
# include <QRegularExpression>
# include <QRegularExpressionMatch>
# include <QStatusBar>
# include <QStyle>
# include <QTextStream>
@@ -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 '<type 'exceptions.ImportError'>' prefixes
rx.setPattern(QLatin1String("^\\s*<type 'exceptions.ImportError'>:\\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());

View File

@@ -27,6 +27,8 @@
# include <QKeyEvent>
# include <QLabel>
# include <QPlainTextEdit>
# include <QRegularExpression>
# include <QRegularExpressionMatch>
# include <QTextCursor>
# include <QToolTip>
#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 );

View File

@@ -23,6 +23,8 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <sstream>
# include <QRegularExpression>
# include <QRegularExpressionMatch>
#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());
}
}

View File

@@ -24,6 +24,8 @@
#ifndef _PreComp_
# include <QApplication>
# include <QMessageBox>
# include <QRegularExpression>
# include <QRegularExpressionMatch>
# include <QWhatsThis>
#endif
@@ -89,11 +91,11 @@ void StdCmdWorkbench::activated(int i)
catch(const Base::PyException& e) {
QString msg(QLatin1String(e.what()));
// ignore '<type 'exceptions.*Error'>' prefixes
QRegExp rx;
QRegularExpression rx;
rx.setPattern(QLatin1String("^\\s*<type 'exceptions.\\w*'>:\\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(...) {

View File

@@ -25,6 +25,8 @@
#ifndef _PreComp_
# include <QMessageBox>
# include <QPushButton>
# include <QRegularExpression>
# include <QRegularExpressionMatch>
#endif
#include "DlgCreateNewPreferencePackImp.h"

View File

@@ -21,6 +21,10 @@
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QRegularExpression>
# include <QRegularExpressionMatch>
#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());
}
}
}

View File

@@ -33,6 +33,8 @@
# include <QLineEdit>
# include <QPushButton>
# include <QRadioButton>
# include <QRegularExpression>
# include <QRegularExpressionMatch>
# include <QResizeEvent>
# include <QStandardPaths>
# include <QStyle>
@@ -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<QUrl> 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));

View File

@@ -25,12 +25,13 @@
#ifndef _PreComp_
# include <boost_signals2.hpp>
# include <QApplication>
# include <QRegExp>
# include <QEvent>
# include <QCloseEvent>
# include <QMdiSubWindow>
# include <QPrinter>
# include <QPrinterInfo>
# include <QRegularExpression>
# include <QRegularExpressionMatch>
#endif
#include <Base/Interpreter.h>
@@ -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 {

View File

@@ -39,6 +39,8 @@
# include <QMessageBox>
# include <QMimeData>
# include <QPainter>
# include <QRegularExpression>
# include <QRegularExpressionMatch>
# include <QScreen>
# include <QSettings>
# include <QSignalMapper>
@@ -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);

View File

@@ -28,6 +28,8 @@
# include <QLocale>
# include <QMutex>
# include <QProcessEnvironment>
# include <QRegularExpression>
# include <QRegularExpressionMatch>
# include <QScreen>
# include <QSysInfo>
# include <QTextBrowser>
@@ -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;
}

View File

@@ -25,6 +25,8 @@
#ifndef _PreComp_
# include <QKeyEvent>
# include <QPainter>
# include <QRegularExpression>
# include <QRegularExpressionMatch>
# include <QShortcut>
# include <QTextCursor>
#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<QString, QString> map;
QStringList::Iterator it = list.begin();
while (it != list.end()) {