/**************************************************************************** ** ** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz ** 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$ ** ****************************************************************************/ #ifndef QUTF8STRINGVIEW_H #define QUTF8STRINGVIEW_H #include #include // for QContainerImplHelper #include QT_BEGIN_NAMESPACE template class QBasicUtf8StringView; class QByteArray; class QLatin1String; namespace QtPrivate { template using IsCompatibleChar8TypeHelper = std::disjunction< #ifdef __cpp_char8_t std::is_same, #endif std::is_same, std::is_same, std::is_same >; template using IsCompatibleChar8Type = IsCompatibleChar8TypeHelper>>; template struct IsCompatiblePointer8Helper : std::false_type {}; template struct IsCompatiblePointer8Helper : IsCompatibleChar8Type {}; template using IsCompatiblePointer8 = IsCompatiblePointer8Helper>>; template struct IsContainerCompatibleWithQUtf8StringView : std::false_type {}; template struct IsContainerCompatibleWithQUtf8StringView()))>, // ... and that has a suitable size ... std::is_convertible< decltype(std::size(std::declval())), qsizetype >, // ... and it's a range as it defines an iterator-like API IsCompatibleChar8Type()))>::value_type >, std::is_convertible< decltype( std::begin(std::declval()) != std::end(std::declval()) ), bool >, // This needs to be treated specially due to the empty vs null distinction std::negation, QByteArray>>, // This has a compatible value_type, but explicitly a different encoding std::negation, QLatin1String>>, // Don't make an accidental copy constructor std::negation, QBasicUtf8StringView>, std::is_same, QBasicUtf8StringView> >> >>> : std::true_type {}; struct hide_char8_t { #ifdef __cpp_char8_t using type = char8_t; #endif }; struct wrap_char { using type = char; }; } // namespace QtPrivate #ifdef Q_CLANG_QDOC #define QBasicUtf8StringView QUtf8StringView #else template #endif class QBasicUtf8StringView { public: #ifndef Q_CLANG_QDOC using storage_type = typename std::conditional::type::type; #else using storage_type = typename QtPrivate::hide_char8_t; #endif typedef const storage_type value_type; typedef qptrdiff difference_type; typedef qsizetype size_type; typedef value_type &reference; typedef value_type &const_reference; typedef value_type *pointer; typedef value_type *const_pointer; typedef pointer iterator; typedef const_pointer const_iterator; typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; private: template using if_compatible_char = std::enable_if_t::value, bool>; template using if_compatible_pointer = std::enable_if_t::value, bool>; template using if_compatible_qstring_like = std::enable_if_t, bool>; template using if_compatible_container = std::enable_if_t::value, bool>; template static constexpr qsizetype lengthHelperContainer(const Container &c) noexcept { return qsizetype(std::size(c)); } // Note: Do not replace with std::size(const Char (&)[N]), cause the result // will be of by one. template static constexpr qsizetype lengthHelperContainer(const Char (&str)[N]) noexcept { const auto it = std::char_traits::find(str, N, Char(0)); const auto end = it ? it : std::next(str, N); return qsizetype(std::distance(str, end)); } template static const storage_type *castHelper(const Char *str) noexcept { return reinterpret_cast(str); } static constexpr const storage_type *castHelper(const storage_type *str) noexcept { return str; } public: constexpr QBasicUtf8StringView() noexcept : m_data(nullptr), m_size(0) {} constexpr QBasicUtf8StringView(std::nullptr_t) noexcept : QBasicUtf8StringView() {} template = true> constexpr QBasicUtf8StringView(const Char *str, qsizetype len) : m_data(castHelper(str)), m_size((Q_ASSERT(len >= 0), Q_ASSERT(str || !len), len)) {} template = true> constexpr QBasicUtf8StringView(const Char *f, const Char *l) : QBasicUtf8StringView(f, l - f) {} #ifdef Q_CLANG_QDOC template constexpr QBasicUtf8StringView(const Char (&array)[N]) noexcept; template constexpr QBasicUtf8StringView(const Char *str) noexcept; #else template = true> constexpr QBasicUtf8StringView(const Pointer &str) noexcept : QBasicUtf8StringView(str, str ? std::char_traits>>::length(str) : 0) {} #endif #ifdef Q_CLANG_QDOC QBasicUtf8StringView(const QByteArray &str) noexcept; #else template = true> QBasicUtf8StringView(const String &str) noexcept : QBasicUtf8StringView(str.isNull() ? nullptr : str.data(), qsizetype(str.size())) {} #endif template = true> constexpr QBasicUtf8StringView(const Container &c) noexcept : QBasicUtf8StringView(std::data(c), lengthHelperContainer(c)) {} #ifdef __cpp_char8_t constexpr QBasicUtf8StringView(QBasicUtf8StringView other) : QBasicUtf8StringView(other.data(), other.size()) {} #endif template = true> [[nodiscard]] constexpr static QBasicUtf8StringView fromArray(const Char (&string)[Size]) noexcept { return QBasicUtf8StringView(string, Size); } [[nodiscard]] inline QString toString() const; // defined in qstring.h [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; } [[nodiscard]] const_pointer data() const noexcept { return reinterpret_cast(m_data); } #if defined(__cpp_char8_t) || defined(Q_CLANG_QDOC) [[nodiscard]] const char8_t *utf8() const noexcept { return reinterpret_cast(m_data); } #endif [[nodiscard]] constexpr storage_type operator[](qsizetype n) const { return Q_ASSERT(n >= 0), Q_ASSERT(n < size()), m_data[n]; } // // QString API // [[nodiscard]] constexpr storage_type at(qsizetype n) const { return (*this)[n]; } [[nodiscard]] constexpr QBasicUtf8StringView mid(qsizetype pos, qsizetype n = -1) const { using namespace QtPrivate; auto result = QContainerImplHelper::mid(size(), &pos, &n); return result == QContainerImplHelper::Null ? QBasicUtf8StringView() : QBasicUtf8StringView(m_data + pos, n); } [[nodiscard]] constexpr QBasicUtf8StringView left(qsizetype n) const { if (size_t(n) >= size_t(size())) n = size(); return QBasicUtf8StringView(m_data, n); } [[nodiscard]] constexpr QBasicUtf8StringView right(qsizetype n) const { if (size_t(n) >= size_t(size())) n = size(); return QBasicUtf8StringView(m_data + m_size - n, n); } [[nodiscard]] constexpr QBasicUtf8StringView sliced(qsizetype pos) const { verify(pos); return QBasicUtf8StringView{m_data + pos, m_size - pos}; } [[nodiscard]] constexpr QBasicUtf8StringView sliced(qsizetype pos, qsizetype n) const { verify(pos, n); return QBasicUtf8StringView(m_data + pos, n); } [[nodiscard]] constexpr QBasicUtf8StringView first(qsizetype n) const { verify(n); return QBasicUtf8StringView(m_data, n); } [[nodiscard]] constexpr QBasicUtf8StringView last(qsizetype n) const { verify(n); return QBasicUtf8StringView(m_data + m_size - n, n); } [[nodiscard]] constexpr QBasicUtf8StringView chopped(qsizetype n) const { verify(n); return QBasicUtf8StringView(m_data, m_size - n); } constexpr void truncate(qsizetype n) { verify(n); m_size = n; } constexpr void chop(qsizetype n) { verify(n); m_size -= n; } // // STL compatibility API: // [[nodiscard]] const_iterator begin() const noexcept { return data(); } [[nodiscard]] const_iterator end() const noexcept { return data() + size(); } [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); } [[nodiscard]] const_iterator cend() const noexcept { return end(); } [[nodiscard]] const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } [[nodiscard]] const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); } [[nodiscard]] const_reverse_iterator crend() const noexcept { return rend(); } [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; } [[nodiscard]] constexpr storage_type front() const { return Q_ASSERT(!empty()), m_data[0]; } [[nodiscard]] constexpr storage_type back() const { return Q_ASSERT(!empty()), m_data[m_size - 1]; } // // Qt compatibility API: // [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; } [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); } #if QT_DEPRECATED_SINCE(6, 0) [[nodiscard]] Q_DECL_DEPRECATED_X("Use size() and port callers to qsizetype.") constexpr int length() const /* not nothrow! */ { return Q_ASSERT(int(size()) == size()), int(size()); } #endif private: [[nodiscard]] static inline int compare(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept { return QtPrivate::compareStrings(QBasicUtf8StringView(lhs.data(), lhs.size()), QBasicUtf8StringView(rhs.data(), rhs.size())); } [[nodiscard]] friend inline bool operator==(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept { return QtPrivate::equalStrings(QBasicUtf8StringView(lhs.data(), lhs.size()), QBasicUtf8StringView(rhs.data(), rhs.size())); } [[nodiscard]] friend inline bool operator!=(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept { return !operator==(lhs, rhs); } #ifdef __cpp_impl_three_way_comparison [[nodiscard]] friend inline auto operator<=>(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept { return QBasicUtf8StringView::compare(lhs, rhs) <=> 0; } #else [[nodiscard]] friend inline bool operator<=(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept { return QBasicUtf8StringView::compare(lhs, rhs) <= 0; } [[nodiscard]] friend inline bool operator>=(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept { return QBasicUtf8StringView::compare(lhs, rhs) >= 0; } [[nodiscard]] friend inline bool operator<(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept { return QBasicUtf8StringView::compare(lhs, rhs) < 0; } [[nodiscard]] friend inline bool operator>(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept { return QBasicUtf8StringView::compare(lhs, rhs) > 0; } #endif Q_ALWAYS_INLINE constexpr void verify(qsizetype pos, qsizetype n = 0) const { Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); Q_ASSERT(n >= 0); Q_ASSERT(n <= size() - pos); } const storage_type *m_data; qsizetype m_size; }; #ifdef Q_CLANG_QDOC #undef QBasicUtf8StringView #else template Q_DECLARE_TYPEINFO_BODY(QBasicUtf8StringView, Q_PRIMITIVE_TYPE); // ### Qt 7: remove the non-char8_t version of QUtf8StringView QT_BEGIN_NO_CHAR8_T_NAMESPACE using QUtf8StringView = QBasicUtf8StringView; QT_END_NO_CHAR8_T_NAMESPACE QT_BEGIN_HAS_CHAR8_T_NAMESPACE using QUtf8StringView = QBasicUtf8StringView; QT_END_HAS_CHAR8_T_NAMESPACE #endif // Q_CLANG_QDOC template , bool> = true> [[nodiscard]] inline q_no_char8_t::QUtf8StringView qToUtf8StringViewIgnoringNull(const QStringLike &s) noexcept { return q_no_char8_t::QUtf8StringView(s.data(), s.size()); } QT_END_NAMESPACE #endif /* QUTF8STRINGVIEW_H */