From 71b106ab436f75d3467a71b1b3739ce570b62522 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 23 Feb 2016 21:53:08 +0100 Subject: QMimeTypeParser: use QStringRef more Keep the return values of QXmlStream*::value() around as QStringRefs for as long as possible. Avoids conversions to QString, among other things, for: - comparison to another string - conversion to int - conversion to UTF-8 or Latin-1 byte arrays Add a pair of Q_UNLIKELY as a drive-by. Saves ~900b in text size on optimized GCC 5.3 Linux AMD64 builds. Change-Id: I17d440a11aeb8675979483f89e66d0a088ccc605 Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/corelib/mimetypes/qmimemagicrule.cpp | 8 ++++---- src/corelib/mimetypes/qmimetypeparser.cpp | 20 ++++++++++---------- src/corelib/mimetypes/qmimetypeparser_p.h | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/corelib/mimetypes/qmimemagicrule.cpp b/src/corelib/mimetypes/qmimemagicrule.cpp index 09473caa78..06628fa323 100644 --- a/src/corelib/mimetypes/qmimemagicrule.cpp +++ b/src/corelib/mimetypes/qmimemagicrule.cpp @@ -238,10 +238,10 @@ QMimeMagicRule::QMimeMagicRule(const QString &type, // Parse for offset as "1" or "1:10" const int colonIndex = offsets.indexOf(QLatin1Char(':')); - const QString startPosStr = colonIndex == -1 ? offsets : offsets.mid(0, colonIndex); - const QString endPosStr = colonIndex == -1 ? offsets : offsets.mid(colonIndex + 1); - if (!QMimeTypeParserBase::parseNumber(startPosStr, &m_startPos, errorString) || - !QMimeTypeParserBase::parseNumber(endPosStr, &m_endPos, errorString)) { + const QStringRef startPosStr = offsets.midRef(0, colonIndex); // \ These decay to returning 'offsets' + const QStringRef endPosStr = offsets.midRef(colonIndex + 1);// / unchanged when colonIndex == -1 + if (Q_UNLIKELY(!QMimeTypeParserBase::parseNumber(startPosStr, &m_startPos, errorString)) || + Q_UNLIKELY(!QMimeTypeParserBase::parseNumber(endPosStr, &m_endPos, errorString))) { m_type = Invalid; return; } diff --git a/src/corelib/mimetypes/qmimetypeparser.cpp b/src/corelib/mimetypes/qmimetypeparser.cpp index 4e348b08ac..b0d599fb0e 100644 --- a/src/corelib/mimetypes/qmimetypeparser.cpp +++ b/src/corelib/mimetypes/qmimetypeparser.cpp @@ -160,12 +160,12 @@ QMimeTypeParserBase::ParseState QMimeTypeParserBase::nextState(ParseState curren } // Parse int number from an (attribute) string -bool QMimeTypeParserBase::parseNumber(const QString &n, int *target, QString *errorMessage) +bool QMimeTypeParserBase::parseNumber(const QStringRef &n, int *target, QString *errorMessage) { bool ok; *target = n.toInt(&ok); if (!ok) { - *errorMessage = QString::fromLatin1("Not a number '%1'.").arg(n); + *errorMessage = QLatin1String("Not a number '") + n + QLatin1String("'."); return false; } return true; @@ -174,11 +174,11 @@ bool QMimeTypeParserBase::parseNumber(const QString &n, int *target, QString *er #ifndef QT_NO_XMLSTREAMREADER static QMimeMagicRule *createMagicMatchRule(const QXmlStreamAttributes &atts, QString *errorMessage) { - const QString type = atts.value(QLatin1String(matchTypeAttributeC)).toString(); - const QString value = atts.value(QLatin1String(matchValueAttributeC)).toString(); - const QString offsets = atts.value(QLatin1String(matchOffsetAttributeC)).toString(); - const QString mask = atts.value(QLatin1String(matchMaskAttributeC)).toString(); - return new QMimeMagicRule(type, value.toUtf8(), offsets, mask.toLatin1(), errorMessage); + const QStringRef type = atts.value(QLatin1String(matchTypeAttributeC)); + const QStringRef value = atts.value(QLatin1String(matchValueAttributeC)); + const QStringRef offsets = atts.value(QLatin1String(matchOffsetAttributeC)); + const QStringRef mask = atts.value(QLatin1String(matchMaskAttributeC)); + return new QMimeMagicRule(type.toString(), value.toUtf8(), offsets.toString(), mask.toLatin1(), errorMessage); } #endif @@ -219,8 +219,8 @@ bool QMimeTypeParserBase::parse(QIODevice *dev, const QString &fileName, QString break; case ParseGlobPattern: { const QString pattern = atts.value(QLatin1String(patternAttributeC)).toString(); - unsigned weight = atts.value(QLatin1String(weightAttributeC)).toString().toInt(); - const bool caseSensitive = atts.value(QLatin1String(caseSensitiveAttributeC)).toString() == QLatin1String("true"); + unsigned weight = atts.value(QLatin1String(weightAttributeC)).toInt(); + const bool caseSensitive = atts.value(QLatin1String(caseSensitiveAttributeC)) == QLatin1String("true"); if (weight == 0) weight = QMimeGlobPattern::DefaultWeight; @@ -255,7 +255,7 @@ bool QMimeTypeParserBase::parse(QIODevice *dev, const QString &fileName, QString break; case ParseMagic: { priority = 50; - const QString priorityS = atts.value(QLatin1String(priorityAttributeC)).toString(); + const QStringRef priorityS = atts.value(QLatin1String(priorityAttributeC)); if (!priorityS.isEmpty()) { if (!parseNumber(priorityS, &priority, errorMessage)) return false; diff --git a/src/corelib/mimetypes/qmimetypeparser_p.h b/src/corelib/mimetypes/qmimetypeparser_p.h index cc11b70e71..a502439419 100644 --- a/src/corelib/mimetypes/qmimetypeparser_p.h +++ b/src/corelib/mimetypes/qmimetypeparser_p.h @@ -72,7 +72,7 @@ public: bool parse(QIODevice *dev, const QString &fileName, QString *errorMessage); - static bool parseNumber(const QString &n, int *target, QString *errorMessage); + static bool parseNumber(const QStringRef &n, int *target, QString *errorMessage); protected: virtual bool process(const QMimeType &t, QString *errorMessage) = 0; -- cgit v1.2.3