Git: Replace QRegExp by QRegularExpression

Task-number: QTCREATORBUG-24098
Change-Id: I77a2e9959981b2e173142253baf1668d57982a2e
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Christian Stenger
2020-07-13 12:45:12 +02:00
parent b9d6a04121
commit 228f6d04af
8 changed files with 56 additions and 51 deletions

View File

@@ -55,7 +55,7 @@
#include <QDebug> #include <QDebug>
#include <QProcess> #include <QProcess>
#include <QRegExp> #include <QRegularExpression>
#include <QAction> #include <QAction>
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
@@ -479,20 +479,20 @@ QString GerritPlugin::findLocalRepository(QString project, const QString &branch
project.remove(0, slashPos + 1); project.remove(0, slashPos + 1);
// When looking at branch 1.7, try to check folders // When looking at branch 1.7, try to check folders
// "qtbase_17", 'qtbase1.7' with a semi-smart regular expression. // "qtbase_17", 'qtbase1.7' with a semi-smart regular expression.
QScopedPointer<QRegExp> branchRegexp; QScopedPointer<QRegularExpression> branchRegexp;
if (!branch.isEmpty() && branch != "master") { if (!branch.isEmpty() && branch != "master") {
QString branchPattern = branch; QString branchPattern = branch;
branchPattern.replace('.', "[\\.-_]?"); branchPattern.replace('.', "[\\.-_]?");
const QString pattern = '^' + project const QString pattern = '^' + project
+ "[-_]?" + "[-_]?"
+ branchPattern + '$'; + branchPattern + '$';
branchRegexp.reset(new QRegExp(pattern)); branchRegexp.reset(new QRegularExpression(pattern));
if (!branchRegexp->isValid()) if (!branchRegexp->isValid())
branchRegexp.reset(); // Oops. branchRegexp.reset(); // Oops.
} }
for (const QString &repository : gitRepositories) { for (const QString &repository : gitRepositories) {
const QString fileName = Utils::FilePath::fromString(repository).fileName(); const QString fileName = Utils::FilePath::fromString(repository).fileName();
if ((!branchRegexp.isNull() && branchRegexp->exactMatch(fileName)) if ((!branchRegexp.isNull() && branchRegexp->match(fileName).hasMatch())
|| fileName == project) { || fileName == project) {
// Perform a check on the branch. // Perform a check on the branch.
if (branch.isEmpty()) { if (branch.isEmpty()) {

View File

@@ -78,7 +78,7 @@
#include <QMenu> #include <QMenu>
#include <QMessageBox> #include <QMessageBox>
#include <QPushButton> #include <QPushButton>
#include <QRegExp> #include <QRegularExpression>
#include <QTextBlock> #include <QTextBlock>
#include <QToolButton> #include <QToolButton>
#include <QTextCodec> #include <QTextCodec>
@@ -702,20 +702,22 @@ private:
void readStdOut(const QString &data) void readStdOut(const QString &data)
{ {
static QRegExp patchFailedRE("Patch failed at ([^\\n]*)"); static const QRegularExpression patchFailedRE("Patch failed at ([^\\n]*)");
static QRegExp conflictedFilesRE("Merge conflict in ([^\\n]*)"); static const QRegularExpression conflictedFilesRE("Merge conflict in ([^\\n]*)");
if (patchFailedRE.indexIn(data) != -1) const QRegularExpressionMatch match = patchFailedRE.match(data);
m_commit = patchFailedRE.cap(1); if (match.hasMatch())
int fileIndex = -1; m_commit = match.captured(1);
while ((fileIndex = conflictedFilesRE.indexIn(data, fileIndex + 1)) != -1) QRegularExpressionMatchIterator it = conflictedFilesRE.globalMatch(data);
m_files.append(conflictedFilesRE.cap(1)); while (it.hasNext())
m_files.append(it.next().captured(1));
} }
void readStdErr(const QString &data) void readStdErr(const QString &data)
{ {
static QRegExp couldNotApplyRE("[Cc]ould not (?:apply|revert) ([^\\n]*)"); static const QRegularExpression couldNotApplyRE("[Cc]ould not (?:apply|revert) ([^\\n]*)");
if (couldNotApplyRE.indexIn(data) != -1) const QRegularExpressionMatch match = couldNotApplyRE.match(data);
m_commit = couldNotApplyRE.cap(1); if (match.hasMatch())
m_commit = match.captured(1);
} }
private: private:
QString m_workingDirectory; QString m_workingDirectory;
@@ -738,11 +740,12 @@ private:
void parseProgress(const QString &text) override void parseProgress(const QString &text) override
{ {
if (m_progressExp.lastIndexIn(text) != -1) const QRegularExpressionMatch match = m_progressExp.match(text);
setProgressAndMaximum(m_progressExp.cap(1).toInt(), m_progressExp.cap(2).toInt()); if (match.hasMatch())
setProgressAndMaximum(match.captured(1).toInt(), match.captured(2).toInt());
} }
QRegExp m_progressExp; const QRegularExpression m_progressExp;
}; };
static inline QString msgRepositoryNotFound(const QString &dir) static inline QString msgRepositoryNotFound(const QString &dir)
@@ -3546,12 +3549,13 @@ unsigned GitClient::synchronousGitVersion(QString *errorMessage) const
// cut 'git version 1.6.5.1.sha' // cut 'git version 1.6.5.1.sha'
// another form: 'git version 1.9.rc1' // another form: 'git version 1.9.rc1'
const QString output = resp.stdOut(); const QString output = resp.stdOut();
QRegExp versionPattern("^[^\\d]+(\\d+)\\.(\\d+)\\.(\\d+|rc\\d).*$"); const QRegularExpression versionPattern("^[^\\d]+(\\d+)\\.(\\d+)\\.(\\d+|rc\\d).*$");
QTC_ASSERT(versionPattern.isValid(), return 0); QTC_ASSERT(versionPattern.isValid(), return 0);
QTC_ASSERT(versionPattern.exactMatch(output), return 0); const QRegularExpressionMatch match = versionPattern.match(output);
const unsigned majorV = versionPattern.cap(1).toUInt(nullptr, 16); QTC_ASSERT(match.hasMatch(), return 0);
const unsigned minorV = versionPattern.cap(2).toUInt(nullptr, 16); const unsigned majorV = match.captured(1).toUInt(nullptr, 16);
const unsigned patchV = versionPattern.cap(3).toUInt(nullptr, 16); const unsigned minorV = match.captured(2).toUInt(nullptr, 16);
const unsigned patchV = match.captured(3).toUInt(nullptr, 16);
return version(majorV, minorV, patchV); return version(majorV, minorV, patchV);
} }

View File

@@ -46,7 +46,7 @@
#include <QFileInfo> #include <QFileInfo>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QMenu> #include <QMenu>
#include <QRegExp> #include <QRegularExpression>
#include <QSet> #include <QSet>
#include <QTextBlock> #include <QTextBlock>
#include <QTextCodec> #include <QTextCodec>
@@ -108,7 +108,7 @@ public:
}; };
GitEditorWidget::GitEditorWidget() : GitEditorWidget::GitEditorWidget() :
m_changeNumberPattern(CHANGE_PATTERN) m_changeNumberPattern(QRegularExpression::anchoredPattern(CHANGE_PATTERN))
{ {
QTC_ASSERT(m_changeNumberPattern.isValid(), return); QTC_ASSERT(m_changeNumberPattern.isValid(), return);
/* Diff format: /* Diff format:
@@ -132,7 +132,7 @@ QString GitEditorWidget::changeUnderCursor(const QTextCursor &c) const
if (!cursor.hasSelection()) if (!cursor.hasSelection())
return QString(); return QString();
const QString change = cursor.selectedText(); const QString change = cursor.selectedText();
if (m_changeNumberPattern.exactMatch(change)) if (m_changeNumberPattern.match(change).hasMatch())
return change; return change;
return QString(); return QString();
} }
@@ -352,9 +352,10 @@ QString GitEditorWidget::fileNameForLine(int line) const
// 7971b6e7 share/qtcreator/dumper/dumper.py (hjk // 7971b6e7 share/qtcreator/dumper/dumper.py (hjk
QTextBlock block = document()->findBlockByLineNumber(line - 1); QTextBlock block = document()->findBlockByLineNumber(line - 1);
QTC_ASSERT(block.isValid(), return source()); QTC_ASSERT(block.isValid(), return source());
static QRegExp renameExp("^" CHANGE_PATTERN "\\s+([^(]+)"); static QRegularExpression renameExp("^" CHANGE_PATTERN "\\s+([^(]+)");
if (renameExp.indexIn(block.text()) != -1) { const QRegularExpressionMatch match = renameExp.match(block.text());
const QString fileName = renameExp.cap(1).trimmed(); if (match.hasMatch()) {
const QString fileName = match.captured(1).trimmed();
if (!fileName.isEmpty()) if (!fileName.isEmpty())
return fileName; return fileName;
} }

View File

@@ -27,7 +27,7 @@
#include <vcsbase/vcsbaseeditor.h> #include <vcsbase/vcsbaseeditor.h>
#include <QRegExp> #include <QRegularExpression>
namespace Utils { class FancyLineEdit; } namespace Utils { class FancyLineEdit; }
@@ -71,7 +71,7 @@ private:
QString fileNameForLine(int line) const override; QString fileNameForLine(int line) const override;
QString sourceWorkingDirectory() const; QString sourceWorkingDirectory() const;
mutable QRegExp m_changeNumberPattern; const QRegularExpression m_changeNumberPattern;
GitLogFilterWidget *m_logFilterWidget = nullptr; GitLogFilterWidget *m_logFilterWidget = nullptr;
}; };

View File

@@ -35,10 +35,10 @@ namespace Internal {
static const char CHANGE_PATTERN[] = "\\b[a-f0-9]{7,40}\\b"; static const char CHANGE_PATTERN[] = "\\b[a-f0-9]{7,40}\\b";
GitSubmitHighlighter::GitSubmitHighlighter(QTextEdit * parent) : GitSubmitHighlighter::GitSubmitHighlighter(QTextEdit * parent) :
TextEditor::SyntaxHighlighter(parent) TextEditor::SyntaxHighlighter(parent),
m_keywordPattern("^[\\w-]+:")
{ {
setDefaultTextFormatCategories(); setDefaultTextFormatCategories();
m_keywordPattern.setPattern("^[\\w-]+:");
m_hashChar = '#'; m_hashChar = '#';
QTC_CHECK(m_keywordPattern.isValid()); QTC_CHECK(m_keywordPattern.isValid());
} }
@@ -73,10 +73,11 @@ void GitSubmitHighlighter::highlightBlock(const QString &text)
} }
case Other: case Other:
// Format key words ("Task:") italic // Format key words ("Task:") italic
if (m_keywordPattern.indexIn(text, 0, QRegExp::CaretAtZero) == 0) { const QRegularExpressionMatch match = m_keywordPattern.match(text);
if (match.hasMatch() && match.capturedStart(0) == 0) {
QTextCharFormat charFormat = format(0); QTextCharFormat charFormat = format(0);
charFormat.setFontItalic(true); charFormat.setFontItalic(true);
setFormat(0, m_keywordPattern.matchedLength(), charFormat); setFormat(0, match.capturedLength(), charFormat);
} }
break; break;
} }
@@ -130,20 +131,21 @@ void GitRebaseHighlighter::highlightBlock(const QString &text)
{ {
if (text.startsWith(m_hashChar)) { if (text.startsWith(m_hashChar)) {
setFormat(0, text.size(), formatForCategory(Format_Comment)); setFormat(0, text.size(), formatForCategory(Format_Comment));
int changeIndex = 0; QRegularExpressionMatchIterator it = m_changeNumberPattern.globalMatch(text);
while ((changeIndex = m_changeNumberPattern.indexIn(text, changeIndex)) != -1) { while (it.hasNext()) {
const int changeLen = m_changeNumberPattern.matchedLength(); const QRegularExpressionMatch match = it.next();
setFormat(changeIndex, changeLen, formatForCategory(Format_Change)); setFormat(match.capturedStart(), match.capturedLength(), formatForCategory(Format_Change));
changeIndex += changeLen;
} }
} else { } else {
for (const RebaseAction &action : qAsConst(m_actions)) { for (const RebaseAction &action : qAsConst(m_actions)) {
if (action.exp.indexIn(text) != -1) { const QRegularExpressionMatch match = action.exp.match(text);
const int len = action.exp.matchedLength(); if (match.hasMatch()) {
const int len = match.capturedLength();
setFormat(0, len, formatForCategory(action.formatCategory)); setFormat(0, len, formatForCategory(action.formatCategory));
const int changeIndex = m_changeNumberPattern.indexIn(text, len); const QRegularExpressionMatch changeMatch = m_changeNumberPattern.match(text, len);
if (changeIndex != -1) { const int changeIndex = changeMatch.capturedStart();
const int changeLen = m_changeNumberPattern.matchedLength(); if (changeMatch.hasMatch()) {
const int changeLen = changeMatch.capturedLength();
const int descStart = changeIndex + changeLen + 1; const int descStart = changeIndex + changeLen + 1;
setFormat(changeIndex, changeLen, formatForCategory(Format_Change)); setFormat(changeIndex, changeLen, formatForCategory(Format_Change));
setFormat(descStart, text.size() - descStart, formatForCategory(Format_Description)); setFormat(descStart, text.size() - descStart, formatForCategory(Format_Description));

View File

@@ -27,7 +27,7 @@
#include <texteditor/syntaxhighlighter.h> #include <texteditor/syntaxhighlighter.h>
#include <QRegExp> #include <QRegularExpression>
namespace Git { namespace Git {
namespace Internal { namespace Internal {
@@ -56,7 +56,7 @@ public:
private: private:
enum State { None = -1, Header, Other }; enum State { None = -1, Header, Other };
QRegExp m_keywordPattern; const QRegularExpression m_keywordPattern;
QChar m_hashChar; QChar m_hashChar;
}; };
@@ -72,12 +72,12 @@ private:
class RebaseAction class RebaseAction
{ {
public: public:
mutable QRegExp exp; const QRegularExpression exp;
Format formatCategory; Format formatCategory;
RebaseAction(const QString &regexp, const Format formatCategory); RebaseAction(const QString &regexp, const Format formatCategory);
}; };
const QChar m_hashChar; const QChar m_hashChar;
QRegExp m_changeNumberPattern; const QRegularExpression m_changeNumberPattern;
QList<RebaseAction> m_actions; QList<RebaseAction> m_actions;
}; };

View File

@@ -38,7 +38,6 @@
#include <QDir> #include <QDir>
#include <QGroupBox> #include <QGroupBox>
#include <QRegExp>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QMenu> #include <QMenu>

View File

@@ -32,7 +32,6 @@
#include <texteditor/syntaxhighlighter.h> #include <texteditor/syntaxhighlighter.h>
#include <vcsbase/submiteditorwidget.h> #include <vcsbase/submiteditorwidget.h>
#include <QRegExp>
#include <QSyntaxHighlighter> #include <QSyntaxHighlighter>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE