Replacing all QRegExp with QRegularExpression (#1820)

* Replacing all QRegExp with QRegularExpression
This commit is contained in:
Jochen Bauer 2019-10-13 16:59:12 +02:00 committed by Itay Cohen
parent 9d2a8d9deb
commit b81eed7f22
15 changed files with 83 additions and 74 deletions

View File

@ -5,6 +5,7 @@
#include <QColor>
#include <QJsonArray>
#include <QStandardPaths>
#include <QRegularExpression>
#include "common/Configuration.h"
@ -313,13 +314,14 @@ bool ColorThemeWorker::isFileTheme(const QString& filePath, bool* ok) const
QString options = (Core()->cmdj("ecj").object().keys() << cutterSpecificOptions)
.join('|')
.replace(".", "\\.");
QRegExp regexp = QRegExp(QString("((ec\\s+(%1)\\s+(((rgb:|#)[0-9a-fA-F]{3,8})|(%2))))\\s*")
.arg(options)
.arg(colors));
QString pattern = QString("((ec\\s+(%1)\\s+(((rgb:|#)[0-9a-fA-F]{3,8})|(%2))))\\s*").arg(options).arg(colors);
// The below construct mimics the behaviour of QRegexP::exactMatch(), which was here before
QRegularExpression regexp("\\A(?:" + pattern + ")\\z");
for (auto &line : QString(f.readAll()).split('\n', QString::SkipEmptyParts)) {
line.replace("#~", "ec ");
if (!line.isEmpty() && !regexp.exactMatch(line)) {
if (!line.isEmpty() && !regexp.match(line).hasMatch()) {
*ok = true;
return false;
}

View File

@ -8,42 +8,43 @@ AsciiHighlighter::AsciiHighlighter(QTextDocument *parent)
HighlightingRule rule;
asciiFormat.setForeground(QColor(65, 131, 215));
rule.pattern = QRegExp("\\b[A-Za-z0-9]+\\b");
rule.pattern.setPattern("\\b[A-Za-z0-9]+\\b");
rule.format = asciiFormat;
highlightingRules.append(rule);
commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("\\*/");
commentStartRegularExpression.setPattern("/\\*");
commentEndRegularExpression.setPattern("\\*/");
}
void AsciiHighlighter::highlightBlock(const QString &text)
{
for (const HighlightingRule &rule : highlightingRules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
QRegularExpression expression(rule.pattern);
int index = expression.match(text).capturedStart();
while (index >= 0) {
int length = expression.matchedLength();
int length = expression.match(text).capturedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
index = expression.match(text.mid(index + length)).capturedStart();
}
}
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = commentStartExpression.indexIn(text);
startIndex = QRegularExpression(commentStartRegularExpression).match(text).capturedStart();
while (startIndex >= 0) {
int endIndex = commentEndExpression.indexIn(text, startIndex);
QRegularExpressionMatch commentEndMatch = QRegularExpression(commentEndRegularExpression).match(text.mid(startIndex));
int endIndex = commentEndMatch.capturedStart();
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ commentEndExpression.matchedLength();
+ commentEndMatch.capturedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
startIndex = QRegularExpression(commentStartRegularExpression).match(text.mid(startIndex + commentLength)).capturedStart();
}
}

View File

@ -5,6 +5,7 @@
#include <QHash>
#include <QTextCharFormat>
#include <QRegularExpression>
class QTextDocument;
@ -20,13 +21,13 @@ protected:
private:
struct HighlightingRule {
QRegExp pattern;
QRegularExpression pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;
QRegExp commentStartExpression;
QRegExp commentEndExpression;
QRegularExpression commentStartRegularExpression;
QRegularExpression commentEndRegularExpression;
QTextCharFormat keywordFormat;
QTextCharFormat classFormat;

View File

@ -27,51 +27,51 @@ HexHighlighter::HexHighlighter(QTextDocument *parent)
<< "\\b75\\b" << "\\b76\\b" << "\\b77\\b" << "\\b78\\b" << "\\b79\\b" << "\\b7a\\b" << "\\b7b\\b"
<< "\\b7c\\b" << "\\b7d\\b" << "\\b7e\\b" << "\\b7f\\b";
for (const QString &pattern : keywordPatterns) {
rule.pattern = QRegExp(pattern);
rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
rule.pattern.setPattern(pattern);
rule.pattern.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
singleLineCommentFormat.setFontWeight(QFont::Bold);
singleLineCommentFormat.setForeground(Qt::darkGreen);
rule.pattern = QRegExp(";[^\n]*");
rule.pattern.setPattern(";[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("\\*/");
commentStartRegularExpression.setPattern("/\\*");
commentEndRegularExpression.setPattern("\\*/");
}
void HexHighlighter::highlightBlock(const QString &text)
{
for (const HighlightingRule &rule : highlightingRules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
QRegularExpression expression(rule.pattern);
int index = expression.match(text).capturedStart();
while (index >= 0) {
int length = expression.matchedLength();
int length = expression.match(text).capturedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
index = expression.match(text.mid(index + length)).capturedStart();
}
}
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = commentStartExpression.indexIn(text);
startIndex = QRegularExpression(commentStartRegularExpression).match(text).capturedStart();
while (startIndex >= 0) {
int endIndex = commentEndExpression.indexIn(text, startIndex);
QRegularExpressionMatch commentEndMatch = QRegularExpression(commentEndRegularExpression).match(text.mid(startIndex));
int endIndex = commentEndMatch.capturedStart();
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ commentEndExpression.matchedLength();
+ commentEndMatch.capturedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
startIndex = QRegularExpression(commentStartRegularExpression).match(text.mid(startIndex + commentLength)).capturedStart();
}
}

View File

@ -5,6 +5,7 @@
#include <QHash>
#include <QTextCharFormat>
#include <QRegularExpression>
class QTextDocument;
@ -20,13 +21,13 @@ protected:
private:
struct HighlightingRule {
QRegExp pattern;
QRegularExpression pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;
QRegExp commentStartExpression;
QRegExp commentEndExpression;
QRegularExpression commentStartRegularExpression;
QRegularExpression commentEndRegularExpression;
QTextCharFormat keywordFormat;
QTextCharFormat classFormat;

View File

@ -13,8 +13,8 @@ Highlighter::Highlighter(QTextDocument *parent) :
keywordFormat.setForeground(QColor(65, 131, 215));
for (const QString &pattern : this->core->opcodes) {
rule.pattern = QRegExp("\\b" + pattern + "\\b");
rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
rule.pattern.setPattern("\\b" + pattern + "\\b");
rule.pattern.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
@ -22,8 +22,8 @@ Highlighter::Highlighter(QTextDocument *parent) :
regFormat.setForeground(QColor(236, 100, 75));
for (const QString &pattern : this->core->regs) {
rule.pattern = QRegExp("\\b" + pattern + "\\b");
rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
rule.pattern.setPattern("\\b" + pattern + "\\b");
rule.pattern.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
rule.format = regFormat;
highlightingRules.append(rule);
}
@ -31,42 +31,43 @@ Highlighter::Highlighter(QTextDocument *parent) :
singleLineCommentFormat.setFontWeight(QFont::Bold);
singleLineCommentFormat.setForeground(QColor(63, 195, 128));
rule.pattern = QRegExp(";[^\n]*");
rule.pattern.setPattern(";[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("\\*/");
commentStartRegularExpression.setPattern("/\\*");
commentEndRegularExpression.setPattern("\\*/");
}
void Highlighter::highlightBlock(const QString &text)
{
for (const HighlightingRule &rule : highlightingRules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
QRegularExpression expression(rule.pattern);
int index = expression.match(text).capturedStart();
while (index >= 0) {
int length = expression.matchedLength();
int length = expression.match(text).capturedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
index = expression.match(text.mid(index + length)).capturedStart();
}
}
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = commentStartExpression.indexIn(text);
startIndex = QRegularExpression(commentStartRegularExpression).match(text).capturedStart();
while (startIndex >= 0) {
int endIndex = commentEndExpression.indexIn(text, startIndex);
QRegularExpressionMatch commentEndMatch = QRegularExpression(commentEndRegularExpression).match(text.mid(startIndex));
int endIndex = commentEndMatch.capturedStart();
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ commentEndExpression.matchedLength();
+ commentEndMatch.capturedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
startIndex = QRegularExpression(commentStartRegularExpression).match(text.mid(startIndex + commentLength)).capturedStart();
}
}

View File

@ -6,6 +6,7 @@
#include <QSyntaxHighlighter>
#include <QHash>
#include <QTextCharFormat>
#include <QRegularExpression>
class QTextDocument;
class MainWindow;
@ -24,13 +25,13 @@ private:
CutterCore *core;
struct HighlightingRule {
QRegExp pattern;
QRegularExpression pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;
QRegExp commentStartExpression;
QRegExp commentEndExpression;
QRegularExpression commentStartRegularExpression;
QRegularExpression commentEndRegularExpression;
QTextCharFormat keywordFormat;
QTextCharFormat regFormat;

View File

@ -16,14 +16,14 @@ MdHighlighter::MdHighlighter(QTextDocument *parent)
<< "\\_\\_([^\\\\]+)\\_\\_";
for (const QString &pattern : keywordPatterns) {
rule.pattern = QRegExp(pattern);
rule.pattern.setPattern(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
singleLineCommentFormat.setFontWeight(QFont::Bold);
singleLineCommentFormat.setForeground(Qt::darkGreen);
rule.pattern = QRegExp(";[^\n]*");
rule.pattern.setPattern(";[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
}
@ -31,12 +31,12 @@ MdHighlighter::MdHighlighter(QTextDocument *parent)
void MdHighlighter::highlightBlock(const QString &text)
{
for (const HighlightingRule &rule : highlightingRules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
QRegularExpression expression(rule.pattern);
int index = expression.match(text).capturedStart();
while (index >= 0) {
int length = expression.matchedLength();
int length = expression.match(text).capturedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
index = expression.match(text.mid(index + length)).capturedStart();
}
}
setCurrentBlockState(0);

View File

@ -5,6 +5,7 @@
#include <QHash>
#include <QTextCharFormat>
#include <QRegularExpression>
class QTextDocument;
@ -20,14 +21,11 @@ protected:
private:
struct HighlightingRule {
QRegExp pattern;
QRegularExpression pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;
QRegExp commentStartExpression;
QRegExp commentEndExpression;
QTextCharFormat keywordFormat;
QTextCharFormat classFormat;
QTextCharFormat singleLineCommentFormat;

View File

@ -293,7 +293,7 @@ bool CutterCore::sdbSet(QString path, QString key, QString val)
QString CutterCore::sanitizeStringForCommand(QString s)
{
static const QRegExp regexp(";|@");
static const QRegularExpression regexp(";|@");
return s.replace(regexp, QStringLiteral("_"));
}
@ -955,7 +955,7 @@ QString CutterCore::createFunctionAt(RVA addr)
QString CutterCore::createFunctionAt(RVA addr, QString name)
{
static const QRegExp regExp("[^a-zA-Z0-9_]");
static const QRegularExpression regExp("[^a-zA-Z0-9_]");
name.remove(regExp);
QString command = "af " + name + " " + RAddressString(addr);
QString ret = cmd(command);
@ -2727,9 +2727,12 @@ void CutterCore::deleteProject(const QString &name)
bool CutterCore::isProjectNameValid(const QString &name)
{
// see is_valid_project_name() in libr/core/project.c
static const QRegExp regexp(R"(^[a-zA-Z0-9\\\._:-]{1,}$)");
return regexp.exactMatch(name) && !name.endsWith(".zip") ;
// see is_valid_project_name() in libr/core/project.
QString pattern(R"(^[a-zA-Z0-9\\\._:-]{1,}$)");
// The below construct mimics the behaviour of QRegexP::exactMatch(), which was here before
static const QRegularExpression regexp("\\A(?:" + pattern + ")\\z");
return regexp.match(name).hasMatch() && !name.endsWith(".zip") ;
}
QList<DisassemblyLine> CutterCore::disassembleLines(RVA offset, int lines)

View File

@ -1126,7 +1126,7 @@ void MainWindow::resetDockWidgetList()
for (auto it : dockWidgets) {
if (isLeft.contains(it->metaObject()->className())) {
toClose.append(it);
} else if (QRegExp("\\w+ \\d+").exactMatch(it->objectName())) {
} else if (QRegularExpression("\\A(?:\\w+ \\d+)\\z").match(it->objectName()).hasMatch()) {
isLeft.append(it->metaObject()->className());
}
}

View File

@ -1,7 +1,7 @@
#include "HexdumpRangeDialog.h"
#include "ui_HexdumpRangeDialog.h"
#include <QRegExpValidator>
#include <QRegularExpressionValidator>
#include <QPushButton>
#include <cstdint>
#include "core/Cutter.h"
@ -12,7 +12,7 @@ HexdumpRangeDialog::HexdumpRangeDialog(QWidget *parent, bool allowEmpty) :
allowEmpty(allowEmpty)
{
ui->setupUi(this);
QRegExpValidator *v = new QRegExpValidator(QRegExp("(?:0[xX])?[0-9a-fA-F]+"), this);
QRegularExpressionValidator *v = new QRegularExpressionValidator(QRegularExpression("(?:0[xX])?[0-9a-fA-F]+"), this);
ui->lengthLineEdit->setValidator(v);
ui->startAddressLineEdit->setValidator(v);
ui->endAddressLineEdit->setValidator(v);

View File

@ -271,7 +271,7 @@ QIcon AppearanceOptionsWidget::getIconFromSvg(const QString& fileName, const QCo
return QIcon();
}
QString data = file.readAll();
data.replace(QRegExp(QString("#%1").arg(before.isValid() ? before.name().remove(0, 1) : "[0-9a-fA-F]{6}")),
data.replace(QRegularExpression(QString("#%1").arg(before.isValid() ? before.name().remove(0, 1) : "[0-9a-fA-F]{6}")),
QString("%1").arg(after.name()));
QSvgRenderer svgRenderer(data.toUtf8());

View File

@ -225,7 +225,8 @@ void ColorPicker::setColor(const QColor& color)
void ColorPicker::colorChannelChanged()
{
QString txt = ui->hexLineEdit->text();
if (!QRegExp("#[0-9a-fA-F]{6}").exactMatch(txt)) {
// Regex pattern below mimics the behaviour of former RegExp::exactMatch()
if (!QRegularExpression("\\A(?:#[0-9a-fA-F]{6})\\z").match(txt).hasMatch()) {
return;
}
QColor hexColor = txt;

View File

@ -192,7 +192,7 @@ QPixmap ColorOptionDelegate::getPixmapFromSvg(const QString& fileName, const QCo
return QPixmap();
}
QString data = file.readAll();
data.replace(QRegExp("#[0-9a-fA-F]{6}"), QString("%1").arg(after.name()));
data.replace(QRegularExpression("#[0-9a-fA-F]{6}"), QString("%1").arg(after.name()));
QSvgRenderer svgRenderer(data.toUtf8());
QPixmap pix(QSize(qApp->fontMetrics().height(), qApp->fontMetrics().height()));