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:
parent
215bda50f9
commit
71b106ab43
@ -238,10 +238,10 @@ QMimeMagicRule::QMimeMagicRule(const QString &type,
|
|||||||
|
|
||||||
// Parse for offset as "1" or "1:10"
|
// Parse for offset as "1" or "1:10"
|
||||||
const int colonIndex = offsets.indexOf(QLatin1Char(':'));
|
const int colonIndex = offsets.indexOf(QLatin1Char(':'));
|
||||||
const QString startPosStr = colonIndex == -1 ? offsets : offsets.mid(0, colonIndex);
|
const QStringRef startPosStr = offsets.midRef(0, colonIndex); // \ These decay to returning 'offsets'
|
||||||
const QString endPosStr = colonIndex == -1 ? offsets : offsets.mid(colonIndex + 1);
|
const QStringRef endPosStr = offsets.midRef(colonIndex + 1);// / unchanged when colonIndex == -1
|
||||||
if (!QMimeTypeParserBase::parseNumber(startPosStr, &m_startPos, errorString) ||
|
if (Q_UNLIKELY(!QMimeTypeParserBase::parseNumber(startPosStr, &m_startPos, errorString)) ||
|
||||||
!QMimeTypeParserBase::parseNumber(endPosStr, &m_endPos, errorString)) {
|
Q_UNLIKELY(!QMimeTypeParserBase::parseNumber(endPosStr, &m_endPos, errorString))) {
|
||||||
m_type = Invalid;
|
m_type = Invalid;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -160,12 +160,12 @@ QMimeTypeParserBase::ParseState QMimeTypeParserBase::nextState(ParseState curren
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse int number from an (attribute) string
|
// 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;
|
bool ok;
|
||||||
*target = n.toInt(&ok);
|
*target = n.toInt(&ok);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
*errorMessage = QString::fromLatin1("Not a number '%1'.").arg(n);
|
*errorMessage = QLatin1String("Not a number '") + n + QLatin1String("'.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -174,11 +174,11 @@ bool QMimeTypeParserBase::parseNumber(const QString &n, int *target, QString *er
|
|||||||
#ifndef QT_NO_XMLSTREAMREADER
|
#ifndef QT_NO_XMLSTREAMREADER
|
||||||
static QMimeMagicRule *createMagicMatchRule(const QXmlStreamAttributes &atts, QString *errorMessage)
|
static QMimeMagicRule *createMagicMatchRule(const QXmlStreamAttributes &atts, QString *errorMessage)
|
||||||
{
|
{
|
||||||
const QString type = atts.value(QLatin1String(matchTypeAttributeC)).toString();
|
const QStringRef type = atts.value(QLatin1String(matchTypeAttributeC));
|
||||||
const QString value = atts.value(QLatin1String(matchValueAttributeC)).toString();
|
const QStringRef value = atts.value(QLatin1String(matchValueAttributeC));
|
||||||
const QString offsets = atts.value(QLatin1String(matchOffsetAttributeC)).toString();
|
const QStringRef offsets = atts.value(QLatin1String(matchOffsetAttributeC));
|
||||||
const QString mask = atts.value(QLatin1String(matchMaskAttributeC)).toString();
|
const QStringRef mask = atts.value(QLatin1String(matchMaskAttributeC));
|
||||||
return new QMimeMagicRule(type, value.toUtf8(), offsets, mask.toLatin1(), errorMessage);
|
return new QMimeMagicRule(type.toString(), value.toUtf8(), offsets.toString(), mask.toLatin1(), errorMessage);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -219,8 +219,8 @@ bool QMimeTypeParserBase::parse(QIODevice *dev, const QString &fileName, QString
|
|||||||
break;
|
break;
|
||||||
case ParseGlobPattern: {
|
case ParseGlobPattern: {
|
||||||
const QString pattern = atts.value(QLatin1String(patternAttributeC)).toString();
|
const QString pattern = atts.value(QLatin1String(patternAttributeC)).toString();
|
||||||
unsigned weight = atts.value(QLatin1String(weightAttributeC)).toString().toInt();
|
unsigned weight = atts.value(QLatin1String(weightAttributeC)).toInt();
|
||||||
const bool caseSensitive = atts.value(QLatin1String(caseSensitiveAttributeC)).toString() == QLatin1String("true");
|
const bool caseSensitive = atts.value(QLatin1String(caseSensitiveAttributeC)) == QLatin1String("true");
|
||||||
|
|
||||||
if (weight == 0)
|
if (weight == 0)
|
||||||
weight = QMimeGlobPattern::DefaultWeight;
|
weight = QMimeGlobPattern::DefaultWeight;
|
||||||
@ -255,7 +255,7 @@ bool QMimeTypeParserBase::parse(QIODevice *dev, const QString &fileName, QString
|
|||||||
break;
|
break;
|
||||||
case ParseMagic: {
|
case ParseMagic: {
|
||||||
priority = 50;
|
priority = 50;
|
||||||
const QString priorityS = atts.value(QLatin1String(priorityAttributeC)).toString();
|
const QStringRef priorityS = atts.value(QLatin1String(priorityAttributeC));
|
||||||
if (!priorityS.isEmpty()) {
|
if (!priorityS.isEmpty()) {
|
||||||
if (!parseNumber(priorityS, &priority, errorMessage))
|
if (!parseNumber(priorityS, &priority, errorMessage))
|
||||||
return false;
|
return false;
|
||||||
|
@ -72,7 +72,7 @@ public:
|
|||||||
|
|
||||||
bool parse(QIODevice *dev, const QString &fileName, QString *errorMessage);
|
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:
|
protected:
|
||||||
virtual bool process(const QMimeType &t, QString *errorMessage) = 0;
|
virtual bool process(const QMimeType &t, QString *errorMessage) = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user