diff options
Diffstat (limited to 'src/corelib/text/qstring.h')
-rw-r--r-- | src/corelib/text/qstring.h | 2188 |
1 files changed, 994 insertions, 1194 deletions
diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index ba21aba618..895ec4b5c0 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -1,44 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2019 Intel Corporation. -** Copyright (C) 2019 Mail.ru Group. -** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> -** 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) 2020 The Qt Company Ltd. +// Copyright (C) 2019 Intel Corporation. +// Copyright (C) 2019 Mail.ru Group. +// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QSTRING_H #define QSTRING_H @@ -48,16 +12,21 @@ #endif #include <QtCore/qchar.h> +#include <QtCore/qcompare.h> #include <QtCore/qbytearray.h> +#include <QtCore/qbytearrayview.h> #include <QtCore/qarraydata.h> +#include <QtCore/qlatin1stringview.h> #include <QtCore/qnamespace.h> #include <QtCore/qstringliteral.h> #include <QtCore/qstringalgorithms.h> -#include <QtCore/qstringview.h> +#include <QtCore/qanystringview.h> #include <QtCore/qstringtokenizer.h> #include <string> #include <iterator> +#include <QtCore/q20memory.h> +#include <string_view> #include <stdarg.h> @@ -70,237 +39,176 @@ Q_FORWARD_DECLARE_CF_TYPE(CFString); Q_FORWARD_DECLARE_OBJC_CLASS(NSString); #endif +class tst_QString; + QT_BEGIN_NAMESPACE class QRegularExpression; class QRegularExpressionMatch; class QString; -class QStringList; -class QStringRef; namespace QtPrivate { template <bool...B> class BoolList; -} - -class QLatin1String -{ -public: - Q_DECL_CONSTEXPR inline QLatin1String() noexcept : m_size(0), m_data(nullptr) {} - Q_DECL_CONSTEXPR inline explicit QLatin1String(const char *s) noexcept : m_size(s ? int(strlen(s)) : 0), m_data(s) {} - Q_DECL_CONSTEXPR explicit QLatin1String(const char *f, const char *l) - : QLatin1String(f, int(l - f)) {} - Q_DECL_CONSTEXPR inline explicit QLatin1String(const char *s, int sz) noexcept : m_size(sz), m_data(s) {} - inline explicit QLatin1String(const QByteArray &s) noexcept : m_size(int(qstrnlen(s.constData(), s.size()))), m_data(s.constData()) {} - - inline QString toString() const; - - Q_DECL_CONSTEXPR const char *latin1() const noexcept { return m_data; } - Q_DECL_CONSTEXPR int size() const noexcept { return m_size; } - Q_DECL_CONSTEXPR const char *data() const noexcept { return m_data; } - - Q_DECL_CONSTEXPR bool isNull() const noexcept { return !data(); } - Q_DECL_CONSTEXPR bool isEmpty() const noexcept { return !size(); } - - template <typename...Args> - Q_REQUIRED_RESULT inline QString arg(Args &&...args) const; - - Q_DECL_CONSTEXPR QLatin1Char at(int i) const - { return Q_ASSERT(i >= 0), Q_ASSERT(i < size()), QLatin1Char(m_data[i]); } - Q_DECL_CONSTEXPR QLatin1Char operator[](int i) const { return at(i); } - - Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QLatin1Char front() const { return at(0); } - Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QLatin1Char back() const { return at(size() - 1); } - - Q_REQUIRED_RESULT int compare(QStringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return QtPrivate::compareStrings(*this, other, cs); } - Q_REQUIRED_RESULT int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return QtPrivate::compareStrings(*this, other, cs); } - Q_REQUIRED_RESULT Q_DECL_CONSTEXPR int compare(QChar c) const noexcept - { return isEmpty() || front() == c ? size() - 1 : uchar(m_data[0]) - c.unicode() ; } - Q_REQUIRED_RESULT int compare(QChar c, Qt::CaseSensitivity cs) const noexcept - { return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs); } - - Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return QtPrivate::startsWith(*this, s, cs); } - Q_REQUIRED_RESULT bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return QtPrivate::startsWith(*this, s, cs); } - Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool startsWith(QChar c) const noexcept - { return !isEmpty() && front() == c; } - Q_REQUIRED_RESULT inline bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept - { return QtPrivate::startsWith(*this, QStringView(&c, 1), cs); } - - Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return QtPrivate::endsWith(*this, s, cs); } - Q_REQUIRED_RESULT bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return QtPrivate::endsWith(*this, s, cs); } - Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool endsWith(QChar c) const noexcept - { return !isEmpty() && back() == c; } - Q_REQUIRED_RESULT inline bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept - { return QtPrivate::endsWith(*this, QStringView(&c, 1), cs); } - - Q_REQUIRED_RESULT int indexOf(QStringView s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return int(QtPrivate::findString(*this, from, s, cs)); } // ### Qt6: qsizetype - Q_REQUIRED_RESULT int indexOf(QLatin1String s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return int(QtPrivate::findString(*this, from, s, cs)); } // ### Qt6: qsizetype - Q_REQUIRED_RESULT inline int indexOf(QChar c, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return int(QtPrivate::findString(*this, from, QStringView(&c, 1), cs)); } // ### Qt6: qsizetype - - Q_REQUIRED_RESULT bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return indexOf(s, 0, cs) != -1; } - Q_REQUIRED_RESULT bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return indexOf(s, 0, cs) != -1; } - Q_REQUIRED_RESULT inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return indexOf(QStringView(&c, 1), 0, cs) != -1; } - - Q_REQUIRED_RESULT int lastIndexOf(QStringView s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return int(QtPrivate::lastIndexOf(*this, from, s, cs)); } // ### Qt6: qsizetype - Q_REQUIRED_RESULT int lastIndexOf(QLatin1String s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return int(QtPrivate::lastIndexOf(*this, from, s, cs)); } // ### Qt6: qsizetype - Q_REQUIRED_RESULT inline int lastIndexOf(QChar c, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return int(QtPrivate::lastIndexOf(*this, from, QStringView(&c, 1), cs)); } // ### Qt6: qsizetype - - using value_type = const char; - using reference = value_type&; - using const_reference = reference; - using iterator = value_type*; - using const_iterator = iterator; - using difference_type = int; // violates Container concept requirements - using size_type = int; // violates Container concept requirements - - Q_DECL_CONSTEXPR const_iterator begin() const noexcept { return data(); } - Q_DECL_CONSTEXPR const_iterator cbegin() const noexcept { return data(); } - Q_DECL_CONSTEXPR const_iterator end() const noexcept { return data() + size(); } - Q_DECL_CONSTEXPR const_iterator cend() const noexcept { return data() + size(); } - - using reverse_iterator = std::reverse_iterator<iterator>; - using const_reverse_iterator = reverse_iterator; - - const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } - const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); } - const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } - const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); } - - constexpr QLatin1String mid(int pos, int n = -1) const - { - qsizetype p = pos; - qsizetype l = n; - using namespace QtPrivate; - auto result = QContainerImplHelper::mid(size(), &p, &l); - return result == QContainerImplHelper::Null ? QLatin1String() : QLatin1String(m_data + p, l); - } - constexpr QLatin1String left(int n) const - { - if (size_t(n) >= size_t(size())) - n = size(); - return QLatin1String(m_data, n); - } - constexpr QLatin1String right(int n) const - { - if (size_t(n) >= size_t(size())) - n = size(); - return QLatin1String(m_data + m_size - n, n); - } - Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QLatin1String chopped(int n) const - { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QLatin1String(m_data, m_size - n); } - - Q_DECL_RELAXED_CONSTEXPR void chop(int n) - { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= n; } - Q_DECL_RELAXED_CONSTEXPR void truncate(int n) - { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size = n; } - - Q_REQUIRED_RESULT QLatin1String trimmed() const noexcept { return QtPrivate::trimmed(*this); } - - template <typename Needle, typename...Flags> - Q_REQUIRED_RESULT inline constexpr auto tokenize(Needle &&needle, Flags...flags) const - noexcept(noexcept(qTokenize(std::declval<const QLatin1String &>(), std::forward<Needle>(needle), flags...))) - -> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...)) - { return qTokenize(*this, std::forward<Needle>(needle), flags...); } - - inline bool operator==(const QString &s) const noexcept; - inline bool operator!=(const QString &s) const noexcept; - inline bool operator>(const QString &s) const noexcept; - inline bool operator<(const QString &s) const noexcept; - inline bool operator>=(const QString &s) const noexcept; - inline bool operator<=(const QString &s) const noexcept; - -#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) - inline QT_ASCII_CAST_WARN bool operator==(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator<(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator>(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator<=(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator>=(const char *s) const; - - inline QT_ASCII_CAST_WARN bool operator==(const QByteArray &s) const; - inline QT_ASCII_CAST_WARN bool operator!=(const QByteArray &s) const; - inline QT_ASCII_CAST_WARN bool operator<(const QByteArray &s) const; - inline QT_ASCII_CAST_WARN bool operator>(const QByteArray &s) const; - inline QT_ASCII_CAST_WARN bool operator<=(const QByteArray &s) const; - inline QT_ASCII_CAST_WARN bool operator>=(const QByteArray &s) const; -#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) -private: - int m_size; - const char *m_data; -}; -Q_DECLARE_TYPEINFO(QLatin1String, Q_MOVABLE_TYPE); +template <typename Char> +using IsCompatibleChar32TypeHelper = + std::is_same<Char, char32_t>; +template <typename Char> +using IsCompatibleChar32Type + = IsCompatibleChar32TypeHelper<q20::remove_cvref_t<Char>>; +} // Qt 4.x compatibility // -// QLatin1String inline implementations +// QLatin1StringView inline implementations // -Q_DECL_CONSTEXPR bool QtPrivate::isLatin1(QLatin1String) noexcept +constexpr bool QtPrivate::isLatin1(QLatin1StringView) noexcept { return true; } // -// QStringView members that require QLatin1String: +// QStringView members that require QLatin1StringView: // -int QStringView::compare(QLatin1String s, Qt::CaseSensitivity cs) const noexcept +int QStringView::compare(QLatin1StringView s, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::compareStrings(*this, s, cs); } -bool QStringView::startsWith(QLatin1String s, Qt::CaseSensitivity cs) const noexcept +bool QStringView::startsWith(QLatin1StringView s, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::startsWith(*this, s, cs); } -bool QStringView::endsWith(QLatin1String s, Qt::CaseSensitivity cs) const noexcept +bool QStringView::endsWith(QLatin1StringView s, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::endsWith(*this, s, cs); } -qsizetype QStringView::indexOf(QLatin1String s, qsizetype from, Qt::CaseSensitivity cs) const noexcept +qsizetype QStringView::indexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::findString(*this, from, s, cs); } -bool QStringView::contains(QLatin1String s, Qt::CaseSensitivity cs) const noexcept +bool QStringView::contains(QLatin1StringView s, Qt::CaseSensitivity cs) const noexcept { return indexOf(s, 0, cs) != qsizetype(-1); } -qsizetype QStringView::lastIndexOf(QLatin1String s, qsizetype from, Qt::CaseSensitivity cs) const noexcept +qsizetype QStringView::lastIndexOf(QLatin1StringView s, Qt::CaseSensitivity cs) const noexcept +{ return QtPrivate::lastIndexOf(*this, size(), s, cs); } +qsizetype QStringView::lastIndexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::lastIndexOf(*this, from, s, cs); } +qsizetype QStringView::count(QLatin1StringView s, Qt::CaseSensitivity cs) const +{ return QtPrivate::count(*this, s, cs); } + +// +// QAnyStringView members that require QLatin1StringView +// + +constexpr QAnyStringView::QAnyStringView(QLatin1StringView str) noexcept + : m_data{str.data()}, m_size{size_t(str.size() << SizeShift) | Tag::Latin1} {} + +constexpr QLatin1StringView QAnyStringView::asLatin1StringView() const +{ + Q_ASSERT(isLatin1()); + return {m_data_utf8, size()}; +} + + +template <typename Visitor> +constexpr decltype(auto) QAnyStringView::visit(Visitor &&v) const +{ + if (isUtf16()) + return std::forward<Visitor>(v)(asStringView()); + else if (isLatin1()) + return std::forward<Visitor>(v)(asLatin1StringView()); + else + return std::forward<Visitor>(v)(asUtf8StringView()); +} + +// +// QAnyStringView members that require QAnyStringView::visit() +// + +constexpr QChar QAnyStringView::front() const +{ + return visit([] (auto that) { return QAnyStringView::toQChar(that.front()); }); +} +constexpr QChar QAnyStringView::back() const +{ + return visit([] (auto that) { return QAnyStringView::toQChar(that.back()); }); +} + class Q_CORE_EXPORT QString { typedef QTypedArrayData<char16_t> Data; + + friend class ::tst_QString; + + template <typename Iterator> + static constexpr bool is_contiguous_iterator_v = + // Can't use contiguous_iterator_tag here, as STL impls can't agree on feature macro. + // To avoid differences in C++20 and C++17 builds, treat only pointers as contiguous + // for now: + // std::contiguous_iterator<Iterator>; + std::is_pointer_v<Iterator>; + + template <typename Char> + using is_compatible_char_helper = std::disjunction< + QtPrivate::IsCompatibleCharType<Char>, + QtPrivate::IsCompatibleChar32Type<Char>, + QtPrivate::IsCompatibleChar8Type<Char>, + std::is_same<Char, QLatin1Char> // special case + >; + + template <typename Iterator> + static constexpr bool is_compatible_iterator_v = std::conjunction_v< + std::is_convertible< + typename std::iterator_traits<Iterator>::iterator_category, + std::input_iterator_tag + >, + is_compatible_char_helper<typename std::iterator_traits<Iterator>::value_type> + >; + + template <typename Iterator> + using if_compatible_iterator = std::enable_if_t<is_compatible_iterator_v<Iterator>, bool>; + public: typedef QStringPrivate DataPointer; - inline constexpr QString() noexcept; + constexpr QString() noexcept; explicit QString(const QChar *unicode, qsizetype size = -1); QString(QChar c); QString(qsizetype size, QChar c); - inline QString(QLatin1String latin1); + inline QString(QLatin1StringView latin1); + explicit QString(QStringView sv) : QString(sv.data(), sv.size()) {} +#if defined(__cpp_char8_t) || defined(Q_QDOC) + Q_WEAK_OVERLOAD + inline QString(const char8_t *str) + : QString(fromUtf8(str)) + {} +#endif inline QString(const QString &) noexcept; inline ~QString(); QString &operator=(QChar c); QString &operator=(const QString &) noexcept; - QString &operator=(QLatin1String latin1); + QString &operator=(QLatin1StringView latin1); inline QString(QString &&other) noexcept - { qSwap(d, other.d); } - inline QString &operator=(QString &&other) noexcept - { qSwap(d, other.d); return *this; } - inline void swap(QString &other) noexcept { qSwap(d, other.d); } - inline qsizetype size() const { return d.size; } + = default; + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QString) + void swap(QString &other) noexcept { d.swap(other.d); } + inline qsizetype size() const noexcept { return d.size; } +#if QT_DEPRECATED_SINCE(6, 4) + QT_DEPRECATED_VERSION_X_6_4("Use size() or length() instead.") inline qsizetype count() const { return d.size; } - inline qsizetype length() const { return d.size; } - inline bool isEmpty() const; +#endif + inline qsizetype length() const noexcept { return d.size; } + inline bool isEmpty() const noexcept { return d.size == 0; } void resize(qsizetype size); void resize(qsizetype size, QChar fillChar); + void resizeForOverwrite(qsizetype size); QString &fill(QChar c, qsizetype size = -1); void truncate(qsizetype pos); void chop(qsizetype n); + QString &slice(qsizetype pos) + { verify(pos, 0); return remove(0, pos); } + QString &slice(qsizetype pos, qsizetype n) + { + verify(pos, n); + if (isNull()) + return *this; + resize(pos + n); + return remove(0, pos); + } + inline qsizetype capacity() const; inline void reserve(qsizetype size); inline void squeeze(); @@ -313,61 +221,56 @@ public: inline void detach(); inline bool isDetached() const; inline bool isSharedWith(const QString &other) const { return d.isSharedWith(other.d); } - void clear(); + inline void clear(); inline const QChar at(qsizetype i) const; - const QChar operator[](qsizetype i) const; - Q_REQUIRED_RESULT QChar &operator[](qsizetype i); - - Q_REQUIRED_RESULT inline QChar front() const { return at(0); } - Q_REQUIRED_RESULT inline QChar &front(); - Q_REQUIRED_RESULT inline QChar back() const { return at(size() - 1); } - Q_REQUIRED_RESULT inline QChar &back(); - - Q_REQUIRED_RESULT QString arg(qlonglong a, int fieldwidth=0, int base=10, - QChar fillChar = QLatin1Char(' ')) const; - Q_REQUIRED_RESULT QString arg(qulonglong a, int fieldwidth=0, int base=10, - QChar fillChar = QLatin1Char(' ')) const; - Q_REQUIRED_RESULT QString arg(long a, int fieldwidth=0, int base=10, - QChar fillChar = QLatin1Char(' ')) const; - Q_REQUIRED_RESULT QString arg(ulong a, int fieldwidth=0, int base=10, - QChar fillChar = QLatin1Char(' ')) const; - Q_REQUIRED_RESULT QString arg(int a, int fieldWidth = 0, int base = 10, - QChar fillChar = QLatin1Char(' ')) const; - Q_REQUIRED_RESULT QString arg(uint a, int fieldWidth = 0, int base = 10, - QChar fillChar = QLatin1Char(' ')) const; - Q_REQUIRED_RESULT QString arg(short a, int fieldWidth = 0, int base = 10, - QChar fillChar = QLatin1Char(' ')) const; - Q_REQUIRED_RESULT QString arg(ushort a, int fieldWidth = 0, int base = 10, - QChar fillChar = QLatin1Char(' ')) const; - Q_REQUIRED_RESULT QString arg(double a, int fieldWidth = 0, char fmt = 'g', int prec = -1, - QChar fillChar = QLatin1Char(' ')) const; - Q_REQUIRED_RESULT QString arg(char a, int fieldWidth = 0, - QChar fillChar = QLatin1Char(' ')) const; - Q_REQUIRED_RESULT QString arg(QChar a, int fieldWidth = 0, - QChar fillChar = QLatin1Char(' ')) const; -#if QT_STRINGVIEW_LEVEL < 2 - Q_REQUIRED_RESULT QString arg(const QString &a, int fieldWidth = 0, - QChar fillChar = QLatin1Char(' ')) const; -#endif - Q_REQUIRED_RESULT QString arg(QStringView a, int fieldWidth = 0, - QChar fillChar = QLatin1Char(' ')) const; - Q_REQUIRED_RESULT QString arg(QLatin1String a, int fieldWidth = 0, - QChar fillChar = QLatin1Char(' ')) const; + inline const QChar operator[](qsizetype i) const; + [[nodiscard]] inline QChar &operator[](qsizetype i); + + [[nodiscard]] inline QChar front() const { return at(0); } + [[nodiscard]] inline QChar &front(); + [[nodiscard]] inline QChar back() const { return at(size() - 1); } + [[nodiscard]] inline QChar &back(); + + [[nodiscard]] QString arg(qlonglong a, int fieldwidth=0, int base=10, + QChar fillChar = u' ') const; + [[nodiscard]] QString arg(qulonglong a, int fieldwidth=0, int base=10, + QChar fillChar = u' ') const; + [[nodiscard]] inline QString arg(long a, int fieldwidth=0, int base=10, + QChar fillChar = u' ') const; + [[nodiscard]] inline QString arg(ulong a, int fieldwidth=0, int base=10, + QChar fillChar = u' ') const; + [[nodiscard]] inline QString arg(int a, int fieldWidth = 0, int base = 10, + QChar fillChar = u' ') const; + [[nodiscard]] inline QString arg(uint a, int fieldWidth = 0, int base = 10, + QChar fillChar = u' ') const; + [[nodiscard]] inline QString arg(short a, int fieldWidth = 0, int base = 10, + QChar fillChar = u' ') const; + [[nodiscard]] inline QString arg(ushort a, int fieldWidth = 0, int base = 10, + QChar fillChar = u' ') const; + [[nodiscard]] QString arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, + QChar fillChar = u' ') const; + [[nodiscard]] QString arg(char a, int fieldWidth = 0, + QChar fillChar = u' ') const; + [[nodiscard]] QString arg(QChar a, int fieldWidth = 0, + QChar fillChar = u' ') const; + [[nodiscard]] QString arg(const QString &a, int fieldWidth = 0, + QChar fillChar = u' ') const; + [[nodiscard]] QString arg(QStringView a, int fieldWidth = 0, + QChar fillChar = u' ') const; + [[nodiscard]] QString arg(QLatin1StringView a, int fieldWidth = 0, + QChar fillChar = u' ') const; private: template <typename T> - struct is_convertible_to_view_or_qstring_helper - : std::integral_constant<bool, - std::is_convertible<T, QString>::value || - std::is_convertible<T, QStringView>::value || - std::is_convertible<T, QLatin1String>::value> {}; - template <typename T> - struct is_convertible_to_view_or_qstring - : is_convertible_to_view_or_qstring_helper<typename std::decay<T>::type> {}; + using is_convertible_to_view_or_qstring = std::disjunction< + std::is_convertible<T, QString>, + std::is_convertible<T, QStringView>, + std::is_convertible<T, QLatin1StringView> + >; public: template <typename...Args> - Q_REQUIRED_RESULT -#ifdef Q_CLANG_QDOC + [[nodiscard]] +#ifdef Q_QDOC QString #else typename std::enable_if< @@ -384,39 +287,51 @@ public: static QString vasprintf(const char *format, va_list ap) Q_ATTRIBUTE_FORMAT_PRINTF(1, 0); static QString asprintf(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2); + [[nodiscard]] QT_CORE_INLINE_SINCE(6, 7) qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - qsizetype indexOf(QLatin1String s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#if QT_STRINGVIEW_LEVEL < 2 - qsizetype indexOf(const QString &s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#endif - Q_REQUIRED_RESULT qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + [[nodiscard]] qsizetype indexOf(QLatin1StringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + [[nodiscard]] qsizetype indexOf(const QString &s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::findString(*this, from, s, cs); } - qsizetype lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - qsizetype lastIndexOf(QLatin1String s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#if QT_STRINGVIEW_LEVEL < 2 - qsizetype lastIndexOf(const QString &s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#endif - - Q_REQUIRED_RESULT qsizetype lastIndexOf(QStringView s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return int(QtPrivate::lastIndexOf(*this, from, s, cs)); } // ### Qt6: qsizetype - - inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#if QT_STRINGVIEW_LEVEL < 2 - inline bool contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#endif - inline bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - inline bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; - qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - qsizetype count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + [[nodiscard]] qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return lastIndexOf(c, -1, cs); } + [[nodiscard]] QT_CORE_INLINE_SINCE(6, 7) + qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + [[nodiscard]] qsizetype lastIndexOf(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + { return lastIndexOf(s, size(), cs); } + [[nodiscard]] qsizetype lastIndexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + [[nodiscard]] qsizetype lastIndexOf(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + { return lastIndexOf(s, size(), cs); } + [[nodiscard]] qsizetype lastIndexOf(const QString &s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + + [[nodiscard]] qsizetype lastIndexOf(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return lastIndexOf(s, size(), cs); } + [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return QtPrivate::lastIndexOf(*this, from, s, cs); } + + [[nodiscard]] inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + [[nodiscard]] inline bool contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + [[nodiscard]] inline bool contains(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + [[nodiscard]] inline bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + [[nodiscard]] qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + [[nodiscard]] qsizetype count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + [[nodiscard]] qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; #if QT_CONFIG(regularexpression) - qsizetype indexOf(const QRegularExpression &re, qsizetype from = 0, - QRegularExpressionMatch *rmatch = nullptr) const; - qsizetype lastIndexOf(const QRegularExpression &re, qsizetype from = -1, - QRegularExpressionMatch *rmatch = nullptr) const; - bool contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const; - qsizetype count(const QRegularExpression &re) const; + [[nodiscard]] qsizetype indexOf(const QRegularExpression &re, qsizetype from = 0, + QRegularExpressionMatch *rmatch = nullptr) const; +#ifdef Q_QDOC + [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const; +#else + // prevent an ambiguity when called like this: lastIndexOf(re, 0) + template <typename T = QRegularExpressionMatch, std::enable_if_t<std::is_same_v<T, QRegularExpressionMatch>, bool> = false> + [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, T *rmatch = nullptr) const + { return lastIndexOf(re, size(), rmatch); } +#endif + [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, qsizetype from, + QRegularExpressionMatch *rmatch = nullptr) const; + [[nodiscard]] bool contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const; + [[nodiscard]] qsizetype count(const QRegularExpression &re) const; #endif enum SectionFlag { @@ -428,147 +343,239 @@ public: }; Q_DECLARE_FLAGS(SectionFlags, SectionFlag) - QString section(QChar sep, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const; - QString section(const QString &in_sep, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const; + [[nodiscard]] inline QString section(QChar sep, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const; + [[nodiscard]] QString section(const QString &in_sep, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const; #if QT_CONFIG(regularexpression) - QString section(const QRegularExpression &re, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const; + [[nodiscard]] QString section(const QRegularExpression &re, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const; #endif - Q_REQUIRED_RESULT QString left(qsizetype n) const; - Q_REQUIRED_RESULT QString right(qsizetype n) const; - Q_REQUIRED_RESULT QString mid(qsizetype position, qsizetype n = -1) const; - - Q_REQUIRED_RESULT QString first(qsizetype n) const - { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QString(data(), n); } - Q_REQUIRED_RESULT QString last(qsizetype n) const - { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QString(data() + size() - n, n); } - Q_REQUIRED_RESULT QString sliced(qsizetype pos) const - { Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); return QString(data() + pos, size() - pos); } - Q_REQUIRED_RESULT QString sliced(qsizetype pos, qsizetype n) const - { Q_ASSERT(pos >= 0); Q_ASSERT(n >= 0); Q_ASSERT(size_t(pos) + size_t(n) <= size_t(size())); return QString(data() + pos, n); } - Q_REQUIRED_RESULT QString chopped(qsizetype n) const - { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return first(size() - n); } - - - Q_REQUIRED_RESULT QStringRef leftRef(int n) const; - Q_REQUIRED_RESULT QStringRef rightRef(int n) const; - Q_REQUIRED_RESULT QStringRef midRef(int position, int n = -1) const; - -#if QT_STRINGVIEW_LEVEL < 2 - bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + +#if QT_CORE_REMOVED_SINCE(6, 7) + QString left(qsizetype n) const; + QString right(qsizetype n) const; + QString mid(qsizetype position, qsizetype n = -1) const; + + QString first(qsizetype n) const; + QString last(qsizetype n) const; + QString sliced(qsizetype pos) const; + QString sliced(qsizetype pos, qsizetype n) const; + QString chopped(qsizetype n) const; +#else + [[nodiscard]] QString left(qsizetype n) const & + { + if (size_t(n) >= size_t(size())) + return *this; + return first(n); + } + [[nodiscard]] QString left(qsizetype n) && + { + if (size_t(n) >= size_t(size())) + return std::move(*this); + return std::move(*this).first(n); + } + [[nodiscard]] QString right(qsizetype n) const & + { + if (size_t(n) >= size_t(size())) + return *this; + return last(n); + } + [[nodiscard]] QString right(qsizetype n) && + { + if (size_t(n) >= size_t(size())) + return std::move(*this); + return std::move(*this).last(n); + } + [[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) const &; + [[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) &&; + + [[nodiscard]] QString first(qsizetype n) const & + { verify(0, n); return sliced(0, n); } + [[nodiscard]] QString last(qsizetype n) const & + { verify(0, n); return sliced(size() - n, n); } + [[nodiscard]] QString sliced(qsizetype pos) const & + { verify(pos, 0); return sliced(pos, size() - pos); } + [[nodiscard]] QString sliced(qsizetype pos, qsizetype n) const & + { verify(pos, n); return QString(begin() + pos, n); } + [[nodiscard]] QString chopped(qsizetype n) const & + { verify(0, n); return sliced(0, size() - n); } + + [[nodiscard]] QString first(qsizetype n) && + { + verify(0, n); + resize(n); // may detach and allocate memory + return std::move(*this); + } + [[nodiscard]] QString last(qsizetype n) && + { verify(0, n); return sliced_helper(*this, size() - n, n); } + [[nodiscard]] QString sliced(qsizetype pos) && + { verify(pos, 0); return sliced_helper(*this, pos, size() - pos); } + [[nodiscard]] QString sliced(qsizetype pos, qsizetype n) && + { verify(pos, n); return sliced_helper(*this, pos, n); } + [[nodiscard]] QString chopped(qsizetype n) && + { verify(0, n); return std::move(*this).first(size() - n); } #endif - Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::startsWith(*this, s, cs); } - bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + bool startsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#if QT_STRINGVIEW_LEVEL < 2 bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#endif - Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::endsWith(*this, s, cs); } - bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; bool isUpper() const; bool isLower() const; - Q_REQUIRED_RESULT QString leftJustified(qsizetype width, QChar fill = QLatin1Char(' '), bool trunc = false) const; - Q_REQUIRED_RESULT QString rightJustified(qsizetype width, QChar fill = QLatin1Char(' '), bool trunc = false) const; - -#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC) -# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !__has_cpp_attribute(nodiscard) - // required due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61941 -# pragma push_macro("Q_REQUIRED_RESULT") -# undef Q_REQUIRED_RESULT -# define Q_REQUIRED_RESULT -# define Q_REQUIRED_RESULT_pushed -# endif - Q_REQUIRED_RESULT QString toLower() const & + [[nodiscard]] QString leftJustified(qsizetype width, QChar fill = u' ', bool trunc = false) const; + [[nodiscard]] QString rightJustified(qsizetype width, QChar fill = u' ', bool trunc = false) const; + +#if !defined(Q_QDOC) + [[nodiscard]] QString toLower() const & { return toLower_helper(*this); } - Q_REQUIRED_RESULT QString toLower() && + [[nodiscard]] QString toLower() && { return toLower_helper(*this); } - Q_REQUIRED_RESULT QString toUpper() const & + [[nodiscard]] QString toUpper() const & { return toUpper_helper(*this); } - Q_REQUIRED_RESULT QString toUpper() && + [[nodiscard]] QString toUpper() && { return toUpper_helper(*this); } - Q_REQUIRED_RESULT QString toCaseFolded() const & + [[nodiscard]] QString toCaseFolded() const & { return toCaseFolded_helper(*this); } - Q_REQUIRED_RESULT QString toCaseFolded() && + [[nodiscard]] QString toCaseFolded() && { return toCaseFolded_helper(*this); } - Q_REQUIRED_RESULT QString trimmed() const & + [[nodiscard]] QString trimmed() const & { return trimmed_helper(*this); } - Q_REQUIRED_RESULT QString trimmed() && + [[nodiscard]] QString trimmed() && { return trimmed_helper(*this); } - Q_REQUIRED_RESULT QString simplified() const & + [[nodiscard]] QString simplified() const & { return simplified_helper(*this); } - Q_REQUIRED_RESULT QString simplified() && + [[nodiscard]] QString simplified() && { return simplified_helper(*this); } -# ifdef Q_REQUIRED_RESULT_pushed -# pragma pop_macro("Q_REQUIRED_RESULT") -# endif #else - Q_REQUIRED_RESULT QString toLower() const; - Q_REQUIRED_RESULT QString toUpper() const; - Q_REQUIRED_RESULT QString toCaseFolded() const; - Q_REQUIRED_RESULT QString trimmed() const; - Q_REQUIRED_RESULT QString simplified() const; + [[nodiscard]] QString toLower() const; + [[nodiscard]] QString toUpper() const; + [[nodiscard]] QString toCaseFolded() const; + [[nodiscard]] QString trimmed() const; + [[nodiscard]] QString simplified() const; #endif - Q_REQUIRED_RESULT QString toHtmlEscaped() const; + [[nodiscard]] QString toHtmlEscaped() const; QString &insert(qsizetype i, QChar c); QString &insert(qsizetype i, const QChar *uc, qsizetype len); -#if QT_STRINGVIEW_LEVEL < 2 - inline QString &insert(qsizetype i, const QString &s) { return insert(i, s.constData(), s.length()); } -#endif - inline QString &insert(qsizetype i, QStringView v) { return insert(i, v.data(), v.length()); } - QString &insert(qsizetype i, QLatin1String s); + inline QString &insert(qsizetype i, const QString &s) { return insert(i, s.constData(), s.size()); } + inline QString &insert(qsizetype i, QStringView v) { return insert(i, v.data(), v.size()); } + QString &insert(qsizetype i, QLatin1StringView s); + QString &insert(qsizetype i, QUtf8StringView s); QString &append(QChar c); QString &append(const QChar *uc, qsizetype len); -#if QT_STRINGVIEW_LEVEL < 2 QString &append(const QString &s); -#endif - inline QString &append(QStringView v) { return append(v.data(), v.length()); } - QString &append(QLatin1String s); + inline QString &append(QStringView v) { return append(v.data(), v.size()); } + QString &append(QLatin1StringView s); + QString &append(QUtf8StringView s); inline QString &prepend(QChar c) { return insert(0, c); } inline QString &prepend(const QChar *uc, qsizetype len) { return insert(0, uc, len); } -#if QT_STRINGVIEW_LEVEL < 2 inline QString &prepend(const QString &s) { return insert(0, s); } -#endif - inline QString &prepend(QStringView v) { return prepend(v.data(), v.length()); } - inline QString &prepend(QLatin1String s) { return insert(0, s); } - - inline QString &operator+=(QChar c) { - if (d->needsDetach() || int(d.size + 1) > capacity()) - reallocData(uint(d.size) + 2u, true); - d->data()[d.size++] = c.unicode(); - d->data()[d.size] = '\0'; - return *this; + inline QString &prepend(QStringView v) { return prepend(v.data(), v.size()); } + inline QString &prepend(QLatin1StringView s) { return insert(0, s); } + QString &prepend(QUtf8StringView s) { return insert(0, s); } + + QString &assign(QAnyStringView s); + inline QString &assign(qsizetype n, QChar c) + { + Q_ASSERT(n >= 0); + return fill(c, n); + } + template <typename InputIterator, if_compatible_iterator<InputIterator> = true> + QString &assign(InputIterator first, InputIterator last) + { + using V = typename std::iterator_traits<InputIterator>::value_type; + constexpr bool IsL1C = std::is_same_v<std::remove_cv_t<V>, QLatin1Char>; + constexpr bool IsFwdIt = std::is_convertible_v< + typename std::iterator_traits<InputIterator>::iterator_category, + std::forward_iterator_tag + >; + + if constexpr (is_contiguous_iterator_v<InputIterator>) { + const auto p = q20::to_address(first); + const auto len = qsizetype(last - first); + if constexpr (IsL1C) + return assign(QLatin1StringView(reinterpret_cast<const char*>(p), len)); + else if constexpr (sizeof(V) == 4) + return assign_helper(p, len); + else + return assign(QAnyStringView(p, len)); + } else if constexpr (sizeof(V) == 4) { // non-contiguous iterator, feed data piecemeal + resize(0); + if constexpr (IsFwdIt) { + const qsizetype requiredCapacity = 2 * std::distance(first, last); + reserve(requiredCapacity); + } + while (first != last) { + append(QChar::fromUcs4(*first)); + ++first; + } + return *this; + } else if constexpr (QtPrivate::IsCompatibleChar8Type<V>::value) { + assign_helper_char8(first, last); + d.data()[d.size] = u'\0'; + return *this; + } else { + d.assign(first, last, [](QChar ch) -> char16_t { return ch.unicode(); }); + d.data()[d.size] = u'\0'; + return *this; + } } -#if QT_STRINGVIEW_LEVEL < 2 + inline QString &operator+=(QChar c) { return append(c); } + inline QString &operator+=(const QString &s) { return append(s); } -#endif inline QString &operator+=(QStringView v) { return append(v); } - inline QString &operator+=(QLatin1String s) { return append(s); } + inline QString &operator+=(QLatin1StringView s) { return append(s); } + QString &operator+=(QUtf8StringView s) { return append(s); } + +#if defined(QT_RESTRICTED_CAST_FROM_ASCII) + template <qsizetype N> + QString &insert(qsizetype i, const char (&ch)[N]) { return insert(i, QUtf8StringView(ch)); } + template <qsizetype N> + QString &append(const char (&ch)[N]) { return append(QUtf8StringView(ch)); } + template <qsizetype N> + QString &prepend(const char (&ch)[N]) { return prepend(QUtf8StringView(ch)); } + template <qsizetype N> + QString &operator+=(const char (&ch)[N]) { return append(QUtf8StringView(ch)); } +#endif QString &remove(qsizetype i, qsizetype len); QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive); - QString &remove(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive); + QString &remove(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive); QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive); + + QString &removeAt(qsizetype pos) + { return size_t(pos) < size_t(size()) ? remove(pos, 1) : *this; } + QString &removeFirst() { return !isEmpty() ? remove(0, 1) : *this; } + QString &removeLast() { return !isEmpty() ? remove(size() - 1, 1) : *this; } + + template <typename Predicate> + QString &removeIf(Predicate pred) + { + removeIf_helper(pred); + return *this; + } + QString &replace(qsizetype i, qsizetype len, QChar after); QString &replace(qsizetype i, qsizetype len, const QChar *s, qsizetype slen); QString &replace(qsizetype i, qsizetype len, const QString &after); QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive); QString &replace(const QChar *before, qsizetype blen, const QChar *after, qsizetype alen, Qt::CaseSensitivity cs = Qt::CaseSensitive); - QString &replace(QLatin1String before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive); - QString &replace(QLatin1String before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive); - QString &replace(const QString &before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive); + QString &replace(QLatin1StringView before, QLatin1StringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive); + QString &replace(QLatin1StringView before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive); + QString &replace(const QString &before, QLatin1StringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive); QString &replace(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive); QString &replace(QChar c, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive); - QString &replace(QChar c, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive); + QString &replace(QChar c, QLatin1StringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive); #if QT_CONFIG(regularexpression) QString &replace(const QRegularExpression &re, const QString &after); inline QString &remove(const QRegularExpression &re) @@ -576,40 +583,32 @@ public: #endif public: - Q_REQUIRED_RESULT + [[nodiscard]] QStringList split(const QString &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - Q_REQUIRED_RESULT - QList<QStringRef> splitRef(const QString &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, - Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - Q_REQUIRED_RESULT + [[nodiscard]] QStringList split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - Q_REQUIRED_RESULT - QList<QStringRef> splitRef(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, - Qt::CaseSensitivity cs = Qt::CaseSensitive) const; #ifndef QT_NO_REGULAREXPRESSION - Q_REQUIRED_RESULT + [[nodiscard]] QStringList split(const QRegularExpression &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const; - Q_REQUIRED_RESULT - QList<QStringRef> splitRef(const QRegularExpression &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const; #endif template <typename Needle, typename...Flags> - Q_REQUIRED_RESULT inline auto tokenize(Needle &&needle, Flags...flags) const & + [[nodiscard]] inline auto tokenize(Needle &&needle, Flags...flags) const & noexcept(noexcept(qTokenize(std::declval<const QString &>(), std::forward<Needle>(needle), flags...))) -> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...)) { return qTokenize(qToStringViewIgnoringNull(*this), std::forward<Needle>(needle), flags...); } template <typename Needle, typename...Flags> - Q_REQUIRED_RESULT inline auto tokenize(Needle &&needle, Flags...flags) const && + [[nodiscard]] inline auto tokenize(Needle &&needle, Flags...flags) const && noexcept(noexcept(qTokenize(std::declval<const QString>(), std::forward<Needle>(needle), flags...))) -> decltype(qTokenize(std::move(*this), std::forward<Needle>(needle), flags...)) { return qTokenize(std::move(*this), std::forward<Needle>(needle), flags...); } template <typename Needle, typename...Flags> - Q_REQUIRED_RESULT inline auto tokenize(Needle &&needle, Flags...flags) && + [[nodiscard]] inline auto tokenize(Needle &&needle, Flags...flags) && noexcept(noexcept(qTokenize(std::declval<QString>(), std::forward<Needle>(needle), flags...))) -> decltype(qTokenize(std::move(*this), std::forward<Needle>(needle), flags...)) { return qTokenize(std::move(*this), std::forward<Needle>(needle), flags...); } @@ -621,56 +620,62 @@ public: NormalizationForm_KD, NormalizationForm_KC }; - Q_REQUIRED_RESULT QString normalized(NormalizationForm mode, QChar::UnicodeVersion version = QChar::Unicode_Unassigned) const; + [[nodiscard]] QString normalized(NormalizationForm mode, QChar::UnicodeVersion version = QChar::Unicode_Unassigned) const; - Q_REQUIRED_RESULT QString repeated(qsizetype times) const; + [[nodiscard]] QString repeated(qsizetype times) const; - const ushort *utf16() const; + const ushort *utf16() const; // ### Qt 7 char16_t -#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC) - Q_REQUIRED_RESULT QByteArray toLatin1() const & +#if !defined(Q_QDOC) + [[nodiscard]] QByteArray toLatin1() const & { return toLatin1_helper(*this); } - Q_REQUIRED_RESULT QByteArray toLatin1() && + [[nodiscard]] QByteArray toLatin1() && { return toLatin1_helper_inplace(*this); } - Q_REQUIRED_RESULT QByteArray toUtf8() const & + [[nodiscard]] QByteArray toUtf8() const & { return toUtf8_helper(*this); } - Q_REQUIRED_RESULT QByteArray toUtf8() && + [[nodiscard]] QByteArray toUtf8() && { return toUtf8_helper(*this); } - Q_REQUIRED_RESULT QByteArray toLocal8Bit() const & + [[nodiscard]] QByteArray toLocal8Bit() const & { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); } - Q_REQUIRED_RESULT QByteArray toLocal8Bit() && + [[nodiscard]] QByteArray toLocal8Bit() && { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); } #else - Q_REQUIRED_RESULT QByteArray toLatin1() const; - Q_REQUIRED_RESULT QByteArray toUtf8() const; - Q_REQUIRED_RESULT QByteArray toLocal8Bit() const; + [[nodiscard]] QByteArray toLatin1() const; + [[nodiscard]] QByteArray toUtf8() const; + [[nodiscard]] QByteArray toLocal8Bit() const; #endif - Q_REQUIRED_RESULT QList<uint> toUcs4() const; + [[nodiscard]] QList<uint> toUcs4() const; // ### Qt 7 char32_t // note - this are all inline so we can benefit from strlen() compile time optimizations - static inline QString fromLatin1(const char *str, qsizetype size = -1) + static QString fromLatin1(QByteArrayView ba); + Q_WEAK_OVERLOAD + static inline QString fromLatin1(const QByteArray &ba) { return fromLatin1(QByteArrayView(ba)); } + static inline QString fromLatin1(const char *str, qsizetype size) { - return QString(fromLatin1_helper(str, (str && size == -1) ? qsizetype(strlen(str)) : size)); + return fromLatin1(QByteArrayView(str, !str || size < 0 ? qstrlen(str) : size)); } - static inline QString fromUtf8(const char *str, qsizetype size = -1) + static QString fromUtf8(QByteArrayView utf8); + Q_WEAK_OVERLOAD + static inline QString fromUtf8(const QByteArray &ba) { return fromUtf8(QByteArrayView(ba)); } + static inline QString fromUtf8(const char *utf8, qsizetype size) { - return fromUtf8_helper(str, (str && size == -1) ? qsizetype(strlen(str)) : size); + return fromUtf8(QByteArrayView(utf8, !utf8 || size < 0 ? qstrlen(utf8) : size)); } -#ifdef __cpp_char8_t +#if defined(__cpp_char8_t) || defined(Q_QDOC) Q_WEAK_OVERLOAD - static inline QString fromUtf8(const char8_t *str, qsizetype size = -1) - { return fromUtf8(reinterpret_cast<const char *>(str), int(size)); } + static inline QString fromUtf8(const char8_t *str) + { return fromUtf8(reinterpret_cast<const char *>(str)); } + Q_WEAK_OVERLOAD + static inline QString fromUtf8(const char8_t *str, qsizetype size) + { return fromUtf8(reinterpret_cast<const char *>(str), size); } #endif - static inline QString fromLocal8Bit(const char *str, qsizetype size = -1) + static QString fromLocal8Bit(QByteArrayView ba); + Q_WEAK_OVERLOAD + static inline QString fromLocal8Bit(const QByteArray &ba) { return fromLocal8Bit(QByteArrayView(ba)); } + static inline QString fromLocal8Bit(const char *str, qsizetype size) { - return fromLocal8Bit_helper(str, (str && size == -1) ? qsizetype(strlen(str)) : size); + return fromLocal8Bit(QByteArrayView(str, !str || size < 0 ? qstrlen(str) : size)); } - static inline QString fromLatin1(const QByteArray &str) - { return str.isNull() ? QString() : fromLatin1(str.data(), qstrnlen(str.constData(), str.size())); } - static inline QString fromUtf8(const QByteArray &str) - { return str.isNull() ? QString() : fromUtf8(str.data(), qstrnlen(str.constData(), str.size())); } - static inline QString fromLocal8Bit(const QByteArray &str) - { return str.isNull() ? QString() : fromLocal8Bit(str.data(), qstrnlen(str.constData(), str.size())); } static QString fromUtf16(const char16_t *, qsizetype size = -1); static QString fromUcs4(const char32_t *, qsizetype size = -1); static QString fromRawData(const QChar *, qsizetype size); @@ -685,16 +690,14 @@ public: #endif inline qsizetype toWCharArray(wchar_t *array) const; - Q_REQUIRED_RESULT static inline QString fromWCharArray(const wchar_t *string, qsizetype size = -1); + [[nodiscard]] static inline QString fromWCharArray(const wchar_t *string, qsizetype size = -1); QString &setRawData(const QChar *unicode, qsizetype size); QString &setUnicode(const QChar *unicode, qsizetype size); - inline QString &setUtf16(const ushort *utf16, qsizetype size); + inline QString &setUtf16(const ushort *utf16, qsizetype size); // ### Qt 7 char16_t -#if QT_STRINGVIEW_LEVEL < 2 int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; -#endif - int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + int compare(QLatin1StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; inline int compare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; int compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return compare(QStringView{&ch, 1}, cs); } @@ -703,10 +706,10 @@ public: Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept { return s1.compare(s2, cs); } - static inline int compare(const QString &s1, QLatin1String s2, + static inline int compare(const QString &s1, QLatin1StringView s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept { return s1.compare(s2, cs); } - static inline int compare(QLatin1String s1, const QString &s2, + static inline int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept { return -s2.compare(s1, cs); } static int compare(const QString &s1, QStringView s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept @@ -715,34 +718,41 @@ public: { return -s2.compare(s1, cs); } int localeAwareCompare(const QString& s) const; - int localeAwareCompare(QStringView s) const; + inline int localeAwareCompare(QStringView s) const; static int localeAwareCompare(const QString& s1, const QString& s2) { return s1.localeAwareCompare(s2); } - static int localeAwareCompare(QStringView s1, QStringView s2); - - // ### Qt6: make inline except for the long long versions - short toShort(bool *ok=nullptr, int base=10) const; - ushort toUShort(bool *ok=nullptr, int base=10) const; - int toInt(bool *ok=nullptr, int base=10) const; - uint toUInt(bool *ok=nullptr, int base=10) const; - long toLong(bool *ok=nullptr, int base=10) const; - ulong toULong(bool *ok=nullptr, int base=10) const; + static inline int localeAwareCompare(QStringView s1, QStringView s2); + + short toShort(bool *ok=nullptr, int base=10) const + { return toIntegral_helper<short>(*this, ok, base); } + ushort toUShort(bool *ok=nullptr, int base=10) const + { return toIntegral_helper<ushort>(*this, ok, base); } + int toInt(bool *ok=nullptr, int base=10) const + { return toIntegral_helper<int>(*this, ok, base); } + uint toUInt(bool *ok=nullptr, int base=10) const + { return toIntegral_helper<uint>(*this, ok, base); } + long toLong(bool *ok=nullptr, int base=10) const + { return toIntegral_helper<long>(*this, ok, base); } + ulong toULong(bool *ok=nullptr, int base=10) const + { return toIntegral_helper<ulong>(*this, ok, base); } + QT_CORE_INLINE_SINCE(6, 5) qlonglong toLongLong(bool *ok=nullptr, int base=10) const; + QT_CORE_INLINE_SINCE(6, 5) qulonglong toULongLong(bool *ok=nullptr, int base=10) const; float toFloat(bool *ok=nullptr) const; double toDouble(bool *ok=nullptr) const; - QString &setNum(short, int base=10); - QString &setNum(ushort, int base=10); - QString &setNum(int, int base=10); - QString &setNum(uint, int base=10); - QString &setNum(long, int base=10); - QString &setNum(ulong, int base=10); + inline QString &setNum(short, int base=10); + inline QString &setNum(ushort, int base=10); + inline QString &setNum(int, int base=10); + inline QString &setNum(uint, int base=10); + inline QString &setNum(long, int base=10); + inline QString &setNum(ulong, int base=10); QString &setNum(qlonglong, int base=10); QString &setNum(qulonglong, int base=10); - QString &setNum(float, char f='g', int prec=6); - QString &setNum(double, char f='g', int prec=6); + inline QString &setNum(float, char format='g', int precision=6); + QString &setNum(double, char format='g', int precision=6); static QString number(int, int base=10); static QString number(uint, int base=10); @@ -750,21 +760,65 @@ public: static QString number(ulong, int base=10); static QString number(qlonglong, int base=10); static QString number(qulonglong, int base=10); - static QString number(double, char f='g', int prec=6); - - friend Q_CORE_EXPORT bool operator==(const QString &s1, const QString &s2) noexcept; - friend Q_CORE_EXPORT bool operator<(const QString &s1, const QString &s2) noexcept; - friend inline bool operator>(const QString &s1, const QString &s2) noexcept { return s2 < s1; } - friend inline bool operator!=(const QString &s1, const QString &s2) noexcept { return !(s1 == s2); } - friend inline bool operator<=(const QString &s1, const QString &s2) noexcept { return !(s1 > s2); } - friend inline bool operator>=(const QString &s1, const QString &s2) noexcept { return !(s1 < s2); } - - bool operator==(QLatin1String s) const noexcept; - bool operator<(QLatin1String s) const noexcept; - bool operator>(QLatin1String s) const noexcept; - inline bool operator!=(QLatin1String s) const noexcept { return !operator==(s); } - inline bool operator<=(QLatin1String s) const noexcept { return !operator>(s); } - inline bool operator>=(QLatin1String s) const noexcept { return !operator<(s); } + static QString number(double, char format='g', int precision=6); + + friend bool comparesEqual(const QString &s1, const QString &s2) noexcept + { return comparesEqual(QStringView(s1), QStringView(s2)); } + friend Qt::strong_ordering compareThreeWay(const QString &s1, const QString &s2) noexcept + { return compareThreeWay(QStringView(s1), QStringView(s2)); } + Q_DECLARE_STRONGLY_ORDERED(QString) + + Q_WEAK_OVERLOAD + friend bool comparesEqual(const QString &s1, QUtf8StringView s2) noexcept + { return QtPrivate::equalStrings(s1, s2); } + Q_WEAK_OVERLOAD + friend Qt::strong_ordering compareThreeWay(const QString &s1, QUtf8StringView s2) noexcept + { + const int res = QtPrivate::compareStrings(s1, s2, Qt::CaseSensitive); + return Qt::compareThreeWay(res, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QString, QUtf8StringView, Q_WEAK_OVERLOAD) + +#ifdef __cpp_char8_t + friend bool comparesEqual(const QString &s1, const char8_t *s2) noexcept + { return comparesEqual(s1, QUtf8StringView(s2)); } + friend Qt::strong_ordering compareThreeWay(const QString &s1, const char8_t *s2) noexcept + { return compareThreeWay(s1, QUtf8StringView(s2)); } + Q_DECLARE_STRONGLY_ORDERED(QString, const char8_t *) +#endif // __cpp_char8_t + + friend bool comparesEqual(const QString &s1, QLatin1StringView s2) noexcept + { return (s1.size() == s2.size()) && QtPrivate::equalStrings(s1, s2); } + friend Qt::strong_ordering + compareThreeWay(const QString &s1, QLatin1StringView s2) noexcept + { + const int res = QtPrivate::compareStrings(s1, s2, Qt::CaseSensitive); + return Qt::compareThreeWay(res, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QString, QLatin1StringView) + + // Check isEmpty() instead of isNull() for backwards compatibility. + friend bool comparesEqual(const QString &s1, std::nullptr_t) noexcept + { return s1.isEmpty(); } + friend Qt::strong_ordering compareThreeWay(const QString &s1, std::nullptr_t) noexcept + { return s1.isEmpty() ? Qt::strong_ordering::equivalent : Qt::strong_ordering::greater; } + Q_DECLARE_STRONGLY_ORDERED(QString, std::nullptr_t) + + friend bool comparesEqual(const QString &s1, const char16_t *s2) noexcept + { return comparesEqual(s1, QStringView(s2)); } + friend Qt::strong_ordering compareThreeWay(const QString &s1, const char16_t *s2) noexcept + { return compareThreeWay(s1, QStringView(s2)); } + Q_DECLARE_STRONGLY_ORDERED(QString, const char16_t *) + + // QChar <> QString + friend bool comparesEqual(const QString &lhs, QChar rhs) noexcept + { return lhs.size() == 1 && rhs == lhs.front(); } + friend Qt::strong_ordering compareThreeWay(const QString &lhs, QChar rhs) noexcept + { + const int res = compare_helper(lhs.data(), lhs.size(), &rhs, 1); + return Qt::compareThreeWay(res, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QString, QChar) // ASCII compatibility #if defined(QT_RESTRICTED_CAST_FROM_ASCII) @@ -781,63 +835,95 @@ public: QString &operator=(char (&)[N]) = delete; #endif #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) - inline QT_ASCII_CAST_WARN QString(const char *ch) + QT_ASCII_CAST_WARN inline QString(const char *ch) : QString(fromUtf8(ch)) {} - inline QT_ASCII_CAST_WARN QString(const QByteArray &a) + QT_ASCII_CAST_WARN inline QString(const QByteArray &a) : QString(fromUtf8(a)) {} - inline QT_ASCII_CAST_WARN QString &operator=(const char *ch) - { return (*this = fromUtf8(ch)); } - inline QT_ASCII_CAST_WARN QString &operator=(const QByteArray &a) - { return (*this = fromUtf8(a)); } - + QT_ASCII_CAST_WARN inline QString &operator=(const char *ch) + { + if (!ch) { + clear(); + return *this; + } + return assign(ch); + } + QT_ASCII_CAST_WARN inline QString &operator=(const QByteArray &a) + { + if (a.isNull()) { + clear(); + return *this; + } + return assign(a); + } // these are needed, so it compiles with STL support enabled - inline QT_ASCII_CAST_WARN QString &prepend(const char *s) - { return prepend(QString::fromUtf8(s)); } - inline QT_ASCII_CAST_WARN QString &prepend(const QByteArray &s) - { return prepend(QString::fromUtf8(s)); } - inline QT_ASCII_CAST_WARN QString &append(const char *s) - { return append(QString::fromUtf8(s)); } - inline QT_ASCII_CAST_WARN QString &append(const QByteArray &s) - { return append(QString::fromUtf8(s)); } - inline QT_ASCII_CAST_WARN QString &insert(qsizetype i, const char *s) - { return insert(i, QString::fromUtf8(s)); } - inline QT_ASCII_CAST_WARN QString &insert(qsizetype i, const QByteArray &s) - { return insert(i, QString::fromUtf8(s)); } - inline QT_ASCII_CAST_WARN QString &operator+=(const char *s) - { return append(QString::fromUtf8(s)); } - inline QT_ASCII_CAST_WARN QString &operator+=(const QByteArray &s) - { return append(QString::fromUtf8(s)); } - - inline QT_ASCII_CAST_WARN bool operator==(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator<(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator<=(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator>(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator>=(const char *s) const; - - inline QT_ASCII_CAST_WARN bool operator==(const QByteArray &s) const; - inline QT_ASCII_CAST_WARN bool operator!=(const QByteArray &s) const; - inline QT_ASCII_CAST_WARN bool operator<(const QByteArray &s) const; - inline QT_ASCII_CAST_WARN bool operator>(const QByteArray &s) const; - inline QT_ASCII_CAST_WARN bool operator<=(const QByteArray &s) const; - inline QT_ASCII_CAST_WARN bool operator>=(const QByteArray &s) const; - - friend inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QString &s2); - friend inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QString &s2); - friend inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QString &s2); - friend inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QString &s2); - friend inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QString &s2); - friend inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QString &s2); - - friend inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QStringRef &s2); - friend inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QStringRef &s2); - friend inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QStringRef &s2); - friend inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QStringRef &s2); - friend inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QStringRef &s2); - friend inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QStringRef &s2); -#endif + QT_ASCII_CAST_WARN inline QString &prepend(const char *s) + { return prepend(QUtf8StringView(s)); } + QT_ASCII_CAST_WARN inline QString &prepend(const QByteArray &s) + { return prepend(QUtf8StringView(s)); } + QT_ASCII_CAST_WARN inline QString &append(const char *s) + { return append(QUtf8StringView(s)); } + QT_ASCII_CAST_WARN inline QString &append(const QByteArray &s) + { return append(QUtf8StringView(s)); } + QT_ASCII_CAST_WARN inline QString &insert(qsizetype i, const char *s) + { return insert(i, QUtf8StringView(s)); } + QT_ASCII_CAST_WARN inline QString &insert(qsizetype i, const QByteArray &s) + { return insert(i, QUtf8StringView(s)); } + QT_ASCII_CAST_WARN inline QString &operator+=(const char *s) + { return append(QUtf8StringView(s)); } + QT_ASCII_CAST_WARN inline QString &operator+=(const QByteArray &s) + { return append(QUtf8StringView(s)); } + +#if QT_CORE_REMOVED_SINCE(6, 8) + QT_ASCII_CAST_WARN inline bool operator==(const char *s) const; + QT_ASCII_CAST_WARN inline bool operator!=(const char *s) const; + QT_ASCII_CAST_WARN inline bool operator<(const char *s) const; + QT_ASCII_CAST_WARN inline bool operator<=(const char *s) const; + QT_ASCII_CAST_WARN inline bool operator>(const char *s) const; + QT_ASCII_CAST_WARN inline bool operator>=(const char *s) const; + + QT_ASCII_CAST_WARN inline bool operator==(const QByteArray &s) const; + QT_ASCII_CAST_WARN inline bool operator!=(const QByteArray &s) const; + QT_ASCII_CAST_WARN inline bool operator<(const QByteArray &s) const; + QT_ASCII_CAST_WARN inline bool operator>(const QByteArray &s) const; + QT_ASCII_CAST_WARN inline bool operator<=(const QByteArray &s) const; + QT_ASCII_CAST_WARN inline bool operator>=(const QByteArray &s) const; +#else + friend bool comparesEqual(const QString &lhs, QByteArrayView rhs) noexcept + { + return QString::compare_helper(lhs.constData(), lhs.size(), + rhs.constData(), rhs.size()) == 0; + } + friend Qt::strong_ordering + compareThreeWay(const QString &lhs, QByteArrayView rhs) noexcept + { + const int res = QString::compare_helper(lhs.constData(), lhs.size(), + rhs.constData(), rhs.size()); + return Qt::compareThreeWay(res, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QString, QByteArrayView, QT_ASCII_CAST_WARN) + + friend bool comparesEqual(const QString &lhs, const QByteArray &rhs) noexcept + { return comparesEqual(lhs, QByteArrayView(rhs)); } + friend Qt::strong_ordering + compareThreeWay(const QString &lhs, const QByteArray &rhs) noexcept + { + return compareThreeWay(lhs, QByteArrayView(rhs)); + } + Q_DECLARE_STRONGLY_ORDERED(QString, QByteArray, QT_ASCII_CAST_WARN) + + friend bool comparesEqual(const QString &lhs, const char *rhs) noexcept + { return comparesEqual(lhs, QByteArrayView(rhs)); } + friend Qt::strong_ordering + compareThreeWay(const QString &lhs, const char *rhs) noexcept + { + return compareThreeWay(lhs, QByteArrayView(rhs)); + } + Q_DECLARE_STRONGLY_ORDERED(QString, const char *, QT_ASCII_CAST_WARN) +#endif // QT_CORE_REMOVED_SINCE(6, 8) + +#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) typedef QChar *iterator; typedef const QChar *const_iterator; @@ -873,18 +959,25 @@ public: inline void push_front(QChar c) { prepend(c); } inline void push_front(const QString &s) { prepend(s); } void shrink_to_fit() { squeeze(); } + iterator erase(const_iterator first, const_iterator last); + inline iterator erase(const_iterator it) { return erase(it, it + 1); } + static constexpr qsizetype max_size() noexcept + { + // -1 to deal with the NUL terminator + return Data::max_size() - 1; + } static inline QString fromStdString(const std::string &s); inline std::string toStdString() const; static inline QString fromStdWString(const std::wstring &s); inline std::wstring toStdWString() const; -#if defined(Q_STDLIB_UNICODE_STRINGS) || defined(Q_QDOC) static inline QString fromStdU16String(const std::u16string &s); inline std::u16string toStdU16String() const; static inline QString fromStdU32String(const std::u32string &s); inline std::u32string toStdU32String() const; -#endif + + Q_IMPLICIT inline operator std::u16string_view() const noexcept; #if defined(Q_OS_DARWIN) || defined(Q_QDOC) static QString fromCFString(CFStringRef string); @@ -893,12 +986,15 @@ public: NSString *toNSString() const Q_DECL_NS_RETURNS_AUTORELEASED; #endif - inline bool isNull() const { return d->isNull(); } +#if defined(Q_OS_WASM) || defined(Q_QDOC) + static QString fromEcmaString(emscripten::val jsString); + emscripten::val toEcmaString() const; +#endif + inline bool isNull() const { return d->isNull(); } - bool isSimpleText() const; bool isRightToLeft() const; - Q_REQUIRED_RESULT bool isValidUtf16() const noexcept + [[nodiscard]] bool isValidUtf16() const noexcept { return QStringView(*this).isValidUtf16(); } QString(qsizetype size, Qt::Initialization); @@ -917,28 +1013,22 @@ private: DataPointer d; static const char16_t _empty; - friend inline bool operator==(QChar, const QString &) noexcept; - friend inline bool operator< (QChar, const QString &) noexcept; - friend inline bool operator> (QChar, const QString &) noexcept; - friend inline bool operator==(QChar, const QStringRef &) noexcept; - friend inline bool operator< (QChar, const QStringRef &) noexcept; - friend inline bool operator> (QChar, const QStringRef &) noexcept; - friend inline bool operator==(QChar, QLatin1String) noexcept; - friend inline bool operator< (QChar, QLatin1String) noexcept; - friend inline bool operator> (QChar, QLatin1String) noexcept; - - void reallocData(size_t alloc, bool grow = false); + void reallocData(qsizetype alloc, QArrayData::AllocationOption option); + void reallocGrowData(qsizetype n); + // ### remove once QAnyStringView supports UTF-32: + QString &assign_helper(const char32_t *data, qsizetype len); + // Defined in qstringconverter.h + template <typename InputIterator> + void assign_helper_char8(InputIterator first, InputIterator last); static int compare_helper(const QChar *data1, qsizetype length1, const QChar *data2, qsizetype length2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; static int compare_helper(const QChar *data1, qsizetype length1, const char *data2, qsizetype length2, - Qt::CaseSensitivity cs = Qt::CaseSensitive); - static int compare_helper(const QChar *data1, qsizetype length1, - QLatin1String s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; static int localeAwareCompare_helper(const QChar *data1, qsizetype length1, const QChar *data2, qsizetype length2); + static QString sliced_helper(QString &str, qsizetype pos, qsizetype n); static QString toLower_helper(const QString &str); static QString toLower_helper(QString &str); static QString toUpper_helper(const QString &str); @@ -949,22 +1039,30 @@ private: static QString trimmed_helper(QString &str); static QString simplified_helper(const QString &str); static QString simplified_helper(QString &str); - static DataPointer fromLatin1_helper(const char *str, qsizetype size = -1); - static QString fromUtf8_helper(const char *str, qsizetype size); - static QString fromLocal8Bit_helper(const char *, qsizetype size); static QByteArray toLatin1_helper(const QString &); static QByteArray toLatin1_helper_inplace(QString &); static QByteArray toUtf8_helper(const QString &); static QByteArray toLocal8Bit_helper(const QChar *data, qsizetype size); +#if QT_CORE_REMOVED_SINCE(6, 6) static qsizetype toUcs4_helper(const ushort *uc, qsizetype length, uint *out); +#endif + static qsizetype toUcs4_helper(const char16_t *uc, qsizetype length, char32_t *out); static qlonglong toIntegral_helper(QStringView string, bool *ok, int base); static qulonglong toIntegral_helper(QStringView string, bool *ok, uint base); - void replace_helper(size_t *indices, qsizetype nIndices, qsizetype blen, const QChar *after, qsizetype alen); - friend class QStringRef; + template <typename Predicate> + qsizetype removeIf_helper(Predicate pred) + { + const qsizetype result = d->eraseIf(pred); + if (result > 0) + d.data()[d.size] = u'\0'; + return result; + } + friend class QStringView; friend class QByteArray; - friend class QCollator; friend struct QAbstractConcatenable; + template <typename T> friend qsizetype erase(QString &s, const T &t); + template <typename Predicate> friend qsizetype erase_if(QString &s, Predicate pred); template <typename T> static T toIntegral_helper(QStringView string, bool *ok, int base) @@ -982,21 +1080,46 @@ private: return T(val); } + Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0, + [[maybe_unused]] qsizetype n = 1) const + { + Q_ASSERT(pos >= 0); + Q_ASSERT(pos <= d.size); + Q_ASSERT(n >= 0); + Q_ASSERT(n <= d.size - pos); + } + public: inline DataPointer &data_ptr() { return d; } + inline const DataPointer &data_ptr() const { return d; } }; // -// QLatin1String inline members that require QString: +// QLatin1StringView inline members that require QUtf8StringView: +// + +int QLatin1StringView::compare(QUtf8StringView other, Qt::CaseSensitivity cs) const noexcept +{ return QtPrivate::compareStrings(*this, other, cs); } + +// +// QLatin1StringView inline members that require QString: +// + +QString QLatin1StringView::toString() const { return *this; } + +// +// QStringView inline members that require QUtf8StringView: // -QString QLatin1String::toString() const { return *this; } +int QStringView::compare(QUtf8StringView other, Qt::CaseSensitivity cs) const noexcept +{ return QtPrivate::compareStrings(*this, other, cs); } // // QStringView inline members that require QString: // + QString QStringView::toString() const -{ return Q_ASSERT(size() == length()), QString(data(), length()); } +{ return QString(*this); } qint64 QStringView::toLongLong(bool *ok, int base) const { return QString::toIntegral_helper<qint64>(*this, ok, base); } @@ -1016,19 +1139,97 @@ ushort QStringView::toUShort(bool *ok, int base) const { return QString::toIntegral_helper<ushort>(*this, ok, base); } // +// QUtf8StringView inline members that require QStringView: +// + +template <bool UseChar8T> +int QBasicUtf8StringView<UseChar8T>::compare(QChar other, Qt::CaseSensitivity cs) const noexcept +{ + return QtPrivate::compareStrings(*this, QStringView(&other, 1), cs); +} + +template <bool UseChar8T> +int QBasicUtf8StringView<UseChar8T>::compare(QStringView other, Qt::CaseSensitivity cs) const noexcept +{ + return QtPrivate::compareStrings(*this, other, cs); +} + +template <bool UseChar8T> +[[nodiscard]] bool QBasicUtf8StringView<UseChar8T>::equal(QChar other) const noexcept +{ + return QtPrivate::equalStrings(*this, QStringView(&other, 1)); +} + +template <bool UseChar8T> +[[nodiscard]] bool QBasicUtf8StringView<UseChar8T>::equal(QStringView other) const noexcept +{ + return QtPrivate::equalStrings(*this, other); +} + +// +// QUtf8StringView inline members that require QString, QL1SV or QBA: +// + +template <bool UseChar8T> +QString QBasicUtf8StringView<UseChar8T>::toString() const +{ + return QString::fromUtf8(data(), size()); +} + +template<bool UseChar8T> +[[nodiscard]] int QBasicUtf8StringView<UseChar8T>::compare(QLatin1StringView other, + Qt::CaseSensitivity cs) const noexcept +{ + return QtPrivate::compareStrings(*this, other, cs); +} + +template<bool UseChar8T> +[[nodiscard]] int QBasicUtf8StringView<UseChar8T>::compare(const QByteArray &other, + Qt::CaseSensitivity cs) const noexcept +{ + return QtPrivate::compareStrings(*this, + QBasicUtf8StringView<UseChar8T>(other.data(), other.size()), + cs); +} + +template <bool UseChar8T> +[[nodiscard]] bool QBasicUtf8StringView<UseChar8T>::equal(QLatin1StringView other) const noexcept +{ + return QtPrivate::equalStrings(*this, other); +} + +template <bool UseChar8T> +[[nodiscard]] bool QBasicUtf8StringView<UseChar8T>::equal(const QByteArray &other) const noexcept +{ + return size() == other.size() + && QtPrivate::equalStrings(*this, QBasicUtf8StringView<UseChar8T>(other.data(), + other.size())); +} + +// +// QAnyStringView inline members that require QString: +// + +QAnyStringView::QAnyStringView(const QByteArray &str) noexcept + : QAnyStringView{str.isNull() ? nullptr : str.data(), str.size()} {} +QAnyStringView::QAnyStringView(const QString &str) noexcept + : QAnyStringView{str.isNull() ? nullptr : str.data(), str.size()} {} + +QString QAnyStringView::toString() const +{ return QtPrivate::convertToQString(*this); } + +// // QString inline members // -inline QString::QString(QLatin1String aLatin1) : d(fromLatin1_helper(aLatin1.latin1(), aLatin1.size())) -{ } -inline const QChar QString::at(qsizetype i) const -{ Q_ASSERT(size_t(i) < size_t(size())); return QChar(d.data()[i]); } -inline const QChar QString::operator[](qsizetype i) const -{ Q_ASSERT(size_t(i) < size_t(size())); return QChar(d.data()[i]); } -inline bool QString::isEmpty() const -{ return d.size == 0; } -inline const QChar *QString::unicode() const +QString::QString(QLatin1StringView latin1) +{ *this = QString::fromLatin1(latin1.data(), latin1.size()); } +const QChar QString::at(qsizetype i) const +{ verify(i, 1); return QChar(d.data()[i]); } +const QChar QString::operator[](qsizetype i) const +{ verify(i, 1); return QChar(d.data()[i]); } +const QChar *QString::unicode() const { return data(); } -inline const QChar *QString::data() const +const QChar *QString::data() const { #if QT5_NULL_STRINGS == 1 return reinterpret_cast<const QChar *>(d.data() ? d.data() : &_empty); @@ -1036,312 +1237,251 @@ inline const QChar *QString::data() const return reinterpret_cast<const QChar *>(d.data()); #endif } -inline QChar *QString::data() +QChar *QString::data() { detach(); Q_ASSERT(d.data()); return reinterpret_cast<QChar *>(d.data()); } -inline const QChar *QString::constData() const +const QChar *QString::constData() const { return data(); } -inline void QString::detach() -{ if (d->needsDetach()) reallocData(d.size + 1u); } -inline bool QString::isDetached() const +void QString::detach() +{ if (d->needsDetach()) reallocData(d.size, QArrayData::KeepSize); } +bool QString::isDetached() const { return !d->isShared(); } -inline void QString::clear() +void QString::clear() { if (!isNull()) *this = QString(); } -inline QString::QString(const QString &other) noexcept : d(other.d) +QString::QString(const QString &other) noexcept : d(other.d) { } -inline qsizetype QString::capacity() const -{ const auto realCapacity = d->constAllocatedCapacity(); return realCapacity ? int(realCapacity) - 1 : 0; } -inline QString &QString::setNum(short n, int base) +qsizetype QString::capacity() const { return qsizetype(d->constAllocatedCapacity()); } +QString &QString::setNum(short n, int base) { return setNum(qlonglong(n), base); } -inline QString &QString::setNum(ushort n, int base) +QString &QString::setNum(ushort n, int base) { return setNum(qulonglong(n), base); } -inline QString &QString::setNum(int n, int base) +QString &QString::setNum(int n, int base) { return setNum(qlonglong(n), base); } -inline QString &QString::setNum(uint n, int base) +QString &QString::setNum(uint n, int base) { return setNum(qulonglong(n), base); } -inline QString &QString::setNum(long n, int base) +QString &QString::setNum(long n, int base) { return setNum(qlonglong(n), base); } -inline QString &QString::setNum(ulong n, int base) +QString &QString::setNum(ulong n, int base) { return setNum(qulonglong(n), base); } -inline QString &QString::setNum(float n, char f, int prec) +QString &QString::setNum(float n, char f, int prec) { return setNum(double(n),f,prec); } -inline QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const +QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const { return arg(qlonglong(a), fieldWidth, base, fillChar); } -inline QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const +QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const { return arg(qulonglong(a), fieldWidth, base, fillChar); } -inline QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const +QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const { return arg(qlonglong(a), fieldWidth, base, fillChar); } -inline QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const +QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const { return arg(qulonglong(a), fieldWidth, base, fillChar); } -inline QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const +QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const { return arg(qlonglong(a), fieldWidth, base, fillChar); } -inline QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const +QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const { return arg(qulonglong(a), fieldWidth, base, fillChar); } -inline QString QString::section(QChar asep, qsizetype astart, qsizetype aend, SectionFlags aflags) const +QString QString::section(QChar asep, qsizetype astart, qsizetype aend, SectionFlags aflags) const { return section(QString(asep), astart, aend, aflags); } QT_WARNING_PUSH QT_WARNING_DISABLE_MSVC(4127) // "conditional expression is constant" QT_WARNING_DISABLE_INTEL(111) // "statement is unreachable" -inline qsizetype QString::toWCharArray(wchar_t *array) const +qsizetype QString::toWCharArray(wchar_t *array) const { return qToStringViewIgnoringNull(*this).toWCharArray(array); } -int QStringView::toWCharArray(wchar_t *array) const +qsizetype QStringView::toWCharArray(wchar_t *array) const { if (sizeof(wchar_t) == sizeof(QChar)) { if (auto src = data()) memcpy(array, src, sizeof(QChar) * size()); - return int(size()); // ### q6sizetype + return size(); } else { - return QString::toUcs4_helper(reinterpret_cast<const ushort *>(data()), int(size()), - reinterpret_cast<uint *>(array)); + return QString::toUcs4_helper(utf16(), size(), reinterpret_cast<char32_t *>(array)); } } QT_WARNING_POP -inline QString QString::fromWCharArray(const wchar_t *string, qsizetype size) +QString QString::fromWCharArray(const wchar_t *string, qsizetype size) { - return sizeof(wchar_t) == sizeof(QChar) ? fromUtf16(reinterpret_cast<const char16_t *>(string), size) - : fromUcs4(reinterpret_cast<const char32_t *>(string), size); + if constexpr (sizeof(wchar_t) == sizeof(QChar)) { + return QString(reinterpret_cast<const QChar *>(string), size); + } else { +#ifdef QT_BOOTSTRAPPED + Q_UNREACHABLE_RETURN(QString()); +#else + return fromUcs4(reinterpret_cast<const char32_t *>(string), size); +#endif + } } -inline constexpr QString::QString() noexcept {} -inline QString::~QString() {} +constexpr QString::QString() noexcept {} +QString::~QString() {} -inline void QString::reserve(qsizetype asize) +void QString::reserve(qsizetype asize) { - if (d->needsDetach() || asize >= capacity()) - reallocData(uint(qMax(asize, size())) + 1u); - - // we're not shared anymore, for sure - d->setFlag(Data::CapacityReserved); + if (d->needsDetach() || asize >= capacity() - d.freeSpaceAtBegin()) + reallocData(qMax(asize, size()), QArrayData::KeepSize); + if (d->constAllocatedCapacity()) + d->setFlag(Data::CapacityReserved); } -inline void QString::squeeze() +void QString::squeeze() { - if ((d->flags() & Data::CapacityReserved) == 0) + if (!d.isMutable()) return; - if (d->needsDetach() || int(d.size) < capacity()) - reallocData(uint(d.size) + 1u); - - // we're not shared anymore, for sure - d->clearFlag(Data::CapacityReserved); + if (d->needsDetach() || size() < capacity()) + reallocData(d.size, QArrayData::KeepSize); + if (d->constAllocatedCapacity()) + d->clearFlag(Data::CapacityReserved); } -inline QString &QString::setUtf16(const ushort *autf16, qsizetype asize) +QString &QString::setUtf16(const ushort *autf16, qsizetype asize) { return setUnicode(reinterpret_cast<const QChar *>(autf16), asize); } -inline QChar &QString::operator[](qsizetype i) -{ Q_ASSERT(i >= 0 && i < size()); return data()[i]; } -inline QChar &QString::front() { return operator[](0); } -inline QChar &QString::back() { return operator[](size() - 1); } -inline QString::iterator QString::begin() +QChar &QString::operator[](qsizetype i) +{ verify(i, 1); return data()[i]; } +QChar &QString::front() { return operator[](0); } +QChar &QString::back() { return operator[](size() - 1); } +QString::iterator QString::begin() { detach(); return reinterpret_cast<QChar*>(d.data()); } -inline QString::const_iterator QString::begin() const +QString::const_iterator QString::begin() const { return reinterpret_cast<const QChar*>(d.data()); } -inline QString::const_iterator QString::cbegin() const +QString::const_iterator QString::cbegin() const { return reinterpret_cast<const QChar*>(d.data()); } -inline QString::const_iterator QString::constBegin() const +QString::const_iterator QString::constBegin() const { return reinterpret_cast<const QChar*>(d.data()); } -inline QString::iterator QString::end() +QString::iterator QString::end() { detach(); return reinterpret_cast<QChar*>(d.data() + d.size); } -inline QString::const_iterator QString::end() const +QString::const_iterator QString::end() const { return reinterpret_cast<const QChar*>(d.data() + d.size); } -inline QString::const_iterator QString::cend() const +QString::const_iterator QString::cend() const { return reinterpret_cast<const QChar*>(d.data() + d.size); } -inline QString::const_iterator QString::constEnd() const +QString::const_iterator QString::constEnd() const { return reinterpret_cast<const QChar*>(d.data() + d.size); } -#if QT_STRINGVIEW_LEVEL < 2 -inline bool QString::contains(const QString &s, Qt::CaseSensitivity cs) const +bool QString::contains(const QString &s, Qt::CaseSensitivity cs) const { return indexOf(s, 0, cs) != -1; } -#endif -inline bool QString::contains(QLatin1String s, Qt::CaseSensitivity cs) const +bool QString::contains(QLatin1StringView s, Qt::CaseSensitivity cs) const { return indexOf(s, 0, cs) != -1; } -inline bool QString::contains(QChar c, Qt::CaseSensitivity cs) const +bool QString::contains(QChar c, Qt::CaseSensitivity cs) const { return indexOf(c, 0, cs) != -1; } -inline bool QString::contains(QStringView s, Qt::CaseSensitivity cs) const noexcept +bool QString::contains(QStringView s, Qt::CaseSensitivity cs) const noexcept { return indexOf(s, 0, cs) != -1; } -inline bool operator==(QLatin1String s1, QLatin1String s2) noexcept -{ return s1.size() == s2.size() && (!s1.size() || !memcmp(s1.latin1(), s2.latin1(), s1.size())); } -inline bool operator!=(QLatin1String s1, QLatin1String s2) noexcept -{ return !operator==(s1, s2); } -inline bool operator<(QLatin1String s1, QLatin1String s2) noexcept -{ - const int len = qMin(s1.size(), s2.size()); - const int r = len ? memcmp(s1.latin1(), s2.latin1(), len) : 0; - return r < 0 || (r == 0 && s1.size() < s2.size()); -} -inline bool operator>(QLatin1String s1, QLatin1String s2) noexcept -{ return operator<(s2, s1); } -inline bool operator<=(QLatin1String s1, QLatin1String s2) noexcept -{ return !operator>(s1, s2); } -inline bool operator>=(QLatin1String s1, QLatin1String s2) noexcept -{ return !operator<(s1, s2); } - -inline bool QLatin1String::operator==(const QString &s) const noexcept -{ return s == *this; } -inline bool QLatin1String::operator!=(const QString &s) const noexcept -{ return s != *this; } -inline bool QLatin1String::operator>(const QString &s) const noexcept -{ return s < *this; } -inline bool QLatin1String::operator<(const QString &s) const noexcept -{ return s > *this; } -inline bool QLatin1String::operator>=(const QString &s) const noexcept -{ return s <= *this; } -inline bool QLatin1String::operator<=(const QString &s) const noexcept -{ return s >= *this; } - #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) -inline bool QString::operator==(const char *s) const +#if QT_CORE_REMOVED_SINCE(6, 8) +bool QString::operator==(const char *s) const { return QString::compare_helper(constData(), size(), s, -1) == 0; } -inline bool QString::operator!=(const char *s) const +bool QString::operator!=(const char *s) const { return QString::compare_helper(constData(), size(), s, -1) != 0; } -inline bool QString::operator<(const char *s) const +bool QString::operator<(const char *s) const { return QString::compare_helper(constData(), size(), s, -1) < 0; } -inline bool QString::operator>(const char *s) const +bool QString::operator>(const char *s) const { return QString::compare_helper(constData(), size(), s, -1) > 0; } -inline bool QString::operator<=(const char *s) const +bool QString::operator<=(const char *s) const { return QString::compare_helper(constData(), size(), s, -1) <= 0; } -inline bool QString::operator>=(const char *s) const +bool QString::operator>=(const char *s) const { return QString::compare_helper(constData(), size(), s, -1) >= 0; } -inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QString &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) == 0; } -inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QString &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) != 0; } -inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QString &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) > 0; } -inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QString &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; } -inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QString &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; } -inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QString &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; } - -inline QT_ASCII_CAST_WARN bool operator==(const char *s1, QLatin1String s2) -{ return QString::fromUtf8(s1) == s2; } -inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, QLatin1String s2) -{ return QString::fromUtf8(s1) != s2; } -inline QT_ASCII_CAST_WARN bool operator<(const char *s1, QLatin1String s2) -{ return (QString::fromUtf8(s1) < s2); } -inline QT_ASCII_CAST_WARN bool operator>(const char *s1, QLatin1String s2) -{ return (QString::fromUtf8(s1) > s2); } -inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, QLatin1String s2) -{ return (QString::fromUtf8(s1) <= s2); } -inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, QLatin1String s2) -{ return (QString::fromUtf8(s1) >= s2); } - -inline QT_ASCII_CAST_WARN bool QLatin1String::operator==(const char *s) const -{ return QString::fromUtf8(s) == *this; } -inline QT_ASCII_CAST_WARN bool QLatin1String::operator!=(const char *s) const -{ return QString::fromUtf8(s) != *this; } -inline QT_ASCII_CAST_WARN bool QLatin1String::operator<(const char *s) const -{ return QString::fromUtf8(s) > *this; } -inline QT_ASCII_CAST_WARN bool QLatin1String::operator>(const char *s) const -{ return QString::fromUtf8(s) < *this; } -inline QT_ASCII_CAST_WARN bool QLatin1String::operator<=(const char *s) const -{ return QString::fromUtf8(s) >= *this; } -inline QT_ASCII_CAST_WARN bool QLatin1String::operator>=(const char *s) const -{ return QString::fromUtf8(s) <= *this; } - -inline QT_ASCII_CAST_WARN bool QLatin1String::operator==(const QByteArray &s) const -{ return QString::fromUtf8(s) == *this; } -inline QT_ASCII_CAST_WARN bool QLatin1String::operator!=(const QByteArray &s) const -{ return QString::fromUtf8(s) != *this; } -inline QT_ASCII_CAST_WARN bool QLatin1String::operator<(const QByteArray &s) const -{ return QString::fromUtf8(s) > *this; } -inline QT_ASCII_CAST_WARN bool QLatin1String::operator>(const QByteArray &s) const -{ return QString::fromUtf8(s) < *this; } -inline QT_ASCII_CAST_WARN bool QLatin1String::operator<=(const QByteArray &s) const -{ return QString::fromUtf8(s) >= *this; } -inline QT_ASCII_CAST_WARN bool QLatin1String::operator>=(const QByteArray &s) const -{ return QString::fromUtf8(s) <= *this; } - -inline QT_ASCII_CAST_WARN bool QString::operator==(const QByteArray &s) const -{ return QString::compare_helper(constData(), size(), s.constData(), qstrnlen(s.constData(), s.size())) == 0; } -inline QT_ASCII_CAST_WARN bool QString::operator!=(const QByteArray &s) const -{ return QString::compare_helper(constData(), size(), s.constData(), qstrnlen(s.constData(), s.size())) != 0; } -inline QT_ASCII_CAST_WARN bool QString::operator<(const QByteArray &s) const +QT_ASCII_CAST_WARN bool QString::operator==(const QByteArray &s) const +{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) == 0; } +QT_ASCII_CAST_WARN bool QString::operator!=(const QByteArray &s) const +{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) != 0; } +QT_ASCII_CAST_WARN bool QString::operator<(const QByteArray &s) const { return QString::compare_helper(constData(), size(), s.constData(), s.size()) < 0; } -inline QT_ASCII_CAST_WARN bool QString::operator>(const QByteArray &s) const +QT_ASCII_CAST_WARN bool QString::operator>(const QByteArray &s) const { return QString::compare_helper(constData(), size(), s.constData(), s.size()) > 0; } -inline QT_ASCII_CAST_WARN bool QString::operator<=(const QByteArray &s) const +QT_ASCII_CAST_WARN bool QString::operator<=(const QByteArray &s) const { return QString::compare_helper(constData(), size(), s.constData(), s.size()) <= 0; } -inline QT_ASCII_CAST_WARN bool QString::operator>=(const QByteArray &s) const +QT_ASCII_CAST_WARN bool QString::operator>=(const QByteArray &s) const { return QString::compare_helper(constData(), size(), s.constData(), s.size()) >= 0; } +bool QByteArray::operator==(const QString &s) const +{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) == 0; } +bool QByteArray::operator!=(const QString &s) const +{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) != 0; } +bool QByteArray::operator<(const QString &s) const +{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) > 0; } +bool QByteArray::operator>(const QString &s) const +{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) < 0; } +bool QByteArray::operator<=(const QString &s) const +{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) >= 0; } +bool QByteArray::operator>=(const QString &s) const +{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) <= 0; } +#endif // QT_CORE_REMOVED_SINCE(6, 8) #endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) #if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER) -inline const QString operator+(const QString &s1, const QString &s2) +inline QString operator+(const QString &s1, const QString &s2) { QString t(s1); t += s2; return t; } -inline const QString operator+(const QString &s1, QChar s2) +inline QString operator+(QString &&lhs, const QString &rhs) +{ return std::move(lhs += rhs); } +inline QString operator+(const QString &s1, QChar s2) { QString t(s1); t += s2; return t; } -inline const QString operator+(QChar s1, const QString &s2) +inline QString operator+(QString &&lhs, QChar rhs) +{ return std::move(lhs += rhs); } +inline QString operator+(QChar s1, const QString &s2) { QString t(s1); t += s2; return t; } # if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) -inline QT_ASCII_CAST_WARN const QString operator+(const QString &s1, const char *s2) -{ QString t(s1); t += QString::fromUtf8(s2); return t; } -inline QT_ASCII_CAST_WARN const QString operator+(const char *s1, const QString &s2) +QT_ASCII_CAST_WARN inline QString operator+(const QString &s1, const char *s2) +{ QString t(s1); t += QUtf8StringView(s2); return t; } +QT_ASCII_CAST_WARN inline QString operator+(QString &&lhs, const char *rhs) +{ QT_IGNORE_DEPRECATIONS(return std::move(lhs += rhs);) } +QT_ASCII_CAST_WARN inline QString operator+(const char *s1, const QString &s2) { QString t = QString::fromUtf8(s1); t += s2; return t; } -inline QT_ASCII_CAST_WARN const QString operator+(const QByteArray &ba, const QString &s) +QT_ASCII_CAST_WARN inline QString operator+(const QByteArray &ba, const QString &s) { QString t = QString::fromUtf8(ba); t += s; return t; } -inline QT_ASCII_CAST_WARN const QString operator+(const QString &s, const QByteArray &ba) -{ QString t(s); t += QString::fromUtf8(ba); return t; } +QT_ASCII_CAST_WARN inline QString operator+(const QString &s, const QByteArray &ba) +{ QString t(s); t += QUtf8StringView(ba); return t; } +QT_ASCII_CAST_WARN inline QString operator+(QString &&lhs, const QByteArray &rhs) +{ QT_IGNORE_DEPRECATIONS(return std::move(lhs += rhs);) } # endif // QT_NO_CAST_FROM_ASCII #endif // QT_USE_QSTRINGBUILDER -inline std::string QString::toStdString() const +std::string QString::toStdString() const { return toUtf8().toStdString(); } -inline QString QString::fromStdString(const std::string &s) -{ return fromUtf8(s.data(), int(s.size())); } +QString QString::fromStdString(const std::string &s) +{ return fromUtf8(s.data(), qsizetype(s.size())); } -inline std::wstring QString::toStdWString() const +std::wstring QString::toStdWString() const { std::wstring str; - str.resize(length()); -#if __cplusplus >= 201703L + str.resize(size()); str.resize(toWCharArray(str.data())); -#else - if (length()) - str.resize(toWCharArray(&str.front())); -#endif return str; } -inline QString QString::fromStdWString(const std::wstring &s) -{ return fromWCharArray(s.data(), int(s.size())); } +QString QString::fromStdWString(const std::wstring &s) +{ return fromWCharArray(s.data(), qsizetype(s.size())); } -#if defined(Q_STDLIB_UNICODE_STRINGS) -inline QString QString::fromStdU16String(const std::u16string &s) -{ return fromUtf16(s.data(), int(s.size())); } +QString QString::fromStdU16String(const std::u16string &s) +{ return fromUtf16(s.data(), qsizetype(s.size())); } -inline std::u16string QString::toStdU16String() const -{ return std::u16string(reinterpret_cast<const char16_t*>(utf16()), length()); } +std::u16string QString::toStdU16String() const +{ return std::u16string(reinterpret_cast<const char16_t*>(data()), size()); } -inline QString QString::fromStdU32String(const std::u32string &s) -{ return fromUcs4(s.data(), int(s.size())); } +QString QString::fromStdU32String(const std::u32string &s) +{ return fromUcs4(s.data(), qsizetype(s.size())); } -inline std::u32string QString::toStdU32String() const +std::u32string QString::toStdU32String() const { - std::u32string u32str(length(), char32_t(0)); - int len = toUcs4_helper(reinterpret_cast<const ushort *>(constData()), length(), - reinterpret_cast<uint*>(&u32str[0])); + std::u32string u32str(size(), char32_t(0)); + const qsizetype len = toUcs4_helper(reinterpret_cast<const char16_t *>(data()), + size(), u32str.data()); u32str.resize(len); return u32str; } -#endif -#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE)) +QString::operator std::u16string_view() const noexcept +{ + return std::u16string_view(d.data(), size_t(d.size)); +} + +#if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED) Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QString &); Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QString &); #endif @@ -1349,439 +1489,60 @@ Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QString &); Q_DECLARE_SHARED(QString) Q_DECLARE_OPERATORS_FOR_FLAGS(QString::SectionFlags) +int QString::compare(QStringView s, Qt::CaseSensitivity cs) const noexcept +{ return -s.compare(*this, cs); } -class Q_CORE_EXPORT QStringRef { - const QString *m_string; - int m_position; - int m_size; -public: - typedef QString::size_type size_type; - typedef QString::value_type value_type; - typedef const QChar *const_iterator; - typedef std::reverse_iterator<const_iterator> const_reverse_iterator; - typedef QString::const_pointer const_pointer; - typedef QString::const_reference const_reference; - - // ### Qt 6: make this constructor constexpr, after the destructor is made trivial - inline QStringRef() : m_string(nullptr), m_position(0), m_size(0) {} - inline QStringRef(const QString *string, int position, int size); - inline QStringRef(const QString *string); - - inline const QString *string() const { return m_string; } - inline int position() const { return m_position; } - inline int size() const { return m_size; } - inline int count() const { return m_size; } - inline int length() const { return m_size; } - -#if QT_STRINGVIEW_LEVEL < 2 - int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - int indexOf(const QStringRef &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#endif - Q_REQUIRED_RESULT int indexOf(QStringView s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return int(QtPrivate::findString(*this, from, s, cs)); } // ### Qt6: qsizetype - int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - int indexOf(QLatin1String str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#if QT_STRINGVIEW_LEVEL < 2 - int lastIndexOf(const QStringRef &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - int lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#endif - int lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - int lastIndexOf(QLatin1String str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - Q_REQUIRED_RESULT int lastIndexOf(QStringView s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return int(QtPrivate::lastIndexOf(*this, from, s, cs)); } // ### Qt6: qsizetype - -#if QT_STRINGVIEW_LEVEL < 2 - inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - inline bool contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#endif - inline bool contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - inline bool contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - inline bool contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; - - int count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - - Q_REQUIRED_RESULT - QList<QStringRef> split(const QString &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, - Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - Q_REQUIRED_RESULT - QList<QStringRef> split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, - Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - - Q_REQUIRED_RESULT QStringRef left(int n) const; - Q_REQUIRED_RESULT QStringRef right(int n) const; - Q_REQUIRED_RESULT QStringRef mid(int pos, int n = -1) const; - Q_REQUIRED_RESULT QStringRef chopped(int n) const - { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return left(size() - n); } - - void truncate(int pos) noexcept { m_size = qBound(0, pos, m_size); } - void chop(int n) noexcept - { - if (n >= m_size) - m_size = 0; - else if (n > 0) - m_size -= n; - } +int QString::localeAwareCompare(QStringView s) const +{ return localeAwareCompare_helper(constData(), size(), s.constData(), s.size()); } +int QString::localeAwareCompare(QStringView s1, QStringView s2) +{ return localeAwareCompare_helper(s1.constData(), s1.size(), s2.constData(), s2.size()); } +int QStringView::localeAwareCompare(QStringView other) const +{ return QString::localeAwareCompare(*this, other); } - bool isRightToLeft() const; +#if QT_CORE_INLINE_IMPL_SINCE(6, 5) +qint64 QString::toLongLong(bool *ok, int base) const +{ + return toIntegral_helper<qlonglong>(*this, ok, base); +} - Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return QtPrivate::startsWith(*this, s, cs); } - bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#if QT_STRINGVIEW_LEVEL < 2 - bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - bool startsWith(const QStringRef &c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +quint64 QString::toULongLong(bool *ok, int base) const +{ + return toIntegral_helper<qulonglong>(*this, ok, base); +} #endif - - Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return QtPrivate::endsWith(*this, s, cs); } - bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -#if QT_STRINGVIEW_LEVEL < 2 - bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - bool endsWith(const QStringRef &c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#if QT_CORE_INLINE_IMPL_SINCE(6, 7) +qsizetype QString::indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const +{ + return qToStringViewIgnoringNull(*this).indexOf(ch, from, cs); +} +qsizetype QString::lastIndexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const +{ + return qToStringViewIgnoringNull(*this).lastIndexOf(ch, from, cs); +} #endif - inline QStringRef &operator=(const QString *string); - - inline const QChar *unicode() const - { - if (!m_string) - return reinterpret_cast<const QChar *>(&QString::_empty); - return m_string->unicode() + m_position; - } - inline const QChar *data() const { return unicode(); } - inline const QChar *constData() const { return unicode(); } - - inline const_iterator begin() const { return unicode(); } - inline const_iterator cbegin() const { return unicode(); } - inline const_iterator constBegin() const { return unicode(); } - inline const_iterator end() const { return unicode() + size(); } - inline const_iterator cend() const { return unicode() + size(); } - inline const_iterator constEnd() const { return unicode() + size(); } - inline const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } - inline const_reverse_iterator crbegin() const { return rbegin(); } - inline const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } - inline const_reverse_iterator crend() const { return rend(); } - - Q_REQUIRED_RESULT QByteArray toLatin1() const; - Q_REQUIRED_RESULT QByteArray toUtf8() const; - Q_REQUIRED_RESULT QByteArray toLocal8Bit() const; - Q_REQUIRED_RESULT QList<uint> toUcs4() const; - - inline void clear() { m_string = nullptr; m_position = m_size = 0; } - QString toString() const; - inline bool isEmpty() const { return m_size == 0; } - inline bool isNull() const { return m_string == nullptr || m_string->isNull(); } - - QStringRef appendTo(QString *string) const; - - inline const QChar at(int i) const - { Q_ASSERT(uint(i) < uint(size())); return m_string->at(i + m_position); } - QChar operator[](int i) const { return at(i); } - Q_REQUIRED_RESULT QChar front() const { return at(0); } - Q_REQUIRED_RESULT QChar back() const { return at(size() - 1); } +namespace QtPrivate { +// used by qPrintable() and qUtf8Printable() macros +inline const QString &asString(const QString &s) { return s; } +inline QString &&asString(QString &&s) { return std::move(s); } +} -#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) - // ASCII compatibility - inline QT_ASCII_CAST_WARN bool operator==(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator<(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator<=(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator>(const char *s) const; - inline QT_ASCII_CAST_WARN bool operator>=(const char *s) const; +#ifndef qPrintable +# define qPrintable(string) QtPrivate::asString(string).toLocal8Bit().constData() #endif - int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; - int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; - int compare(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs); } - int compare(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; -#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) - int compare(const QByteArray &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const - { return QString::compare_helper(unicode(), size(), s.data(), qstrnlen(s.data(), s.size()), cs); } +#ifndef qUtf8Printable +# define qUtf8Printable(string) QtPrivate::asString(string).toUtf8().constData() #endif - static int compare(const QStringRef &s1, const QString &s2, - Qt::CaseSensitivity = Qt::CaseSensitive) noexcept; - static int compare(const QStringRef &s1, const QStringRef &s2, - Qt::CaseSensitivity = Qt::CaseSensitive) noexcept; - static int compare(const QStringRef &s1, QLatin1String s2, - Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; - - int localeAwareCompare(const QString &s) const; - int localeAwareCompare(const QStringRef &s) const; - static int localeAwareCompare(const QStringRef &s1, const QString &s2); - static int localeAwareCompare(const QStringRef &s1, const QStringRef &s2); - - Q_REQUIRED_RESULT QStringRef trimmed() const; - short toShort(bool *ok = nullptr, int base = 10) const; - ushort toUShort(bool *ok = nullptr, int base = 10) const; - int toInt(bool *ok = nullptr, int base = 10) const; - uint toUInt(bool *ok = nullptr, int base = 10) const; - long toLong(bool *ok = nullptr, int base = 10) const; - ulong toULong(bool *ok = nullptr, int base = 10) const; - qlonglong toLongLong(bool *ok = nullptr, int base = 10) const; - qulonglong toULongLong(bool *ok = nullptr, int base = 10) const; - float toFloat(bool *ok = nullptr) const; - double toDouble(bool *ok = nullptr) const; -}; -Q_DECLARE_TYPEINFO(QStringRef, Q_PRIMITIVE_TYPE); - -inline QStringRef &QStringRef::operator=(const QString *aString) -{ m_string = aString; m_position = 0; m_size = aString?aString->size():0; return *this; } - -inline QStringRef::QStringRef(const QString *aString, int aPosition, int aSize) - :m_string(aString), m_position(aPosition), m_size(aSize){} - -inline QStringRef::QStringRef(const QString *aString) - :m_string(aString), m_position(0), m_size(aString?aString->size() : 0){} - -// QStringRef <> QStringRef -Q_CORE_EXPORT bool operator==(const QStringRef &s1, const QStringRef &s2) noexcept; -inline bool operator!=(const QStringRef &s1, const QStringRef &s2) noexcept -{ return !(s1 == s2); } -Q_CORE_EXPORT bool operator<(const QStringRef &s1, const QStringRef &s2) noexcept; -inline bool operator>(const QStringRef &s1, const QStringRef &s2) noexcept -{ return s2 < s1; } -inline bool operator<=(const QStringRef &s1, const QStringRef &s2) noexcept -{ return !(s1 > s2); } -inline bool operator>=(const QStringRef &s1, const QStringRef &s2) noexcept -{ return !(s1 < s2); } - -// QString <> QStringRef -Q_CORE_EXPORT bool operator==(const QString &lhs, const QStringRef &rhs) noexcept; -inline bool operator!=(const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(rhs) != 0; } -inline bool operator< (const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(rhs) < 0; } -inline bool operator> (const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(rhs) > 0; } -inline bool operator<=(const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(rhs) <= 0; } -inline bool operator>=(const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(rhs) >= 0; } - -inline bool operator==(const QStringRef &lhs, const QString &rhs) noexcept { return rhs == lhs; } -inline bool operator!=(const QStringRef &lhs, const QString &rhs) noexcept { return rhs != lhs; } -inline bool operator< (const QStringRef &lhs, const QString &rhs) noexcept { return rhs > lhs; } -inline bool operator> (const QStringRef &lhs, const QString &rhs) noexcept { return rhs < lhs; } -inline bool operator<=(const QStringRef &lhs, const QString &rhs) noexcept { return rhs >= lhs; } -inline bool operator>=(const QStringRef &lhs, const QString &rhs) noexcept { return rhs <= lhs; } - -inline int QString::compare(QStringView s, Qt::CaseSensitivity cs) const noexcept -{ return -s.compare(*this, cs); } -inline int QStringRef::compare(const QString &s, Qt::CaseSensitivity cs) const noexcept -{ return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); } -inline int QStringRef::compare(const QStringRef &s, Qt::CaseSensitivity cs) const noexcept -{ return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); } -inline int QStringRef::compare(QLatin1String s, Qt::CaseSensitivity cs) const noexcept -{ return QString::compare_helper(constData(), length(), s, cs); } -inline int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs) noexcept -{ return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); } -inline int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs) noexcept -{ return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); } -inline int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs) noexcept -{ return QString::compare_helper(s1.constData(), s1.length(), s2, cs); } - -// QLatin1String <> QStringRef -Q_CORE_EXPORT bool operator==(QLatin1String lhs, const QStringRef &rhs) noexcept; -inline bool operator!=(QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(lhs) != 0; } -inline bool operator< (QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(lhs) > 0; } -inline bool operator> (QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(lhs) < 0; } -inline bool operator<=(QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(lhs) >= 0; } -inline bool operator>=(QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(lhs) <= 0; } - -inline bool operator==(const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs == lhs; } -inline bool operator!=(const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs != lhs; } -inline bool operator< (const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs > lhs; } -inline bool operator> (const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs < lhs; } -inline bool operator<=(const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs >= lhs; } -inline bool operator>=(const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs <= lhs; } - -// QChar <> QString -inline bool operator==(QChar lhs, const QString &rhs) noexcept -{ return rhs.size() == 1 && lhs == rhs.front(); } -inline bool operator< (QChar lhs, const QString &rhs) noexcept -{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) < 0; } -inline bool operator> (QChar lhs, const QString &rhs) noexcept -{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) > 0; } - -inline bool operator!=(QChar lhs, const QString &rhs) noexcept { return !(lhs == rhs); } -inline bool operator<=(QChar lhs, const QString &rhs) noexcept { return !(lhs > rhs); } -inline bool operator>=(QChar lhs, const QString &rhs) noexcept { return !(lhs < rhs); } - -inline bool operator==(const QString &lhs, QChar rhs) noexcept { return rhs == lhs; } -inline bool operator!=(const QString &lhs, QChar rhs) noexcept { return !(rhs == lhs); } -inline bool operator< (const QString &lhs, QChar rhs) noexcept { return rhs > lhs; } -inline bool operator> (const QString &lhs, QChar rhs) noexcept { return rhs < lhs; } -inline bool operator<=(const QString &lhs, QChar rhs) noexcept { return !(rhs < lhs); } -inline bool operator>=(const QString &lhs, QChar rhs) noexcept { return !(rhs > lhs); } - -// QChar <> QStringRef -inline bool operator==(QChar lhs, const QStringRef &rhs) noexcept -{ return rhs.size() == 1 && lhs == rhs.front(); } -inline bool operator< (QChar lhs, const QStringRef &rhs) noexcept -{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) < 0; } -inline bool operator> (QChar lhs, const QStringRef &rhs) noexcept -{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) > 0; } - -inline bool operator!=(QChar lhs, const QStringRef &rhs) noexcept { return !(lhs == rhs); } -inline bool operator<=(QChar lhs, const QStringRef &rhs) noexcept { return !(lhs > rhs); } -inline bool operator>=(QChar lhs, const QStringRef &rhs) noexcept { return !(lhs < rhs); } - -inline bool operator==(const QStringRef &lhs, QChar rhs) noexcept { return rhs == lhs; } -inline bool operator!=(const QStringRef &lhs, QChar rhs) noexcept { return !(rhs == lhs); } -inline bool operator< (const QStringRef &lhs, QChar rhs) noexcept { return rhs > lhs; } -inline bool operator> (const QStringRef &lhs, QChar rhs) noexcept { return rhs < lhs; } -inline bool operator<=(const QStringRef &lhs, QChar rhs) noexcept { return !(rhs < lhs); } -inline bool operator>=(const QStringRef &lhs, QChar rhs) noexcept { return !(rhs > lhs); } - -// QChar <> QLatin1String -inline bool operator==(QChar lhs, QLatin1String rhs) noexcept -{ return rhs.size() == 1 && lhs == rhs.front(); } -inline bool operator< (QChar lhs, QLatin1String rhs) noexcept -{ return QString::compare_helper(&lhs, 1, rhs) < 0; } -inline bool operator> (QChar lhs, QLatin1String rhs) noexcept -{ return QString::compare_helper(&lhs, 1, rhs) > 0; } - -inline bool operator!=(QChar lhs, QLatin1String rhs) noexcept { return !(lhs == rhs); } -inline bool operator<=(QChar lhs, QLatin1String rhs) noexcept { return !(lhs > rhs); } -inline bool operator>=(QChar lhs, QLatin1String rhs) noexcept { return !(lhs < rhs); } - -inline bool operator==(QLatin1String lhs, QChar rhs) noexcept { return rhs == lhs; } -inline bool operator!=(QLatin1String lhs, QChar rhs) noexcept { return !(rhs == lhs); } -inline bool operator< (QLatin1String lhs, QChar rhs) noexcept { return rhs > lhs; } -inline bool operator> (QLatin1String lhs, QChar rhs) noexcept { return rhs < lhs; } -inline bool operator<=(QLatin1String lhs, QChar rhs) noexcept { return !(rhs < lhs); } -inline bool operator>=(QLatin1String lhs, QChar rhs) noexcept { return !(rhs > lhs); } - -// QStringView <> QStringView -inline bool operator==(QStringView lhs, QStringView rhs) noexcept { return lhs.size() == rhs.size() && QtPrivate::compareStrings(lhs, rhs) == 0; } -inline bool operator!=(QStringView lhs, QStringView rhs) noexcept { return !(lhs == rhs); } -inline bool operator< (QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; } -inline bool operator<=(QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; } -inline bool operator> (QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; } -inline bool operator>=(QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; } - -// QStringView <> QChar -inline bool operator==(QStringView lhs, QChar rhs) noexcept { return lhs == QStringView(&rhs, 1); } -inline bool operator!=(QStringView lhs, QChar rhs) noexcept { return lhs != QStringView(&rhs, 1); } -inline bool operator< (QStringView lhs, QChar rhs) noexcept { return lhs < QStringView(&rhs, 1); } -inline bool operator<=(QStringView lhs, QChar rhs) noexcept { return lhs <= QStringView(&rhs, 1); } -inline bool operator> (QStringView lhs, QChar rhs) noexcept { return lhs > QStringView(&rhs, 1); } -inline bool operator>=(QStringView lhs, QChar rhs) noexcept { return lhs >= QStringView(&rhs, 1); } - -inline bool operator==(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) == rhs; } -inline bool operator!=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) != rhs; } -inline bool operator< (QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) < rhs; } -inline bool operator<=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) <= rhs; } -inline bool operator> (QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) > rhs; } -inline bool operator>=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) >= rhs; } - -// QStringView <> QLatin1String -inline bool operator==(QStringView lhs, QLatin1String rhs) noexcept { return lhs.size() == rhs.size() && QtPrivate::compareStrings(lhs, rhs) == 0; } -inline bool operator!=(QStringView lhs, QLatin1String rhs) noexcept { return !(lhs == rhs); } -inline bool operator< (QStringView lhs, QLatin1String rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; } -inline bool operator<=(QStringView lhs, QLatin1String rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; } -inline bool operator> (QStringView lhs, QLatin1String rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; } -inline bool operator>=(QStringView lhs, QLatin1String rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; } - -inline bool operator==(QLatin1String lhs, QStringView rhs) noexcept { return lhs.size() == rhs.size() && QtPrivate::compareStrings(lhs, rhs) == 0; } -inline bool operator!=(QLatin1String lhs, QStringView rhs) noexcept { return !(lhs == rhs); } -inline bool operator< (QLatin1String lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; } -inline bool operator<=(QLatin1String lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; } -inline bool operator> (QLatin1String lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; } -inline bool operator>=(QLatin1String lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; } -#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) -// QStringRef <> QByteArray -inline QT_ASCII_CAST_WARN bool operator==(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) == 0; } -inline QT_ASCII_CAST_WARN bool operator!=(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) != 0; } -inline QT_ASCII_CAST_WARN bool operator< (const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) < 0; } -inline QT_ASCII_CAST_WARN bool operator> (const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) > 0; } -inline QT_ASCII_CAST_WARN bool operator<=(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) <= 0; } -inline QT_ASCII_CAST_WARN bool operator>=(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) >= 0; } - -inline QT_ASCII_CAST_WARN bool operator==(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) == 0; } -inline QT_ASCII_CAST_WARN bool operator!=(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) != 0; } -inline QT_ASCII_CAST_WARN bool operator< (const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) > 0; } -inline QT_ASCII_CAST_WARN bool operator> (const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) < 0; } -inline QT_ASCII_CAST_WARN bool operator<=(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) >= 0; } -inline QT_ASCII_CAST_WARN bool operator>=(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) <= 0; } - -// QStringRef <> const char * -inline QT_ASCII_CAST_WARN bool QStringRef::operator==(const char *s) const -{ return QString::compare_helper(constData(), size(), s, -1) == 0; } -inline QT_ASCII_CAST_WARN bool QStringRef::operator!=(const char *s) const -{ return QString::compare_helper(constData(), size(), s, -1) != 0; } -inline QT_ASCII_CAST_WARN bool QStringRef::operator<(const char *s) const -{ return QString::compare_helper(constData(), size(), s, -1) < 0; } -inline QT_ASCII_CAST_WARN bool QStringRef::operator<=(const char *s) const -{ return QString::compare_helper(constData(), size(), s, -1) <= 0; } -inline QT_ASCII_CAST_WARN bool QStringRef::operator>(const char *s) const -{ return QString::compare_helper(constData(), size(), s, -1) > 0; } -inline QT_ASCII_CAST_WARN bool QStringRef::operator>=(const char *s) const -{ return QString::compare_helper(constData(), size(), s, -1) >= 0; } - -inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QStringRef &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) == 0; } -inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QStringRef &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) != 0; } -inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QStringRef &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) > 0; } -inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QStringRef &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; } -inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QStringRef &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; } -inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QStringRef &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; } -#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) - -inline int QString::localeAwareCompare(QStringView s) const -{ return localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); } -inline int QString::localeAwareCompare(QStringView s1, QStringView s2) -{ return localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); } -inline int QStringRef::localeAwareCompare(const QString &s) const -{ return QString::localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); } -inline int QStringRef::localeAwareCompare(const QStringRef &s) const -{ return QString::localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); } -inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QString &s2) -{ return QString::localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); } -inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef &s2) -{ return QString::localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); } - -#if QT_STRINGVIEW_LEVEL < 2 -inline bool QStringRef::contains(const QString &s, Qt::CaseSensitivity cs) const -{ return indexOf(s, 0, cs) != -1; } -inline bool QStringRef::contains(const QStringRef &s, Qt::CaseSensitivity cs) const -{ return indexOf(s, 0, cs) != -1; } +/* + Wrap QString::utf16() with enough casts to allow passing it + to QString::asprintf("%ls") without warnings. +*/ +#ifndef qUtf16Printable +# define qUtf16Printable(string) \ + static_cast<const wchar_t*>(static_cast<const void*>(QtPrivate::asString(string).utf16())) #endif -inline bool QStringRef::contains(QLatin1String s, Qt::CaseSensitivity cs) const -{ return indexOf(s, 0, cs) != -1; } -inline bool QStringRef::contains(QChar c, Qt::CaseSensitivity cs) const -{ return indexOf(c, 0, cs) != -1; } -inline bool QStringRef::contains(QStringView s, Qt::CaseSensitivity cs) const noexcept -{ return indexOf(s, 0, cs) != -1; } - -#if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER) -inline QString operator+(const QString &s1, const QStringRef &s2) -{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; } -inline QString operator+(const QStringRef &s1, const QString &s2) -{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; } -inline QString operator+(const QStringRef &s1, QLatin1String s2) -{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; } -inline QString operator+(QLatin1String s1, const QStringRef &s2) -{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; } -inline QString operator+(const QStringRef &s1, const QStringRef &s2) -{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; } -inline QString operator+(const QStringRef &s1, QChar s2) -{ QString t; t.reserve(s1.size() + 1); t += s1; t += s2; return t; } -inline QString operator+(QChar s1, const QStringRef &s2) -{ QString t; t.reserve(1 + s2.size()); t += s1; t += s2; return t; } -#endif // !(QT_USE_FAST_OPERATOR_PLUS || QT_USE_QSTRINGBUILDER) - -namespace QtPrivate { -// used by qPrintable() and qUtf8Printable() macros -inline const QString &asString(const QString &s) { return s; } -inline QString &&asString(QString &&s) { return std::move(s); } -} // // QStringView::arg() implementation @@ -1796,29 +1557,29 @@ struct ArgBase { struct QStringViewArg : ArgBase { QStringView string; QStringViewArg() = default; - Q_DECL_CONSTEXPR explicit QStringViewArg(QStringView v) noexcept : ArgBase{U16}, string{v} {} + constexpr explicit QStringViewArg(QStringView v) noexcept : ArgBase{U16}, string{v} {} }; struct QLatin1StringArg : ArgBase { - QLatin1String string; + QLatin1StringView string; QLatin1StringArg() = default; - Q_DECL_CONSTEXPR explicit QLatin1StringArg(QLatin1String v) noexcept : ArgBase{L1}, string{v} {} + constexpr explicit QLatin1StringArg(QLatin1StringView v) noexcept : ArgBase{L1}, string{v} {} }; -Q_REQUIRED_RESULT Q_CORE_EXPORT QString argToQString(QStringView pattern, size_t n, const ArgBase **args); -Q_REQUIRED_RESULT Q_CORE_EXPORT QString argToQString(QLatin1String pattern, size_t n, const ArgBase **args); +[[nodiscard]] Q_CORE_EXPORT QString argToQString(QStringView pattern, size_t n, const ArgBase **args); +[[nodiscard]] Q_CORE_EXPORT QString argToQString(QLatin1StringView pattern, size_t n, const ArgBase **args); template <typename StringView, typename...Args> -Q_REQUIRED_RESULT Q_ALWAYS_INLINE QString argToQStringDispatch(StringView pattern, const Args &...args) +[[nodiscard]] Q_ALWAYS_INLINE QString argToQStringDispatch(StringView pattern, const Args &...args) { const ArgBase *argBases[] = {&args..., /* avoid zero-sized array */ nullptr}; return QtPrivate::argToQString(pattern, sizeof...(Args), argBases); } - inline QStringViewArg qStringLikeToArg(const QString &s) noexcept { return QStringViewArg{qToStringViewIgnoringNull(s)}; } -Q_DECL_CONSTEXPR inline QStringViewArg qStringLikeToArg(QStringView s) noexcept { return QStringViewArg{s}; } - inline QStringViewArg qStringLikeToArg(const QChar &c) noexcept { return QStringViewArg{QStringView{&c, 1}}; } -Q_DECL_CONSTEXPR inline QLatin1StringArg qStringLikeToArg(QLatin1String s) noexcept { return QLatin1StringArg{s}; } + inline QStringViewArg qStringLikeToArg(const QString &s) noexcept { return QStringViewArg{qToStringViewIgnoringNull(s)}; } +constexpr inline QStringViewArg qStringLikeToArg(QStringView s) noexcept { return QStringViewArg{s}; } + inline QStringViewArg qStringLikeToArg(const QChar &c) noexcept { return QStringViewArg{QStringView{&c, 1}}; } +constexpr inline QLatin1StringArg qStringLikeToArg(QLatin1StringView s) noexcept { return QLatin1StringArg{s}; } } // namespace QtPrivate @@ -1831,15 +1592,54 @@ QString QStringView::arg(Args &&...args) const template <typename...Args> Q_ALWAYS_INLINE -QString QLatin1String::arg(Args &&...args) const +QString QLatin1StringView::arg(Args &&...args) const { return QtPrivate::argToQStringDispatch(*this, QtPrivate::qStringLikeToArg(args)...); } +template <typename T> +qsizetype erase(QString &s, const T &t) +{ + return s.removeIf_helper([&t](const auto &e) { return t == e; }); +} + +template <typename Predicate> +qsizetype erase_if(QString &s, Predicate pred) +{ + return s.removeIf_helper(pred); +} + +namespace Qt { +inline namespace Literals { +inline namespace StringLiterals { +inline QString operator""_s(const char16_t *str, size_t size) noexcept +{ + return QString(QStringPrivate(nullptr, const_cast<char16_t *>(str), qsizetype(size))); +} + +} // StringLiterals +} // Literals +} // Qt + +inline namespace QtLiterals { +#if QT_DEPRECATED_SINCE(6, 8) + +QT_DEPRECATED_VERSION_X_6_8("Use _s from Qt::StringLiterals namespace instead.") +inline QString operator""_qs(const char16_t *str, size_t size) noexcept +{ + return Qt::StringLiterals::operator""_s(str, size); +} + +#endif // QT_DEPRECATED_SINCE(6, 8) +} // QtLiterals + QT_END_NAMESPACE -#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER) #include <QtCore/qstringbuilder.h> +#include <QtCore/qstringconverter.h> + +#ifdef Q_L1S_VIEW_IS_PRIMARY +# undef Q_L1S_VIEW_IS_PRIMARY #endif #endif // QSTRING_H |