Use QRE::matchView instead of match when argument is a stringview

The QStringView overload of match will be deprecated in Qt 6.8.
As a drive-by, replace a match(view.toString()) with a a matchView; the
view lives long enough, so no reason to convert to QString.
Also use capturedView instead of captured when working with
string-views, and hoist one regex construction out of a loop.

As the QML DOM has to build with older Qt versions, condition the use of
matchView on Qt being >= 6.4

Fixes: QTBUG-105360
Change-Id: If248d135fd381c6368d446376798a52dec220b40
Reviewed-by: Maximilian Goldstein <max.goldstein@qt.io>
This commit is contained in:
Fabian Kosmale 2022-08-01 11:03:27 +02:00
parent 2bd4ee37ff
commit 9c2fd1724c
5 changed files with 39 additions and 11 deletions

View File

@ -724,7 +724,11 @@ public:
pathFromOwner = comp.addId(idVal, AddOption::KeepExisting, &idPtr);
QRegularExpression idRe(QRegularExpression::anchoredPattern(
QStringLiteral(uR"([[:lower:]][[:lower:][:upper:]0-9_]*)")));
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
auto m = idRe.match(iExp->name);
#else
auto m = idRe.matchView(iExp->name);
#endif
if (!m.hasMatch()) {
qmlFile.addError(
myParseErrors()

View File

@ -203,13 +203,17 @@ Version Version::fromString(QStringView v)
return Version(Latest, Latest);
QRegularExpression r(
QRegularExpression::anchoredPattern(QStringLiteral(uR"(([0-9]*)(?:\.([0-9]*))?)")));
auto m = r.match(v.toString());
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
auto m = r.match(v);
#else
auto m = r.matchView(v);
#endif
if (m.hasMatch()) {
bool ok;
int majorV = m.captured(1).toInt(&ok);
int majorV = m.capturedView(1).toInt(&ok);
if (!ok)
majorV = Version::Undefined;
int minorV = m.captured(2).toInt(&ok);
int minorV = m.capturedView(2).toInt(&ok);
if (!ok)
minorV = Version::Undefined;
return Version(majorV, minorV);

View File

@ -97,14 +97,19 @@ bool FieldFilter::operator()(DomItem &base, const PathEls::PathComponent &c, Dom
bool FieldFilter::addFilter(QString fFields)
{
// parses a base filter of the form <op><typeName>:<fieldName> or <op><fieldName>
// as described in this class documentation
QRegularExpression fieldRe(QRegularExpression::anchoredPattern(QStringLiteral(
uR"((?<op>[-+])?(?:(?<type>[a-zA-Z0-9_]*):)?(?<field>[a-zA-Z0-9_]*))")));
for (const QString &fField : fFields.split(QLatin1Char(','))) {
// parses a base filter of the form <op><typeName>:<fieldName> or <op><fieldName>
// as described in this class documentation
QRegularExpression fieldRe(QRegularExpression::anchoredPattern(QStringLiteral(
uR"((?<op>[-+])?(?:(?<type>[a-zA-Z0-9_]*):)?(?<field>[a-zA-Z0-9_]*))")));
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
QRegularExpressionMatch m = fieldRe.match(fField);
#else
QRegularExpressionMatch m = fieldRe.matchView(fField);
#endif
if (m.hasMatch()) {
if (m.captured(u"op") == u"+") {
if (m.capturedView(u"op") == u"+") {
m_fieldFilterRemove.remove(m.captured(u"type"), m.captured(u"field"));
m_fieldFilterAdd.insert(m.captured(u"type"), m.captured(u"field"));
} else {

View File

@ -1680,7 +1680,12 @@ public:
CppTypeInfo res;
QRegularExpression reTarget = QRegularExpression(QRegularExpression::anchoredPattern(
uR"(QList<(?<list>[a-zA-Z_0-9:]+) *(?<listPtr>\*?)>|QMap< *(?<mapKey>[a-zA-Z_0-9:]+) *, *(?<mapValue>[a-zA-Z_0-9:]+) *(?<mapPtr>\*?)>|(?<baseType>[a-zA-Z_0-9:]+) *(?<ptr>\*?))"));
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
QRegularExpressionMatch m = reTarget.match(target);
#else
QRegularExpressionMatch m = reTarget.matchView(target);
#endif
if (!m.hasMatch()) {
DomItem::myResolveErrors()
.error(tr("Unexpected complex CppType %1").arg(target))

View File

@ -137,13 +137,23 @@ QString LineWriter::eolToWrite() const
return QStringLiteral(u"\n");
}
template<typename String, typename ...Args>
static QRegularExpressionMatch matchHelper(QRegularExpression &re, String &&s, Args &&...args)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
return re.match(s, args...);
#else
return re.matchView(s, args...);
#endif
}
LineWriter &LineWriter::write(QStringView v, TextAddType tAdd)
{
QString eol;
// split multiple lines
static QRegularExpression eolRe(QLatin1String(
"(\r?\n|\r)")); // does not support split of \r and \n for windows style line endings
QRegularExpressionMatch m = eolRe.match(v);
QRegularExpressionMatch m = matchHelper(eolRe, v);
if (m.hasMatch()) {
// add line by line
auto i = m.capturedStart(1);
@ -153,11 +163,11 @@ LineWriter &LineWriter::write(QStringView v, TextAddType tAdd)
// because we cannot have already opened or closed a PendingSourceLocation
if (iEnd < v.size()) {
write(v.mid(0, iEnd));
m = eolRe.match(v, iEnd);
m = matchHelper(eolRe, v, iEnd);
while (m.hasMatch()) {
write(v.mid(iEnd, m.capturedEnd(1) - iEnd));
iEnd = m.capturedEnd(1);
m = eolRe.match(v, iEnd);
m = matchHelper(eolRe, v, iEnd);
}
if (iEnd < v.size())
write(v.mid(iEnd, v.size() - iEnd));