Add conversion from QUtf8StringView to std::u8string_view
Writing the tests for QUtf8StringView showed that this conversation does not work if the underlying storage_type is not char8_t. This is something a user rightfully expects from our library and we therefore added an explicit conversion operator for it. [ChangeLog][QtCore][QUtf8StringView] Added std::u8string_view operator if compiled with C++20. Pick-to: 6.10 Change-Id: Ia80507bdd76686bee16a40745be064e9bdfef130 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
parent
cfd52c5c5f
commit
bb48dbb113
@ -286,8 +286,13 @@ public:
|
|||||||
[[nodiscard]] constexpr storage_type front() const { return Q_ASSERT(!empty()), m_data[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]; }
|
[[nodiscard]] constexpr storage_type back() const { return Q_ASSERT(!empty()), m_data[m_size - 1]; }
|
||||||
|
|
||||||
[[nodiscard]] Q_IMPLICIT operator std::basic_string_view<storage_type>() const noexcept
|
[[nodiscard]] Q_IMPLICIT operator std::string_view() const noexcept
|
||||||
{ return std::basic_string_view<storage_type>(data(), size_t(size())); }
|
{ return std::string_view{reinterpret_cast<const char*>(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(); }
|
[[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); }
|
||||||
|
|
||||||
|
@ -735,13 +735,24 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*! \fn QUtf8StringView::operator std::basic_string_view<storage_type>() const
|
/*!
|
||||||
|
\fn QUtf8StringView::operator std::string_view() const
|
||||||
\since 6.7
|
\since 6.7
|
||||||
|
|
||||||
Converts this QUtf8StringView object to a
|
Converts this QUtf8StringView object to a
|
||||||
\c{std::basic_string_view} object. The returned view will have the
|
\c{std::string_view} object. The returned view will have the
|
||||||
same data pointer and length of this view. The character type of
|
same data pointer and length as this view.
|
||||||
the returned view will be \c{storage_type}.
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -431,6 +431,30 @@ void tst_QUtf8StringView::std_stringview_conversion()
|
|||||||
QCOMPARE(sv.size(), size_t(12));
|
QCOMPARE(sv.size(), size_t(12));
|
||||||
QCOMPARE(sv, std::basic_string_view<QUtf8StringView::storage_type>("Hello\0world\0", 12));
|
QCOMPARE(sv, std::basic_string_view<QUtf8StringView::storage_type>("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 {
|
namespace QUtf8StringViewOverloadResolution {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user