diff --git a/src/corelib/text/qutf8stringview.h b/src/corelib/text/qutf8stringview.h index f205a48acd3..a96844305f5 100644 --- a/src/corelib/text/qutf8stringview.h +++ b/src/corelib/text/qutf8stringview.h @@ -286,8 +286,13 @@ public: [[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]; } - [[nodiscard]] Q_IMPLICIT operator std::basic_string_view() const noexcept - { return std::basic_string_view(data(), size_t(size())); } + [[nodiscard]] Q_IMPLICIT operator std::string_view() const noexcept + { return std::string_view{reinterpret_cast(data()), size_t(size())}; } + +#ifdef __cpp_lib_char8_t + [[nodiscard]] Q_IMPLICIT operator std::u8string_view() const noexcept + { return std::u8string_view{utf8(), size_t(size())}; } +#endif [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); } diff --git a/src/corelib/text/qutf8stringview.qdoc b/src/corelib/text/qutf8stringview.qdoc index 8bd1c20fc05..6261927f682 100644 --- a/src/corelib/text/qutf8stringview.qdoc +++ b/src/corelib/text/qutf8stringview.qdoc @@ -735,13 +735,24 @@ */ -/*! \fn QUtf8StringView::operator std::basic_string_view() const +/*! + \fn QUtf8StringView::operator std::string_view() const \since 6.7 Converts this QUtf8StringView object to a - \c{std::basic_string_view} object. The returned view will have the - same data pointer and length of this view. The character type of - the returned view will be \c{storage_type}. + \c{std::string_view} object. The returned view will have the + same data pointer and length as this view. +*/ + +/*! + \fn QUtf8StringView::operator std::u8string_view() const + \since 6.10 + + Converts this QUtf8StringView object to a \c{std::u8string_view} + object. The returned view will have the same data pointer and length + as this view. + + This function is only available when compiling in C++20 mode. */ /*! diff --git a/tests/auto/corelib/text/qutf8stringview/tst_qutf8stringview.cpp b/tests/auto/corelib/text/qutf8stringview/tst_qutf8stringview.cpp index 03ede935daf..0bdddf9c8e8 100644 --- a/tests/auto/corelib/text/qutf8stringview/tst_qutf8stringview.cpp +++ b/tests/auto/corelib/text/qutf8stringview/tst_qutf8stringview.cpp @@ -431,6 +431,30 @@ void tst_QUtf8StringView::std_stringview_conversion() QCOMPARE(sv.size(), size_t(12)); QCOMPARE(sv, std::basic_string_view("Hello\0world\0", 12)); } + +#ifdef __cpp_lib_char8_t + { + QUtf8StringView s; + std::u8string_view sv(s); + QCOMPARE(sv, std::u8string_view()); + + s = u8""; + sv = s; + QCOMPARE(s.size(), 0); + QCOMPARE(sv.size(), size_t(0)); + QCOMPARE(sv, std::u8string_view()); + + s = u8"Hello"; + sv = s; + QCOMPARE(sv, std::u8string_view(u8"Hello")); + + s = QUtf8StringView::fromArray(u8"Hello\0world"); + sv = s; + QCOMPARE(s.size(), 12); + QCOMPARE(sv.size(), size_t(12)); + QCOMPARE(sv, std::u8string_view(u8"Hello\0world\0", 12)); + } +#endif } namespace QUtf8StringViewOverloadResolution {