diff options
Diffstat (limited to 'src/corelib/text/qstringtokenizer.h')
-rw-r--r-- | src/corelib/text/qstringtokenizer.h | 93 |
1 files changed, 38 insertions, 55 deletions
diff --git a/src/corelib/text/qstringtokenizer.h b/src/corelib/text/qstringtokenizer.h index e0aefe2522..7a627b4508 100644 --- a/src/corelib/text/qstringtokenizer.h +++ b/src/corelib/text/qstringtokenizer.h @@ -1,54 +1,17 @@ -/**************************************************************************** -** -** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> -** Contact: http://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 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 QSTRINGTOKENIZER_H #define QSTRINGTOKENIZER_H #include <QtCore/qnamespace.h> #include <QtCore/qcontainerfwd.h> +#include <iterator> QT_BEGIN_NAMESPACE template <typename, typename> class QStringBuilder; -#if defined(Q_QDOC) || 1 || (defined(__cpp_range_based_for) && __cpp_range_based_for >= 201603) -# define Q_STRINGTOKENIZER_USE_SENTINEL -#endif +#define Q_STRINGTOKENIZER_USE_SENTINEL class QStringTokenizerBaseBase { @@ -109,8 +72,8 @@ public: iterator() noexcept = default; // violates std::forward_iterator (returns a reference into the iterator) - Q_REQUIRED_RESULT constexpr const Haystack* operator->() const { return Q_ASSERT(current.ok), ¤t.value; } - Q_REQUIRED_RESULT constexpr const Haystack& operator*() const { return *operator->(); } + [[nodiscard]] constexpr const Haystack* operator->() const { return Q_ASSERT(current.ok), ¤t.value; } + [[nodiscard]] constexpr const Haystack& operator*() const { return *operator->(); } iterator& operator++() { advance(); return *this; } iterator operator++(int) { auto tmp = *this; advance(); return tmp; } @@ -145,12 +108,12 @@ public: using reference = typename iterator::reference; using const_reference = reference; - Q_REQUIRED_RESULT iterator begin() const noexcept { return iterator{*this}; } - Q_REQUIRED_RESULT iterator cbegin() const noexcept { return begin(); } + [[nodiscard]] iterator begin() const noexcept { return iterator{*this}; } + [[nodiscard]] iterator cbegin() const noexcept { return begin(); } template <bool = std::is_same<iterator, sentinel>::value> // ODR protection - Q_REQUIRED_RESULT constexpr sentinel end() const noexcept { return {}; } + [[nodiscard]] constexpr sentinel end() const noexcept { return {}; } template <bool = std::is_same<iterator, sentinel>::value> // ODR protection - Q_REQUIRED_RESULT constexpr sentinel cend() const noexcept { return {}; } + [[nodiscard]] constexpr sentinel cend() const noexcept { return {}; } private: Haystack m_haystack; @@ -170,7 +133,7 @@ namespace Tok { template <typename String> struct ViewForImpl {}; template <> struct ViewForImpl<QStringView> { using type = QStringView; }; - template <> struct ViewForImpl<QLatin1String> { using type = QLatin1String; }; + template <> struct ViewForImpl<QLatin1StringView> { using type = QLatin1StringView; }; template <> struct ViewForImpl<QChar> { using type = QChar; }; template <> struct ViewForImpl<QString> : ViewForImpl<QStringView> {}; template <> struct ViewForImpl<QLatin1Char> : ViewForImpl<QChar> {}; @@ -187,7 +150,7 @@ namespace Tok { #endif // This metafunction maps a StringLike to a View (currently, QChar, - // QStringView, QLatin1String). This is what QStringTokenizerBase + // QStringView, QLatin1StringView). This is what QStringTokenizerBase // operates on. QStringTokenizer adds pinning to keep rvalues alive // for the duration of the algorithm. template <typename String> @@ -294,14 +257,30 @@ class QStringTokenizer using if_haystack_not_pinned = typename if_haystack_not_pinned_impl<Container, HPin>::type; template <typename Container, typename Iterator = decltype(std::begin(std::declval<Container>()))> using if_compatible_container = typename std::enable_if< - std::is_same< + std::is_convertible< typename Base::value_type, typename std::iterator_traits<Iterator>::value_type >::value, bool >::type; public: - using value_type = typename Base::value_type; + using value_type = typename Base::value_type; + using difference_type = typename Base::difference_type; + using size_type = typename Base::size_type; + using reference = typename Base::reference; + using const_reference = typename Base::const_reference; + using pointer = typename Base::pointer; + using const_pointer = typename Base::const_pointer; + using iterator = typename Base::iterator; + using const_iterator = typename Base::const_iterator; + using sentinel = typename Base::sentinel; + +#ifdef Q_QDOC + [[nodiscard]] iterator begin() const noexcept { return Base::begin(); } + [[nodiscard]] iterator cbegin() const noexcept { return begin(); } + [[nodiscard]] constexpr sentinel end() const noexcept { return {}; } + [[nodiscard]] constexpr sentinel cend() const noexcept { return {}; } +#endif constexpr explicit QStringTokenizer(Haystack haystack, Needle needle, Qt::CaseSensitivity cs, @@ -328,22 +307,26 @@ public: this->needleView(needle), sb, cs} {} +#ifdef Q_QDOC + template<typename LContainer> LContainer toContainer(LContainer &&c = {}) const & {} + template<typename RContainer> RContainer toContainer(RContainer &&c = {}) const && {} +#else template<typename Container = QList<value_type>, if_compatible_container<Container> = true> Container toContainer(Container &&c = {}) const & { for (auto e : *this) c.emplace_back(e); - return c; + return std::forward<Container>(c); } - template<typename Container = QList<value_type>, if_compatible_container<Container> = true, if_haystack_not_pinned<Container> = true> Container toContainer(Container &&c = {}) const && { for (auto e : *this) c.emplace_back(e); - return c; + return std::forward<Container>(c); } +#endif }; namespace QtPrivate { @@ -395,7 +378,7 @@ QStringTokenizer(Haystack&&, Needle&&, Qt::CaseSensitivity, Qt::SplitBehavior) #undef Q_TOK_RESULT template <typename Haystack, typename Needle, typename...Flags> -Q_REQUIRED_RESULT constexpr auto +[[nodiscard]] constexpr auto qTokenize(Haystack &&h, Needle &&n, Flags...flags) noexcept(QtPrivate::Tok::is_nothrow_constructible_from<Haystack, Needle>::value) -> decltype(QtPrivate::Tok::TokenizerResult<Haystack, Needle>{std::forward<Haystack>(h), |