From 86ad338aa4a6d60ae6b98c62bb2259cf36afee3e Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Fri, 9 Apr 2021 18:14:42 +0200 Subject: [PATCH] Q{*String,ByteArray}View::length(): use qsizetype, not int Looks like these ones have been forgotten in the Qt 5 -> 6 upgrade to qsizetype. Change them to return qsizetype as well, and fix the docs. Being entirely inline, non-exported classes, we should be able to get away with it without affecting binary compatibility. Moreover, there's no reason for keeping them deprecated. Requires some minor adjustments, just like the ones done for size()'s changes. [ChangeLog][QtCore][QStringView] The length() function now returns `qsizetype`, not `int`, for consistency with the other string and container classes in Qt. Following this change, it has been un-deprecated. [ChangeLog][QtCore][QAnyStringView] The length() function now returns `qsizetype`, not `int`, for consistency with the other string and container classes in Qt. Following this change, it has been un-deprecated. [ChangeLog][QtCore][QUtf8StringView] The length() function now returns `qsizetype`, not `int`, for consistency with the other string and container classes in Qt. Following this change, it has been un-deprecated. [ChangeLog][QtCore][QByteArrayView] The length() function now returns `qsizetype`, not `int`, for consistency with the other string and container classes in Qt. Following this change, it has been un-deprecated. Fixes: QTBUG-92496 Change-Id: Ie0f4939d1083884e95d4725f891b6c6764532cb8 Reviewed-by: Thiago Macieira --- src/corelib/global/qlogging.cpp | 8 ++++---- src/corelib/text/qanystringview.h | 8 ++------ src/corelib/text/qanystringview.qdoc | 8 +------- src/corelib/text/qbytearrayview.h | 8 ++------ src/corelib/text/qbytearrayview.qdoc | 12 +----------- src/corelib/text/qstringview.cpp | 11 +---------- src/corelib/text/qstringview.h | 4 ++-- src/corelib/text/qutf8stringview.h | 8 ++------ src/corelib/text/qutf8stringview.qdoc | 8 +------- 9 files changed, 16 insertions(+), 59 deletions(-) diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index 748ca606618..f4fac3f35e4 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -1667,7 +1667,7 @@ static bool android_default_message_handler(QtMsgType type, #ifdef Q_OS_WIN static void win_outputDebugString_helper(QStringView message) { - const int maxOutputStringLength = 32766; + const qsizetype maxOutputStringLength = 32766; static QBasicMutex m; auto locker = qt_unique_lock(m); // fast path: Avoid string copies if one output is enough @@ -1675,9 +1675,9 @@ static void win_outputDebugString_helper(QStringView message) OutputDebugString(reinterpret_cast(message.utf16())); } else { wchar_t *messagePart = new wchar_t[maxOutputStringLength + 1]; - for (int i = 0; i < message.length(); i += maxOutputStringLength) { - const int length = std::min(message.length() - i, maxOutputStringLength); - const int len = message.mid(i, length).toWCharArray(messagePart); + for (qsizetype i = 0; i < message.length(); i += maxOutputStringLength) { + const qsizetype length = std::min(message.length() - i, maxOutputStringLength); + const qsizetype len = message.mid(i, length).toWCharArray(messagePart); Q_ASSERT(len == length); messagePart[len] = 0; OutputDebugString(messagePart); diff --git a/src/corelib/text/qanystringview.h b/src/corelib/text/qanystringview.h index ff1f8a4050a..68754931e91 100644 --- a/src/corelib/text/qanystringview.h +++ b/src/corelib/text/qanystringview.h @@ -217,12 +217,8 @@ public: // [[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 + [[nodiscard]] constexpr qsizetype length() const noexcept + { return size(); } private: [[nodiscard]] friend inline bool operator==(QAnyStringView lhs, QAnyStringView rhs) noexcept diff --git a/src/corelib/text/qanystringview.qdoc b/src/corelib/text/qanystringview.qdoc index c1a16816950..cb98fb00521 100644 --- a/src/corelib/text/qanystringview.qdoc +++ b/src/corelib/text/qanystringview.qdoc @@ -322,17 +322,11 @@ /*! \fn int QAnyStringView::length() const - \obsolete - Use size() instead, and port callers to qsizetype. - Same as size(), except that it returns the result as an \c int. + Same as size(). This function is provided for compatibility with other Qt containers. - \warning QAnyStringView can represent strings with more than 2\sup{31} characters. - Calling this function on a string view for which size() returns a value greater - than \c{INT_MAX} constitutes undefined behavior. - \sa size() */ diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index 4934294a815..02ce49613e1 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -291,12 +291,8 @@ public: // [[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! */ - { Q_ASSERT(int(size()) == size()); return int(size()); } -#endif + [[nodiscard]] constexpr qsizetype length() const noexcept + { return size(); } [[nodiscard]] constexpr char first() const { return front(); } [[nodiscard]] constexpr char last() const { return back(); } diff --git a/src/corelib/text/qbytearrayview.qdoc b/src/corelib/text/qbytearrayview.qdoc index 36571e53d46..91048c5dad8 100644 --- a/src/corelib/text/qbytearrayview.qdoc +++ b/src/corelib/text/qbytearrayview.qdoc @@ -470,23 +470,13 @@ \sa empty(), isEmpty(), isNull() */ -#if QT_DEPRECATED_SINCE(6, 0) /*! \fn int QByteArrayView::length() const - \obsolete - Use size() and port callers to qsizetype. - Same as size(), but returns the result as an \c int. - - This function is provided for compatibility with other Qt containers. - - \warning QByteArrayView can represent data with more than 2\sup{31} bytes. - Calling this function on a byte array view for which size() returns a value greater - than \c{INT_MAX} constitutes undefined behavior. + Same as size(). \sa empty(), isEmpty(), isNull(), size() */ -#endif /*! \fn char QByteArrayView::operator[](qsizetype n) const diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp index 1c3249fbfb4..e003f257d75 100644 --- a/src/corelib/text/qstringview.cpp +++ b/src/corelib/text/qstringview.cpp @@ -130,11 +130,6 @@ QT_BEGIN_NAMESPACE \typedef QStringView::size_type Alias for qsizetype. Provided for compatibility with the STL. - - Unlike other Qt classes, QStringView uses qsizetype as its \c size_type, to allow - accepting data from \c{std::basic_string} without truncation. The Qt API functions, - for example length(), return \c int, while the STL-compatible functions, for example - size(), return \c size_type. */ /*! @@ -530,14 +525,10 @@ QT_BEGIN_NAMESPACE /*! \fn int QStringView::length() const - Same as size(), except returns the result as an \c int. + Same as size(). This function is provided for compatibility with other Qt containers. - \warning QStringView can represent strings with more than 2\sup{31} characters. - Calling this function on a string view for which size() returns a value greater - than \c{INT_MAX} constitutes undefined behavior. - \sa empty(), isEmpty(), isNull(), size() */ diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index c0082b70b5c..e34418e9560 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -446,8 +446,8 @@ public: [[nodiscard]] const_iterator constEnd() const noexcept { return end(); } [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; } [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); } - [[nodiscard]] constexpr int length() const /* not nothrow! */ - { return Q_ASSERT(int(size()) == size()), int(size()); } + [[nodiscard]] constexpr qsizetype length() const noexcept + { return size(); } [[nodiscard]] constexpr QChar first() const { return front(); } [[nodiscard]] constexpr QChar last() const { return back(); } private: diff --git a/src/corelib/text/qutf8stringview.h b/src/corelib/text/qutf8stringview.h index 5069bf7b070..a49aa08e4ca 100644 --- a/src/corelib/text/qutf8stringview.h +++ b/src/corelib/text/qutf8stringview.h @@ -306,12 +306,8 @@ public: // [[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 + [[nodiscard]] constexpr qsizetype length() const noexcept + { return size(); } private: [[nodiscard]] static inline int compare(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept diff --git a/src/corelib/text/qutf8stringview.qdoc b/src/corelib/text/qutf8stringview.qdoc index 8ff208ff23e..36e09421a38 100644 --- a/src/corelib/text/qutf8stringview.qdoc +++ b/src/corelib/text/qutf8stringview.qdoc @@ -526,17 +526,11 @@ /*! \fn int QUtf8StringView::length() const - \obsolete - Use size() and port callers to qsizetype. - Same as size(), except returns the result as an \c int. + Same as size(). This function is provided for compatibility with other Qt containers. - \warning QUtf8StringView can represent strings with more than 2\sup{31} code points. - Calling this function on a string view for which size() returns a value greater - than \c{INT_MAX} constitutes undefined behavior. - \sa empty(), isEmpty(), isNull(), size() */