QmlDesigner: Replace QRegExp by QRegularExpression

Task-number: QTCREATORBUG-24098
Change-Id: I7cc6093b8275435f941bf4d9de59128570d90420
Reviewed-by: Henning Gründl <henning.gruendl@qt.io>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Christian Stenger
2020-07-13 13:42:56 +02:00
parent e4c1d52e88
commit 1de507a710
10 changed files with 60 additions and 47 deletions

View File

@@ -33,7 +33,7 @@
#include <utils/stylehelper.h>
#include <QApplication>
#include <QRegExp>
#include <QRegularExpression>
#include <QScreen>
#include <QPointer>
#include <QQmlEngine>
@@ -92,13 +92,15 @@ Theme *Theme::instance()
QString Theme::replaceCssColors(const QString &input)
{
QRegExp rx("creatorTheme\\.(\\w+)");
const QRegularExpression rx("creatorTheme\\.(\\w+)");
int pos = 0;
QString output = input;
while ((pos = rx.indexIn(input, pos)) != -1) {
const QString themeColorName = rx.cap(1);
QRegularExpressionMatchIterator it = rx.globalMatch(input);
while (it.hasNext()) {
const QRegularExpressionMatch match = it.next();
const QString themeColorName = match.captured(1);
if (themeColorName == "smallFontPixelSize") {
output.replace("creatorTheme." + themeColorName, QString::number(instance()->smallFontPixelSize()) + "px");
@@ -106,9 +108,9 @@ QString Theme::replaceCssColors(const QString &input)
output.replace("creatorTheme." + themeColorName, QString::number(instance()->captionFontPixelSize()) + "px");
} else {
const QColor color = instance()->evaluateColorAtThemeInstance(themeColorName);
output.replace("creatorTheme." + rx.cap(1), color.name());
output.replace("creatorTheme." + themeColorName, color.name());
}
pos += rx.matchedLength();
pos += match.capturedLength();
}
return output;

View File

@@ -28,12 +28,13 @@
#include <utils/qtcassert.h>
#include <utils/algorithm.h>
#include <QVariant>
#include <QMetaObject>
#include <QMetaEnum>
#include <algorithm>
#include <QDebug>
#include <QMetaEnum>
#include <QMetaObject>
#include <QRegularExpression>
#include <QVariant>
#include <algorithm>
GradientPresetItem::GradientPresetItem()
: m_gradientVal(QGradient())
@@ -155,7 +156,7 @@ QString GradientPresetItem::getNameByPreset(Preset value)
QString enumName = QString::fromUtf8(metaEnum.valueToKey(static_cast<int>(value)));
const QStringList sl = enumName.split(QRegExp("(?=[A-Z])"), QString::SkipEmptyParts);
const QStringList sl = enumName.split(QRegularExpression("(?=[A-Z])"), Qt::SkipEmptyParts);
enumName.clear();
std::for_each(sl.begin(), sl.end(), [&enumName](const QString &s) { enumName += (s + " "); });

View File

@@ -35,7 +35,7 @@
#include <utils/qtcassert.h>
#include <QRegExp>
#include <QRegularExpression>
#include <QUrl>
//using namespace QmlDesigner;
@@ -246,12 +246,13 @@ bool PropertyEditorValue::isTranslated() const
if (modelNode().metaInfo().propertyTypeName(name()) == "QString" || modelNode().metaInfo().propertyTypeName(name()) == "string") {
const QmlDesigner::QmlObjectNode objectNode(modelNode());
if (objectNode.isValid() && objectNode.hasBindingProperty(name())) {
QRegExp rx("qsTr(|Id|anslate)\\(\".*\"\\)");
const QRegularExpression rx(QRegularExpression::anchoredPattern(
"qsTr(|Id|anslate)\\(\".*\"\\)"));
//qsTr()
if (objectNode.propertyAffectedByCurrentState(name())) {
return rx.exactMatch(expression());
return expression().contains(rx);
} else {
return rx.exactMatch(modelNode().bindingProperty(name()).expression());
return modelNode().bindingProperty(name()).expression().contains(rx);
}
}
return false;
@@ -358,9 +359,11 @@ QString PropertyEditorValue::getTranslationContext() const
if (modelNode().metaInfo().propertyTypeName(name()) == "QString" || modelNode().metaInfo().propertyTypeName(name()) == "string") {
const QmlDesigner::QmlObjectNode objectNode(modelNode());
if (objectNode.isValid() && objectNode.hasBindingProperty(name())) {
QRegExp rx("qsTranslate\\(\"(.*)\"\\s*,\\s*\".*\"\\s*\\)");
if (rx.exactMatch(expression()))
return rx.cap(1);
const QRegularExpression rx(QRegularExpression::anchoredPattern(
"qsTranslate\\(\"(.*)\"\\s*,\\s*\".*\"\\s*\\)"));
const QRegularExpressionMatch match = rx.match(expression());
if (match.hasMatch())
return match.captured(1);
}
}
}
@@ -372,11 +375,12 @@ bool PropertyEditorValue::isIdList() const
if (modelNode().isValid() && modelNode().metaInfo().isValid() && modelNode().metaInfo().hasProperty(name())) {
const QmlDesigner::QmlObjectNode objectNode(modelNode());
if (objectNode.isValid() && objectNode.hasBindingProperty(name())) {
static const QRegExp rx("^[a-z_]\\w*|^[A-Z]\\w*\\.{1}([a-z_]\\w*\\.?)+");
static const QRegularExpression rx(QRegularExpression::anchoredPattern(
"^[a-z_]\\w*|^[A-Z]\\w*\\.{1}([a-z_]\\w*\\.?)+"));
const QString exp = objectNode.propertyAffectedByCurrentState(name()) ? expression() : modelNode().bindingProperty(name()).expression();
for (const auto &str : generateStringList(exp))
{
if (!rx.exactMatch(str))
if (!str.contains(rx))
return false;
}
return true;
@@ -397,8 +401,9 @@ bool PropertyEditorValue::idListAdd(const QString &value)
if (!isIdList() && (objectNode.isValid() && objectNode.hasProperty(name())))
return false;
static const QRegExp rx("^[a-z_]\\w*|^[A-Z]\\w*\\.{1}([a-z_]\\w*\\.?)+");
if (!rx.exactMatch(value))
static const QRegularExpression rx(QRegularExpression::anchoredPattern(
"^[a-z_]\\w*|^[A-Z]\\w*\\.{1}([a-z_]\\w*\\.?)+"));
if (!value.contains(rx))
return false;
auto stringList = generateStringList(expression());
@@ -426,8 +431,9 @@ bool PropertyEditorValue::idListReplace(int idx, const QString &value)
{
QTC_ASSERT(isIdList(), return false);
static const QRegExp rx("^[a-z_]\\w*|^[A-Z]\\w*\\.{1}([a-z_]\\w*\\.?)+");
if (!rx.exactMatch(value))
static const QRegularExpression rx(QRegularExpression::anchoredPattern(
"^[a-z_]\\w*|^[A-Z]\\w*\\.{1}([a-z_]\\w*\\.?)+"));
if (!value.contains(rx))
return false;
auto stringList = generateStringList(expression());
@@ -446,7 +452,7 @@ QStringList PropertyEditorValue::generateStringList(const QString &string) const
QString copy = string;
copy = copy.remove("[").remove("]");
QStringList tmp = copy.split(",", QString::SkipEmptyParts);
QStringList tmp = copy.split(',', Qt::SkipEmptyParts);
for (QString &str : tmp)
str = str.trimmed();

View File

@@ -29,7 +29,7 @@
#include <rewritingexception.h>
#include <QDebug>
#include <QRegExp>
#include <QRegularExpression>
#include <cmath>
#include <nodemetainfo.h>
@@ -186,10 +186,10 @@ void StatesEditorView::duplicateCurrentState()
QString newName = state.name();
// Strip out numbers at the end of the string
QRegExp regEx(QLatin1String("[0-9]+$"));
int numberIndex = newName.indexOf(regEx);
if ((numberIndex != -1) && (numberIndex+regEx.matchedLength()==newName.length()))
newName = newName.left(numberIndex);
QRegularExpression regEx(QLatin1String("[0-9]+$"));
const QRegularExpressionMatch match = regEx.match(newName);
if (match.hasMatch() && (match.capturedStart() + match.capturedLength() == newName.length()))
newName = newName.left(match.capturedStart());
int i = 1;
QStringList stateNames = rootStateGroup().names();

View File

@@ -150,7 +150,7 @@ bool EasingCurve::fromString(const QString &code)
{
if (code.startsWith(QLatin1Char('[')) && code.endsWith(QLatin1Char(']'))) {
const QStringRef cleanCode(&code, 1, code.size() - 2);
const auto stringList = cleanCode.split(QLatin1Char(','), QString::SkipEmptyParts);
const auto stringList = cleanCode.split(QLatin1Char(','), Qt::SkipEmptyParts);
if (stringList.count() >= 6 && (stringList.count() % 6 == 0)) {
bool checkX, checkY;

View File

@@ -41,7 +41,7 @@
#include <utils/qtcassert.h>
#include <utils/algorithm.h>
#include <QRegExp>
#include <QRegularExpression>
#include <QWidget>
#include <QtGui/qimage.h>
@@ -520,12 +520,12 @@ QString AbstractView::generateNewId(const QString &prefixName) const
*/
QString newId = QString(QStringLiteral("%1")).arg(firstCharToLower(prefixName));
newId.remove(QRegExp(QStringLiteral("[^a-zA-Z0-9_]")));
newId.remove(QRegularExpression(QStringLiteral("[^a-zA-Z0-9_]")));
while (!ModelNode::isValidId(newId) || hasId(newId) || rootModelNode().hasProperty(newId.toUtf8()) || newId == "item") {
counter += 1;
newId = QString(QStringLiteral("%1%2")).arg(firstCharToLower(prefixName)).arg(counter - 1);
newId.remove(QRegExp(QStringLiteral("[^a-zA-Z0-9_]")));
newId.remove(QRegularExpression(QStringLiteral("[^a-zA-Z0-9_]")));
}
return newId;

View File

@@ -46,7 +46,7 @@
#include <utils/algorithm.h>
#include <QHash>
#include <QRegExp>
#include <QRegularExpression>
#include <QSet>
#include <QTextStream>
@@ -224,8 +224,8 @@ static bool isIdToAvoid(const QString& id)
static bool idContainsWrongLetter(const QString& id)
{
static QRegExp idExpr(QStringLiteral("[a-z_][a-zA-Z0-9_]*"));
return !idExpr.exactMatch(id);
static QRegularExpression idExpr(QStringLiteral("^[a-z_][a-zA-Z0-9_]+$"));
return !id.contains(idExpr);
}
bool ModelNode::isValidId(const QString &id)

View File

@@ -44,7 +44,7 @@
#include <qmldesignerplugin.h>
#endif
#include <QRegExp>
#include <QRegularExpression>
namespace QmlDesigner {
@@ -246,8 +246,9 @@ bool QmlObjectNode::isTranslatableText(const PropertyName &name) const
if (modelNode().metaInfo().isValid() && modelNode().metaInfo().hasProperty(name))
if (modelNode().metaInfo().propertyTypeName(name) == "QString" || modelNode().metaInfo().propertyTypeName(name) == "string") {
if (modelNode().hasBindingProperty(name)) {
static QRegExp regularExpressionPatter(QLatin1String("qsTr(|Id|anslate)\\(\".*\"\\)"));
return regularExpressionPatter.exactMatch(modelNode().bindingProperty(name).expression());
static QRegularExpression regularExpressionPattern(
QLatin1String("^qsTr(|Id|anslate)\\(\".*\"\\)$"));
return modelNode().bindingProperty(name).expression().contains(regularExpressionPattern);
}
return false;
@@ -259,9 +260,12 @@ bool QmlObjectNode::isTranslatableText(const PropertyName &name) const
QString QmlObjectNode::stripedTranslatableText(const PropertyName &name) const
{
if (modelNode().hasBindingProperty(name)) {
static QRegExp regularExpressionPatter(QLatin1String("qsTr(|Id|anslate)\\(\"(.*)\"\\)"));
if (regularExpressionPatter.exactMatch(modelNode().bindingProperty(name).expression()))
return regularExpressionPatter.cap(2);
static QRegularExpression regularExpressionPattern(
QLatin1String("^qsTr(|Id|anslate)\\(\"(.*)\"\\)$"));
const QRegularExpressionMatch match = regularExpressionPattern.match(
modelNode().bindingProperty(name).expression());
if (match.hasMatch())
return match.captured(2);
return instanceValue(name).toString();
}
return instanceValue(name).toString();

View File

@@ -53,7 +53,7 @@
#include <utils/changeset.h>
#include <utils/qtcassert.h>
#include <QRegExp>
#include <QRegularExpression>
#include <utility>
#include <vector>
@@ -542,7 +542,7 @@ QString RewriterView::auxiliaryDataAsQML() const
int columnCount = 0;
const QRegExp safeName("[a-z][a-zA-Z0-9]*");
const QRegularExpression safeName("^[a-z][a-zA-Z0-9]+$");
for (const auto &node : allModelNodes()) {
QHash<PropertyName, QVariant> data = node.auxiliaryData();
@@ -576,7 +576,7 @@ QString RewriterView::auxiliaryDataAsQML() const
if (idIsQmlKeyWord(key))
continue;
if (!safeName.exactMatch(key))
if (!key.contains(safeName))
continue;
const QVariant value = data.value(key.toUtf8());

View File

@@ -353,7 +353,7 @@ void StylesheetMerger::adjustNodeIndex(ModelNode &node)
void StylesheetMerger::applyStyleProperties(ModelNode &templateNode, const ModelNode &styleNode)
{
QRegExp regEx("[a-z]", Qt::CaseInsensitive);
const QRegularExpression regEx("[a-z]", QRegularExpression::CaseInsensitiveOption);
for (const VariantProperty &variantProperty : styleNode.variantProperties()) {
if (templateNode.hasBindingProperty(variantProperty.name())) {
// if the binding does not contain any alpha letters (i.e. binds to a term rather than a property,