Commit f04c535a authored by Lorenz Haas's avatar Lorenz Haas

Beautifier: Get rid of QRegExp

Change-Id: If3f7e6d93ef1f0b6920f0958f1e2eb00c37462bf
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent 037a96ae
......@@ -37,6 +37,7 @@
#include <QDateTime>
#include <QFile>
#include <QFileInfo>
#include <QRegularExpression>
#include <QXmlStreamWriter>
namespace Beautifier {
......@@ -67,10 +68,11 @@ ArtisticStyleSettings::ArtisticStyleSettings() :
static int parseVersion(const QString &text)
{
// The version in Artistic Style is printed like "Artistic Style Version 2.04"
const QRegExp rx("([2-9]{1})\\.([0-9]{2})(\\.[1-9]{1})?$");
if (rx.indexIn(text) != -1) {
const int major = rx.cap(1).toInt() * 100;
const int minor = rx.cap(2).toInt();
const QRegularExpression rx("([2-9]{1})\\.([0-9]{2})(\\.[1-9]{1})?$");
const QRegularExpressionMatch match = rx.match(text);
if (match.hasMatch()) {
const int major = match.capturedRef(1).toInt() * 100;
const int minor = match.capturedRef(2).toInt();
return major + minor;
}
return 0;
......@@ -200,7 +202,7 @@ void ArtisticStyleSettings::createDocumentationFile() const
stream.writeTextElement(Constants::DOCUMENTATION_XMLKEY, key);
stream.writeEndElement();
const QString text = "<p><span class=\"option\">"
+ keys.filter(QRegExp("^\\-")).join(", ") + "</span></p><p>"
+ keys.filter(QRegularExpression("^\\-")).join(", ") + "</span></p><p>"
+ (docu.join(' ').toHtmlEscaped()) + "</p>";
stream.writeTextElement(Constants::DOCUMENTATION_XMLDOC, text);
stream.writeEndElement();
......
......@@ -152,8 +152,11 @@ FormatTask format(FormatTask task)
const bool returnsCRLF = task.command.returnsCRLF();
if (addsNewline || returnsCRLF) {
task.formattedData = QString::fromUtf8(process.readAllStandardOutput());
if (addsNewline)
task.formattedData.remove(QRegExp("(\\r\\n|\\n)$"));
if (addsNewline && task.formattedData.endsWith('\n')) {
task.formattedData.chop(1);
if (task.formattedData.endsWith('\r'))
task.formattedData.chop(1);
}
if (returnsCRLF)
task.formattedData.replace("\r\n", "\n");
return task;
......
......@@ -32,7 +32,7 @@
#include <texteditor/texteditorsettings.h>
#include <QPushButton>
#include <QRegExpValidator>
#include <QRegularExpressionValidator>
namespace Beautifier {
namespace Internal {
......@@ -44,8 +44,8 @@ ConfigurationDialog::ConfigurationDialog(QWidget *parent) :
ui->setupUi(this);
// Filter out characters which are not allowed in a file name
QRegExpValidator *fileNameValidator = new QRegExpValidator(ui->name);
fileNameValidator->setRegExp(QRegExp("^[^\\/\\\\\\?\\>\\<\\*\\%\\:\\\"\\']*$"));
QRegularExpressionValidator *fileNameValidator = new QRegularExpressionValidator(
QRegularExpression("^[^\\/\\\\\\?\\>\\<\\*\\%\\:\\\"\\']*$"), ui->name);
ui->name->setValidator(fileNameValidator);
updateDocumentation();
......
......@@ -47,7 +47,6 @@ ConfigurationSyntaxHighlighter::ConfigurationSyntaxHighlighter(QTextDocument *pa
m_formatComment = fs.toTextCharFormat(TextEditor::C_COMMENT);
m_expressionComment.setPattern("#[^\\n]*");
m_expressionComment.setMinimal(false);
}
void ConfigurationSyntaxHighlighter::setKeywords(const QStringList &keywords)
......@@ -59,35 +58,29 @@ void ConfigurationSyntaxHighlighter::setKeywords(const QStringList &keywords)
QStringList pattern;
for (const QString &word : keywords) {
if (!word.isEmpty())
pattern << QRegExp::escape(word);
pattern << QRegularExpression::escape(word);
}
m_expressionKeyword.setPattern("(?:\\s|^)(" + pattern.join('|') + ")(?=\\s|\\:|\\=|\\,|$)");
}
void ConfigurationSyntaxHighlighter::setCommentExpression(const QRegExp &rx)
void ConfigurationSyntaxHighlighter::setCommentExpression(const QRegularExpression &rx)
{
m_expressionComment = rx;
}
void ConfigurationSyntaxHighlighter::highlightBlock(const QString &text)
{
int pos = 0;
if (!m_expressionKeyword.isEmpty()) {
while ((pos = m_expressionKeyword.indexIn(text, pos)) != -1) {
const int length = m_expressionKeyword.matchedLength();
setFormat(pos, length, m_formatKeyword);
pos += length;
}
QRegularExpressionMatchIterator it = m_expressionKeyword.globalMatch(text);
while (it.hasNext()) {
const QRegularExpressionMatch match = it.next();
setFormat(match.capturedStart(), match.capturedLength(), m_formatKeyword);
}
if (!m_expressionComment.isEmpty()) {
pos = 0;
while ((pos = m_expressionComment.indexIn(text, pos)) != -1) {
const int length = m_expressionComment.matchedLength();
setFormat(pos, length, m_formatComment);
pos += length;
}
it = m_expressionComment.globalMatch(text);
while (it.hasNext()) {
const QRegularExpressionMatch match = it.next();
setFormat(match.capturedStart(), match.capturedLength(), m_formatComment);
}
}
......@@ -123,7 +116,7 @@ void ConfigurationEditor::setSettings(AbstractSettings *settings)
m_model->setStringList(keywords);
}
void ConfigurationEditor::setCommentExpression(const QRegExp &rx)
void ConfigurationEditor::setCommentExpression(const QRegularExpression &rx)
{
m_highlighter->setCommentExpression(rx);
}
......
......@@ -26,7 +26,7 @@
#pragma once
#include <QPlainTextEdit>
#include <QRegExp>
#include <QRegularExpression>
#include <QString>
#include <QStringList>
#include <QSyntaxHighlighter>
......@@ -50,14 +50,14 @@ class ConfigurationSyntaxHighlighter : public QSyntaxHighlighter
public:
explicit ConfigurationSyntaxHighlighter(QTextDocument *parent);
void setKeywords(const QStringList &keywords);
void setCommentExpression(const QRegExp &rx);
void setCommentExpression(const QRegularExpression &rx);
protected:
void highlightBlock(const QString &text) override;
private:
QRegExp m_expressionKeyword;
QRegExp m_expressionComment;
QRegularExpression m_expressionKeyword;
QRegularExpression m_expressionComment;
QTextCharFormat m_formatKeyword;
QTextCharFormat m_formatComment;
};
......@@ -69,7 +69,7 @@ class ConfigurationEditor : public QPlainTextEdit
public:
explicit ConfigurationEditor(QWidget *parent = nullptr);
void setSettings(AbstractSettings *settings);
void setCommentExpression(const QRegExp &rx);
void setCommentExpression(const QRegularExpression &rx);
protected:
bool eventFilter(QObject *object, QEvent *event) override;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment