diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-09-23 09:18:56 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-10-12 11:24:58 +0200 |
commit | 84b53a4514c62ab06034de555ed03e41ab718dfc (patch) | |
tree | 58f5bffdaf80648cfe607477d84e22c4965f0674 /src/corelib/text/qstringview.h | |
parent | fd839b60a6617a11385326701449fb6825f81eff (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>
Diffstat (limited to 'src/corelib/text/qstringview.h')
-rw-r--r-- | src/corelib/text/qstringview.h | 16 |
1 files changed, 12 insertions, 4 deletions
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); } |