diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index dc929902faf..0ae17725301 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -72,6 +72,7 @@ QT_BEGIN_NAMESPACE class QString; class QStringRef; +class QStringView; namespace QtPrivate { template @@ -85,15 +86,6 @@ template struct IsCompatibleCharType : IsCompatibleCharTypeHelper::type>::type> {}; -template -struct IsCompatibleArrayHelper : std::false_type {}; -template -struct IsCompatibleArrayHelper - : IsCompatibleCharType {}; -template -struct IsCompatibleArray - : IsCompatibleArrayHelper::type>::type> {}; - template struct IsCompatiblePointerHelper : std::false_type {}; template @@ -103,17 +95,28 @@ template struct IsCompatiblePointer : IsCompatiblePointerHelper::type>::type> {}; -template -struct IsCompatibleStdBasicStringHelper : std::false_type {}; -template -struct IsCompatibleStdBasicStringHelper > - : IsCompatibleCharType {}; +template +struct IsContainerCompatibleWithQStringView : std::false_type {}; template -struct IsCompatibleStdBasicString - : IsCompatibleStdBasicStringHelper< - typename std::remove_cv::type>::type - > {}; +struct IsContainerCompatibleWithQStringView()) )>, + // ... and that has a suitable size ... + std::is_convertible()) ), qsizetype>, + // ... and it's a range as it defines an iterator-like API + IsCompatibleCharType()) )>::value_type>, + std::is_convertible< + decltype( std::begin(std::declval()) != std::end(std::declval()) ), + bool>, + + // These need to be treated specially due to the empty vs null distinction + std::negation, QString>>, + std::negation, QStringRef>>, + + // Don't make an accidental copy constructor + std::negation, QStringView>> + >>> : std::true_type {}; } // namespace QtPrivate @@ -138,23 +141,14 @@ private: template using if_compatible_char = typename std::enable_if::value, bool>::type; - template - using if_compatible_array = typename std::enable_if::value, bool>::type; - template using if_compatible_pointer = typename std::enable_if::value, bool>::type; - template - using if_compatible_string = typename std::enable_if::value, bool>::type; - template using if_compatible_qstring_like = typename std::enable_if::value || std::is_same::value, bool>::type; - template - static Q_DECL_CONSTEXPR qsizetype lengthHelperArray(const Char (&)[N]) noexcept - { - return qsizetype(N - 1); - } + template + using if_compatible_container = typename std::enable_if::value, bool>::type; template static qsizetype lengthHelperPointer(const Char *str) noexcept @@ -174,6 +168,18 @@ private: return QtPrivate::qustrlen(reinterpret_cast(str)); } + template + static Q_DECL_CONSTEXPR qsizetype lengthHelperContainer(const Container &c) noexcept + { + return qsizetype(std::size(c)); + } + + template + static Q_DECL_CONSTEXPR qsizetype lengthHelperContainer(const Char (&)[N]) noexcept + { + return qsizetype(N - 1); + } + template static const storage_type *castHelper(const Char *str) noexcept { return reinterpret_cast(str); } @@ -202,9 +208,6 @@ public: template Q_DECL_CONSTEXPR QStringView(const Char *str) noexcept; #else - template = true> - Q_DECL_CONSTEXPR QStringView(const Array &str) noexcept - : QStringView(str, lengthHelperArray(str)) {} template = true> Q_DECL_CONSTEXPR QStringView(const Pointer &str) noexcept @@ -220,9 +223,9 @@ public: : QStringView(str.isNull() ? nullptr : str.data(), qsizetype(str.size())) {} #endif - template = true> - Q_DECL_CONSTEXPR QStringView(const StdBasicString &str) noexcept - : QStringView(str.data(), qsizetype(str.size())) {} + template = true> + Q_DECL_CONSTEXPR QStringView(const Container &c) noexcept + : QStringView(std::data(c), lengthHelperContainer(c)) {} Q_REQUIRED_RESULT inline QString toString() const; // defined in qstring.h #if defined(Q_OS_DARWIN) || defined(Q_QDOC) diff --git a/tests/auto/corelib/text/qstringview/tst_qstringview.cpp b/tests/auto/corelib/text/qstringview/tst_qstringview.cpp index 573b846dae8..b2288f0785a 100644 --- a/tests/auto/corelib/text/qstringview/tst_qstringview.cpp +++ b/tests/auto/corelib/text/qstringview/tst_qstringview.cpp @@ -30,10 +30,19 @@ #include #include #include +#include +#include #include #include +#include +#include +#include + +// for negative testing (can't convert from) +#include +#include template using CanConvert = std::is_convertible; @@ -77,29 +86,38 @@ Q_STATIC_ASSERT(CanConvert::value); Q_STATIC_ASSERT(CanConvert< ushort*>::value); Q_STATIC_ASSERT(CanConvert::value); +static_assert(CanConvert>::value); +static_assert(CanConvert>::value); +static_assert(CanConvert>::value); +static_assert(CanConvert>::value); +static_assert(!CanConvert>::value); +static_assert(!CanConvert>::value); // // char16_t // -#if defined(Q_COMPILER_UNICODE_STRINGS) - Q_STATIC_ASSERT(!CanConvert::value); Q_STATIC_ASSERT(CanConvert< char16_t*>::value); Q_STATIC_ASSERT(CanConvert::value); -#endif - -#if defined(Q_STDLIB_UNICODE_STRINGS) - Q_STATIC_ASSERT(CanConvert< std::u16string >::value); Q_STATIC_ASSERT(CanConvert::value); Q_STATIC_ASSERT(CanConvert< std::u16string&>::value); Q_STATIC_ASSERT(CanConvert::value); -#endif +static_assert(CanConvert< std::u16string_view >::value); +static_assert(CanConvert::value); +static_assert(CanConvert< std::u16string_view&>::value); +static_assert(CanConvert::value); +static_assert(CanConvert>::value); +static_assert(CanConvert>::value); +static_assert(CanConvert>::value); +static_assert(CanConvert>::value); +static_assert(!CanConvert>::value); +static_assert(!CanConvert>::value); // // wchar_t @@ -123,6 +141,17 @@ Q_STATIC_ASSERT(CanConvert::value == CanConvertFromWCharT); Q_STATIC_ASSERT(CanConvert< std::wstring&>::value == CanConvertFromWCharT); Q_STATIC_ASSERT(CanConvert::value == CanConvertFromWCharT); +static_assert(CanConvert< std::wstring_view >::value == CanConvertFromWCharT); +static_assert(CanConvert::value == CanConvertFromWCharT); +static_assert(CanConvert< std::wstring_view&>::value == CanConvertFromWCharT); +static_assert(CanConvert::value == CanConvertFromWCharT); + +static_assert(CanConvert>::value == CanConvertFromWCharT); +static_assert(CanConvert>::value == CanConvertFromWCharT); +static_assert(CanConvert>::value == CanConvertFromWCharT); +static_assert(CanConvert>::value == CanConvertFromWCharT); +static_assert(!CanConvert>::value); +static_assert(!CanConvert>::value); class tst_QStringView : public QObject { @@ -207,6 +236,30 @@ private Q_SLOTS: fromStdString(); } + void fromUShortContainers() const + { + fromContainers(); + } + + void fromQCharContainers() const + { + fromContainers(); + } + + void fromChar16TContainers() const + { + fromContainers(); + } + + void fromWCharTContainers() const + { +#ifdef Q_OS_WIN + fromContainers(); +#else + QSKIP("This is a Windows-only test"); +#endif + } + void comparison(); void overloadResolution(); @@ -221,6 +274,8 @@ private: template void fromContainer() const; template + void fromContainers() const; + template void fromStdString() const { fromContainer >(); } }; @@ -501,6 +556,14 @@ void tst_QStringView::fromContainer() const conversion_tests(std::move(c)); } +template +void tst_QStringView::fromContainers() const +{ + fromContainer>(); + fromContainer>(); + fromContainer>(); +} + namespace help { template size_t size(const T &t) { return size_t(t.size()); }