summaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2021-01-27 12:33:46 +0100
committerEdward Welbourne <edward.welbourne@qt.io>2022-01-18 16:23:56 +0100
commit755b0aa681c203e6677430180a9cc6d103650e15 (patch)
treeb24b8c43a5a18dad2ba9bbbb4aa26e09ef312852 /util
parentf2230ea1a6c83bade142eca0a696dc1e219d427f (diff)
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>
Diffstat (limited to 'util')
-rw-r--r--util/publicSuffix/main.cpp11
1 files changed, 6 insertions, 5 deletions
diff --git a/util/publicSuffix/main.cpp b/util/publicSuffix/main.cpp
index 412f42684d..894e4f7523 100644
--- a/util/publicSuffix/main.cpp
+++ b/util/publicSuffix/main.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2020 The Qt Company Ltd.
+** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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
{
+ const auto isHexChar = [](char c) {
+ return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
+ };
QString result;
result.reserve(array.length() + array.length() / 3);
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
// interpreted as part of the hex value, e.g. "äc.com" -> "\xabc.com"; this
// should be "\xab""c.com"
- bool isHexChar = ((c >= '0' && c <= '9') ||
- (c >= 'a' && c <= 'f') ||
- (c >= 'A' && c <= 'F'));
- if (wasHex && isHexChar)
+ if (wasHex && isHexChar(c))
result += quadQuote;
result += c;
wasHex = false;
}
}
+ Q_ASSERT(array.isEmpty() == result.isEmpty());
return result;
}