From 755b0aa681c203e6677430180a9cc6d103650e15 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 27 Jan 2021 12:33:46 +0100 Subject: Turn a condition into a lambda to save needless evaluation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Ievgenii Meshcheriakov --- util/publicSuffix/main.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'util/publicSuffix') 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; } -- cgit v1.2.3