diff options
Diffstat (limited to 'src/corelib/io/qipaddress.cpp')
-rw-r--r-- | src/corelib/io/qipaddress.cpp | 102 |
1 files changed, 30 insertions, 72 deletions
diff --git a/src/corelib/io/qipaddress.cpp b/src/corelib/io/qipaddress.cpp index 93a74ccd09..c2b274f8b5 100644 --- a/src/corelib/io/qipaddress.cpp +++ b/src/corelib/io/qipaddress.cpp @@ -1,42 +1,6 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2016 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qipaddress_p.h" #include "private/qlocale_tools_p.h" @@ -44,20 +8,23 @@ #include "qvarlengtharray.h" QT_BEGIN_NAMESPACE + +using namespace Qt::StringLiterals; + namespace QIPAddressUtils { -static QString number(quint8 val, int base = 10) +static QString number(quint8 val) { QString zero = QStringLiteral("0"); - return val ? qulltoa(val, base, zero) : zero; + return val ? qulltoa(val, 10, zero) : zero; } typedef QVarLengthArray<char, 64> Buffer; static const QChar *checkedToAscii(Buffer &buffer, const QChar *begin, const QChar *end) { - const ushort *const ubegin = reinterpret_cast<const ushort *>(begin); - const ushort *const uend = reinterpret_cast<const ushort *>(end); - const ushort *src = ubegin; + const auto *const ubegin = reinterpret_cast<const char16_t *>(begin); + const auto *const uend = reinterpret_cast<const char16_t *>(end); + auto *src = ubegin; buffer.resize(uend - ubegin + 1); char *dst = buffer.data(); @@ -93,14 +60,12 @@ static bool parseIp4Internal(IPv4Address &address, const char *ptr, bool acceptL ptr[1] != '.' && ptr[1] != '\0') return false; - const char *endptr; - bool ok; - quint64 ll = qstrntoull(ptr, stop - ptr, &endptr, 0, &ok); - quint32 x = ll; - if (!ok || endptr == ptr || ll != x) + auto [ll, used] = qstrntoull(ptr, stop - ptr, 0); + const quint32 x = quint32(ll); + if (used <= 0 || ll != x) return false; - if (*endptr == '.' || dotCount == 3) { + if (ptr[used] == '.' || dotCount == 3) { if (x & ~0xff) return false; address <<= 8; @@ -115,15 +80,13 @@ static bool parseIp4Internal(IPv4Address &address, const char *ptr, bool acceptL } address |= x; - if (dotCount == 3 && *endptr != '\0') - return false; - else if (dotCount == 3 || *endptr == '\0') - return true; - if (*endptr != '.') + if (dotCount == 3 || ptr[used] == '\0') + return ptr[used] == '\0'; + if (ptr[used] != '.') return false; ++dotCount; - ptr = endptr + 1; + ptr += used + 1; } return false; } @@ -132,12 +95,9 @@ void toString(QString &appendTo, IPv4Address address) { // reconstructing is easy // use the fast operator% that pre-calculates the size - appendTo += number(address >> 24) - % QLatin1Char('.') - % number(address >> 16) - % QLatin1Char('.') - % number(address >> 8) - % QLatin1Char('.') + appendTo += number(address >> 24) % u'.' + % number(address >> 16) % u'.' + % number(address >> 8) % u'.' % number(address); } @@ -214,18 +174,16 @@ const QChar *parseIp6(IPv6Address &address, const QChar *begin, const QChar *end continue; } - const char *endptr; - bool ok; - quint64 ll = qstrntoull(ptr, stop - ptr, &endptr, 16, &ok); + auto [ll, used] = qstrntoull(ptr, stop - ptr, 16); quint16 x = ll; // Reject malformed fields: // - failed to parse // - too many hex digits - if (!ok || endptr > ptr + 4) + if (used <= 0 || used > 4) return begin + (ptr - buffer.data()); - if (*endptr == '.') { + if (ptr[used] == '.') { // this could be an IPv4 address // it's only valid in the last element if (pos != 12) @@ -245,11 +203,11 @@ const QChar *parseIp6(IPv6Address &address, const QChar *begin, const QChar *end address[pos++] = x >> 8; address[pos++] = x & 0xff; - if (*endptr == '\0') + if (ptr[used] == '\0') break; - if (*endptr != ':') - return begin + (endptr - buffer.data()); - ptr = endptr + 1; + if (ptr[used] != ':') + return begin + (used + ptr - buffer.data()); + ptr += used + 1; } return pos == 16 ? nullptr : end; } @@ -285,7 +243,7 @@ void toString(QString &appendTo, const IPv6Address address) if (address[12] != 0 || address[13] != 0 || address[14] != 0) { embeddedIp4 = true; } else if (address[15] == 0) { - appendTo.append(QLatin1String("::")); + appendTo.append("::"_L1); return; } } |