Turn a condition into a lambda to save needless evaluation
The public suffix list scanner's utf8encode()'s main loop always worked out whether a character is a hex digit, even when it didn't need to know. Package the computation in a lambda and only test it when it is needed. Also assert non-empty input generates non-empty output. Change-Id: Iaf48aad382624e421cea9c9cdb8bba5fc47b1596 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>
This commit is contained in:
parent
f2230ea1a6
commit
755b0aa681
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2020 The Qt Company Ltd.
|
** Copyright (C) 2022 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the utils of the Qt Toolkit.
|
** This file is part of the utils of the Qt Toolkit.
|
||||||
@ -32,6 +32,9 @@ const QString quadQuote = QStringLiteral("\"\""); // Closes one string, opens a
|
|||||||
|
|
||||||
static QString utf8encode(const QByteArray &array) // turns e.g. tranøy.no to tran\xc3\xb8y.no
|
static QString utf8encode(const QByteArray &array) // turns e.g. tranøy.no to tran\xc3\xb8y.no
|
||||||
{
|
{
|
||||||
|
const auto isHexChar = [](char c) {
|
||||||
|
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
|
||||||
|
};
|
||||||
QString result;
|
QString result;
|
||||||
result.reserve(array.length() + array.length() / 3);
|
result.reserve(array.length() + array.length() / 3);
|
||||||
bool wasHex = false;
|
bool wasHex = false;
|
||||||
@ -45,15 +48,13 @@ static QString utf8encode(const QByteArray &array) // turns e.g. tranøy.no to t
|
|||||||
// if previous char was escaped, we need to make sure the next char is not
|
// if previous char was escaped, we need to make sure the next char is not
|
||||||
// interpreted as part of the hex value, e.g. "äc.com" -> "\xabc.com"; this
|
// interpreted as part of the hex value, e.g. "äc.com" -> "\xabc.com"; this
|
||||||
// should be "\xab""c.com"
|
// should be "\xab""c.com"
|
||||||
bool isHexChar = ((c >= '0' && c <= '9') ||
|
if (wasHex && isHexChar(c))
|
||||||
(c >= 'a' && c <= 'f') ||
|
|
||||||
(c >= 'A' && c <= 'F'));
|
|
||||||
if (wasHex && isHexChar)
|
|
||||||
result += quadQuote;
|
result += quadQuote;
|
||||||
result += c;
|
result += c;
|
||||||
wasHex = false;
|
wasHex = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Q_ASSERT(array.isEmpty() == result.isEmpty());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user