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) <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2016-02-23 21:53:08 +01:00
parent 215bda50f9
commit 71b106ab43
3 changed files with 15 additions and 15 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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;