summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-09-23 09:18:56 +0200
committerLars Knoll <lars.knoll@qt.io>2020-10-12 11:24:58 +0200
commit84b53a4514c62ab06034de555ed03e41ab718dfc (patch)
tree58f5bffdaf80648cfe607477d84e22c4965f0674
parentfd839b60a6617a11385326701449fb6825f81eff (diff)
Loosen the API contract for QStringView::left() and friends
Remove the assertions in QStringView::left() and friends and turn them into runtime bounds checking instead. This changes the behavior to be compatible with what we do in Qt 6 (see change d2833a3ce5af725d66ef9338f2a61b766dd3cb2d) and simplifies porting from QStringRef to QStringView. Task-number: QTBUG-86516 Change-Id: I28e3beb4f42c7b9dfc52813cbcfa322a3df5685a Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
-rw-r--r--src/corelib/text/qstringview.cpp15
-rw-r--r--src/corelib/text/qstringview.h16
2 files changed, 22 insertions, 9 deletions
diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp
index 9df656dc59..93febceb19 100644
--- a/src/corelib/text/qstringview.cpp
+++ b/src/corelib/text/qstringview.cpp
@@ -604,7 +604,9 @@ QT_BEGIN_NAMESPACE
Returns the substring starting at position \a start in this object,
and extending to the end of the string.
- \note The behavior is undefined when \a start < 0 or \a start > size().
+ \note Until 5.15.1, the behavior was undefined when \a start < 0 or
+ \a start > size(). Since 5.15.2, the behavior is compatible with
+ QString::mid().
\sa left(), right(), chopped(), chop(), truncate()
*/
@@ -616,8 +618,9 @@ QT_BEGIN_NAMESPACE
Returns the substring of length \a length starting at position
\a start in this object.
- \note The behavior is undefined when \a start < 0, \a length < 0,
- or \a start + \a length > size().
+ \note Until 5.15.1, the behavior was undefined when \a start < 0, \a length < 0,
+ or \a start + \a length > size(). Since 5.15.2, the behavior is compatible with
+ QString::mid().
\sa left(), right(), chopped(), chop(), truncate()
*/
@@ -628,7 +631,8 @@ QT_BEGIN_NAMESPACE
Returns the substring of length \a length starting at position
0 in this object.
- \note The behavior is undefined when \a length < 0 or \a length > size().
+ \note Until 5.15.1, the behavior was undefined when \a length < 0 or \a length > size().
+ Since 5.15.2, the behavior is compatible with QString::left().
\sa mid(), right(), chopped(), chop(), truncate()
*/
@@ -639,7 +643,8 @@ QT_BEGIN_NAMESPACE
Returns the substring of length \a length starting at position
size() - \a length in this object.
- \note The behavior is undefined when \a length < 0 or \a length > size().
+ \note Until 5.15.1, the behavior was undefined when \a length < 0 or \a length > size().
+ Since 5.15.2, the behavior is compatible with QString::right().
\sa mid(), left(), chopped(), chop(), truncate()
*/
diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h
index 9b85dcffa5..72ddd91968 100644
--- a/src/corelib/text/qstringview.h
+++ b/src/corelib/text/qstringview.h
@@ -238,13 +238,21 @@ public:
Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar at(qsizetype n) const { return (*this)[n]; }
Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView mid(qsizetype pos) const
- { return Q_ASSERT(pos >= 0), Q_ASSERT(pos <= size()), QStringView(m_data + pos, m_size - pos); }
+ {
+ return QStringView(m_data + qBound(qsizetype(0), pos, m_size), m_size - qBound(qsizetype(0), pos, m_size));
+ }
Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView mid(qsizetype pos, qsizetype n) const
- { return Q_ASSERT(pos >= 0), Q_ASSERT(n >= 0), Q_ASSERT(pos + n <= size()), QStringView(m_data + pos, n); }
+ {
+ return QStringView(m_data + qBound(qsizetype(0), pos, m_size), qBound(qsizetype(0), pos + n, m_size) - qBound(qsizetype(0), pos, m_size));
+ }
Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView left(qsizetype n) const
- { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, n); }
+ {
+ return QStringView(m_data, (size_t(n) > size_t(m_size) ? m_size : n));
+ }
Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView right(qsizetype n) const
- { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data + m_size - n, n); }
+ {
+ return QStringView(m_data + m_size - (size_t(n) > size_t(m_size) ? m_size : n), (size_t(n) > size_t(m_size) ? m_size : n));
+ }
Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView chopped(qsizetype n) const
{ return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, m_size - n); }