From 6f4160042226be86276fa9264663c2942413fc8d Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 31 Mar 2017 12:49:12 +0200 Subject: Port internal class QXmlUtils to QStringView ... and adapt callers. In one case, port a use of QString::toLatin1() to QStringRef:: toLatin1() so together with this change and the addition of QString::arg(QStringView) we can drop one more QStringRef::toString(). Change-Id: Iba898af8cc2a9e1a811e271789359395f595ad32 Reviewed-by: Thiago Macieira Reviewed-by: Edward Welbourne --- src/corelib/xml/qxmlstream.cpp | 5 ++--- src/corelib/xml/qxmlstream_p.h | 2 +- src/corelib/xml/qxmlutils.cpp | 37 +++++++++++++++++-------------------- src/corelib/xml/qxmlutils_p.h | 7 +++---- 4 files changed, 23 insertions(+), 28 deletions(-) (limited to 'src/corelib/xml') diff --git a/src/corelib/xml/qxmlstream.cpp b/src/corelib/xml/qxmlstream.cpp index ecca5569f8..3d600c2380 100644 --- a/src/corelib/xml/qxmlstream.cpp +++ b/src/corelib/xml/qxmlstream.cpp @@ -1798,18 +1798,17 @@ void QXmlStreamReaderPrivate::startDocument() QStringRef value(symString(attrib.value)); if (prefix.isEmpty() && key == QLatin1String("encoding")) { - const QString name(value.toString()); documentEncoding = value; if(hasStandalone) err = QXmlStream::tr("The standalone pseudo attribute must appear after the encoding."); - if(!QXmlUtils::isEncName(name)) + if (!QXmlUtils::isEncName(value)) err = QXmlStream::tr("%1 is an invalid encoding name.").arg(value); else { #ifdef QT_NO_TEXTCODEC readBuffer = QString::fromLatin1(rawReadBuffer.data(), nbytesread); #else - QTextCodec *const newCodec = QTextCodec::codecForName(name.toLatin1()); + QTextCodec *const newCodec = QTextCodec::codecForName(value.toLatin1()); if (!newCodec) err = QXmlStream::tr("Encoding %1 is unsupported").arg(value); else if (newCodec != codec && !lockEncoding) { diff --git a/src/corelib/xml/qxmlstream_p.h b/src/corelib/xml/qxmlstream_p.h index b62cc9ac39..1edd716094 100644 --- a/src/corelib/xml/qxmlstream_p.h +++ b/src/corelib/xml/qxmlstream_p.h @@ -1636,7 +1636,7 @@ bool QXmlStreamReaderPrivate::parse() break; case 175: { - if (!QXmlUtils::isPublicID(symString(1).toString())) { + if (!QXmlUtils::isPublicID(symString(1))) { raiseWellFormedError(QXmlStream::tr("%1 is an invalid PUBLIC identifier.").arg(symString(1).toString())); resume(175); return false; diff --git a/src/corelib/xml/qxmlutils.cpp b/src/corelib/xml/qxmlutils.cpp index 23caae2935..01c84251fd 100644 --- a/src/corelib/xml/qxmlutils.cpp +++ b/src/corelib/xml/qxmlutils.cpp @@ -227,19 +227,22 @@ bool QXmlUtils::isBaseChar(const QChar c) \sa {http://www.w3.org/TR/REC-xml/#NT-EncName}, {Extensible Markup Language (XML) 1.0 (Fourth Edition), [81] EncName} */ -bool QXmlUtils::isEncName(const QString &encName) +bool QXmlUtils::isEncName(QStringView encName) { // Valid encoding names are given by "[A-Za-z][A-Za-z0-9._\\-]*" - const ushort *c = encName.utf16(); - int l = encName.length(); - if (l < 1 || !((c[0] >= 'a' && c[0] <= 'z') || (c[0] >= 'A' && c[0] <= 'Z'))) + if (encName.isEmpty()) return false; - for (int i = 1; i < l; ++i) { - if ((c[i] >= 'a' && c[i] <= 'z') - || (c[i] >= 'A' && c[i] <= 'Z') - || (c[i] >= '0' && c[i] <= '9') - || c[i] == '.' || c[i] == '_' || c[i] == '-') + const auto first = encName.front().unicode(); + if (!((first >= 'a' && first <= 'z') || (first >= 'A' && first <= 'Z'))) + return false; + for (QChar ch : encName.mid(1)) { + const auto cp = ch.unicode(); + if ((cp >= 'a' && cp <= 'z') + || (cp >= 'A' && cp <= 'Z') + || (cp >= '0' && cp <= '9') + || cp == '.' || cp == '_' || cp == '-') { continue; + } return false; } return true; @@ -310,13 +313,10 @@ bool QXmlUtils::isNameChar(const QChar c) \sa {http://www.w3.org/TR/REC-xml/#NT-PubidLiteral}, {Extensible Markup Language (XML) 1.0 (Fourth Edition), [12] PubidLiteral} */ -bool QXmlUtils::isPublicID(const QString &candidate) +bool QXmlUtils::isPublicID(QStringView candidate) { - const int len = candidate.length(); - - for(int i = 0; i < len; ++i) - { - const ushort cp = candidate.at(i).unicode(); + for (QChar ch : candidate) { + const ushort cp = ch.unicode(); if ((cp >= 'a' && cp <= 'z') || (cp >= 'A' && cp <= 'Z') @@ -369,7 +369,7 @@ bool QXmlUtils::isPublicID(const QString &candidate) \sa {http://www.w3.org/TR/REC-xml-names/#NT-NCName}, {W3CNamespaces in XML 1.0 (Second Edition), [4] NCName} */ -bool QXmlUtils::isNCName(const QStringRef &ncName) +bool QXmlUtils::isNCName(QStringView ncName) { if(ncName.isEmpty()) return false; @@ -379,10 +379,7 @@ bool QXmlUtils::isNCName(const QStringRef &ncName) if(!QXmlUtils::isLetter(first) && first.unicode() != '_' && first.unicode() != ':') return false; - const int len = ncName.size(); - for(int i = 0; i < len; ++i) - { - const QChar at = ncName.at(i); + for (QChar at : ncName) { if(!QXmlUtils::isNameChar(at) || at == QLatin1Char(':')) return false; } diff --git a/src/corelib/xml/qxmlutils_p.h b/src/corelib/xml/qxmlutils_p.h index 6db347b3ee..db6bddd5be 100644 --- a/src/corelib/xml/qxmlutils_p.h +++ b/src/corelib/xml/qxmlutils_p.h @@ -68,13 +68,12 @@ class QXmlCharRange; class Q_CORE_EXPORT QXmlUtils { public: - static bool isEncName(const QString &encName); + static bool isEncName(QStringView encName); static bool isChar(const QChar c); static bool isNameChar(const QChar c); static bool isLetter(const QChar c); - static bool isNCName(const QStringRef &ncName); - static inline bool isNCName(const QString &ncName) { return isNCName(QStringRef(&ncName)); } - static bool isPublicID(const QString &candidate); + static bool isNCName(QStringView ncName); + static bool isPublicID(QStringView candidate); private: typedef const QXmlCharRange *RangeIter; -- cgit v1.2.3