diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2023-09-25 11:37:53 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2023-10-25 12:01:58 -0700 |
commit | 2fefc8c63c1090925986e7650626a91af308d82e (patch) | |
tree | d16dee562b35faeea01f23d428d6ec3da8a4cd73 /src/corelib | |
parent | QString/QByteArray: make the sliced(pos) overload call the other sliced (diff) | |
download | qtbase-2fefc8c63c1090925986e7650626a91af308d82e.tar.xz qtbase-2fefc8c63c1090925986e7650626a91af308d82e.zip |
QString/QByteArray: add lvalue and rvalue overloads of left/mid/right
The first/last/sliced API may be what we suggest users use, but the vast
majority of the installed codebase uses left/mid/right because they've
been available since time immemorial.
An additional benefit of this is to make left() and right() available as
inline methods.
Change-Id: Ifeb6206a9fa04424964bfffd1788383817ed906c
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/compat/removed_api.cpp | 70 | ||||
-rw-r--r-- | src/corelib/text/qbytearray.cpp | 49 | ||||
-rw-r--r-- | src/corelib/text/qbytearray.h | 34 | ||||
-rw-r--r-- | src/corelib/text/qstring.cpp | 46 | ||||
-rw-r--r-- | src/corelib/text/qstring.h | 34 |
5 files changed, 191 insertions, 42 deletions
diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index 5c3a587f6e..6f0bb2bf10 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -620,6 +620,44 @@ QStringView QXmlStreamAttributes::value(QLatin1StringView qualifiedName) const #include "qbytearray.h" +QByteArray QByteArray::left(qsizetype len) const +{ + if (len >= size()) + return *this; + if (len < 0) + len = 0; + return QByteArray(data(), len); +} + +QByteArray QByteArray::right(qsizetype len) const +{ + if (len >= size()) + return *this; + if (len < 0) + len = 0; + return QByteArray(end() - len, len); +} + +QByteArray QByteArray::mid(qsizetype pos, qsizetype len) const +{ + qsizetype p = pos; + qsizetype l = len; + using namespace QtPrivate; + switch (QContainerImplHelper::mid(size(), &p, &l)) { + case QContainerImplHelper::Null: + return QByteArray(); + case QContainerImplHelper::Empty: + { + return QByteArray(DataPointer::fromRawData(&_empty, 0)); + } + case QContainerImplHelper::Full: + return *this; + case QContainerImplHelper::Subset: + return QByteArray(d.data() + p, l); + } + Q_UNREACHABLE_RETURN(QByteArray()); +} + #ifdef Q_CC_MSVC // previously inline methods, only needed for MSVC compat QByteArray QByteArray::first(qsizetype n) const @@ -744,6 +782,38 @@ bool QMetaObject::invokeMethodImpl(QObject *object, QtPrivate::QSlotObjectBase * #include "qstring.h" +QString QString::left(qsizetype n) const +{ + if (size_t(n) >= size_t(size())) + return *this; + return QString((const QChar*) d.data(), n); +} + +QString QString::right(qsizetype n) const +{ + if (size_t(n) >= size_t(size())) + return *this; + return QString(constData() + size() - n, n); +} + +QString QString::mid(qsizetype position, qsizetype n) const +{ + qsizetype p = position; + qsizetype l = n; + using namespace QtPrivate; + switch (QContainerImplHelper::mid(size(), &p, &l)) { + case QContainerImplHelper::Null: + return QString(); + case QContainerImplHelper::Empty: + return QString(DataPointer::fromRawData(&_empty, 0)); + case QContainerImplHelper::Full: + return *this; + case QContainerImplHelper::Subset: + return QString(constData() + p, l); + } + Q_UNREACHABLE_RETURN(QString()); +} + #ifdef Q_CC_MSVC // previously inline methods, only needed for MSVC compat QString QString::first(qsizetype n) const diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 1821dd2c26..1f496bd404 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -3022,6 +3022,9 @@ bool QByteArray::isLower() const */ /*! + \fn QByteArray QByteArray::left(qsizetype len) const & + \fn QByteArray QByteArray::left(qsizetype len) && + Returns a byte array that contains the first \a len bytes of this byte array. @@ -3036,16 +3039,10 @@ bool QByteArray::isLower() const \sa first(), last(), startsWith(), chopped(), chop(), truncate() */ -QByteArray QByteArray::left(qsizetype len) const -{ - if (len >= size()) - return *this; - if (len < 0) - len = 0; - return QByteArray(data(), len); -} - /*! + \fn QByteArray QByteArray::right(qsizetype len) const & + \fn QByteArray QByteArray::right(qsizetype len) && + Returns a byte array that contains the last \a len bytes of this byte array. If you know that \a len cannot be out of bounds, use last() instead in new @@ -3058,16 +3055,11 @@ QByteArray QByteArray::left(qsizetype len) const \sa endsWith(), last(), first(), sliced(), chopped(), chop(), truncate() */ -QByteArray QByteArray::right(qsizetype len) const -{ - if (len >= size()) - return *this; - if (len < 0) - len = 0; - return QByteArray(end() - len, len); -} /*! + \fn QByteArray QByteArray::mid(qsizetype pos, qsizetype len) const & + \fn QByteArray QByteArray::mid(qsizetype pos, qsizetype len) && + Returns a byte array containing \a len bytes from this byte array, starting at position \a pos. @@ -3081,7 +3073,7 @@ QByteArray QByteArray::right(qsizetype len) const \sa first(), last(), sliced(), chopped(), chop(), truncate() */ -QByteArray QByteArray::mid(qsizetype pos, qsizetype len) const +QByteArray QByteArray::mid(qsizetype pos, qsizetype len) const & { qsizetype p = pos; qsizetype l = len; @@ -3096,7 +3088,26 @@ QByteArray QByteArray::mid(qsizetype pos, qsizetype len) const case QContainerImplHelper::Full: return *this; case QContainerImplHelper::Subset: - return QByteArray(d.data() + p, l); + return sliced(p, l); + } + Q_UNREACHABLE_RETURN(QByteArray()); +} + +QByteArray QByteArray::mid(qsizetype pos, qsizetype len) && +{ + qsizetype p = pos; + qsizetype l = len; + using namespace QtPrivate; + switch (QContainerImplHelper::mid(size(), &p, &l)) { + case QContainerImplHelper::Null: + return QByteArray(); + case QContainerImplHelper::Empty: + resize(0); // keep capacity if we've reserve()d + [[fallthrough]]; + case QContainerImplHelper::Full: + return std::move(*this); + case QContainerImplHelper::Subset: + return std::move(*this).sliced(p, l); } Q_UNREACHABLE_RETURN(QByteArray()); } diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index b5278dbba9..1f3316eed6 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -151,17 +151,43 @@ public: inline int compare(QByteArrayView a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; - [[nodiscard]] QByteArray left(qsizetype len) const; - [[nodiscard]] QByteArray right(qsizetype len) const; - [[nodiscard]] QByteArray mid(qsizetype index, qsizetype len = -1) const; - #if QT_CORE_REMOVED_SINCE(6, 7) + QByteArray left(qsizetype len) const; + QByteArray right(qsizetype len) const; + QByteArray mid(qsizetype index, qsizetype len = -1) const; QByteArray first(qsizetype n) const; QByteArray last(qsizetype n) const; QByteArray sliced(qsizetype pos) const; QByteArray sliced(qsizetype pos, qsizetype n) const; QByteArray chopped(qsizetype len) const; #else + [[nodiscard]] QByteArray left(qsizetype n) const & + { + if (n >= size()) + return *this; + return first(qMax(n, 0)); + } + [[nodiscard]] QByteArray left(qsizetype n) && + { + if (n >= size()) + return std::move(*this); + return std::move(*this).first(qMax(n, 0)); + } + [[nodiscard]] QByteArray right(qsizetype n) const & + { + if (n >= size()) + return *this; + return last(qMax(n, 0)); + } + [[nodiscard]] QByteArray right(qsizetype n) && + { + if (n >= size()) + return std::move(*this); + return std::move(*this).last(qMax(n, 0)); + } + [[nodiscard]] QByteArray mid(qsizetype index, qsizetype len = -1) const &; + [[nodiscard]] QByteArray mid(qsizetype index, qsizetype len = -1) &&; + [[nodiscard]] QByteArray first(qsizetype n) const & { verify(0, n); return sliced(0, n); } [[nodiscard]] QByteArray last(qsizetype n) const & diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index b5b9af55e2..50bb047253 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -5149,6 +5149,9 @@ QString QString::section(const QRegularExpression &re, qsizetype start, qsizetyp #endif // QT_CONFIG(regularexpression) /*! + \fn QString QString::left(qsizetype n) const & + \fn QString QString::left(qsizetype n) && + Returns a substring that contains the \a n leftmost characters of the string. @@ -5160,14 +5163,11 @@ QString QString::section(const QRegularExpression &re, qsizetype start, qsizetyp \sa first(), last(), startsWith(), chopped(), chop(), truncate() */ -QString QString::left(qsizetype n) const -{ - if (size_t(n) >= size_t(size())) - return *this; - return QString((const QChar*) d.data(), n); -} /*! + \fn QString QString::right(qsizetype n) const & + \fn QString QString::right(qsizetype n) && + Returns a substring that contains the \a n rightmost characters of the string. @@ -5179,14 +5179,11 @@ QString QString::left(qsizetype n) const \sa endsWith(), last(), first(), sliced(), chopped(), chop(), truncate() */ -QString QString::right(qsizetype n) const -{ - if (size_t(n) >= size_t(size())) - return *this; - return QString(constData() + size() - n, n); -} /*! + \fn QString QString::mid(qsizetype position, qsizetype n) const & + \fn QString QString::mid(qsizetype position, qsizetype n) && + Returns a string that contains \a n characters of this string, starting at the specified \a position index. @@ -5199,11 +5196,9 @@ QString QString::right(qsizetype n) const \a n is -1 (default), the function returns all characters that are available from the specified \a position. - \sa first(), last(), sliced(), chopped(), chop(), truncate() */ - -QString QString::mid(qsizetype position, qsizetype n) const +QString QString::mid(qsizetype position, qsizetype n) const & { qsizetype p = position; qsizetype l = n; @@ -5216,7 +5211,26 @@ QString QString::mid(qsizetype position, qsizetype n) const case QContainerImplHelper::Full: return *this; case QContainerImplHelper::Subset: - return QString(constData() + p, l); + return sliced(p, l); + } + Q_UNREACHABLE_RETURN(QString()); +} + +QString QString::mid(qsizetype position, qsizetype n) && +{ + qsizetype p = position; + qsizetype l = n; + using namespace QtPrivate; + switch (QContainerImplHelper::mid(size(), &p, &l)) { + case QContainerImplHelper::Null: + return QString(); + case QContainerImplHelper::Empty: + resize(0); // keep capacity if we've reserve()d + [[fallthrough]]; + case QContainerImplHelper::Full: + return std::move(*this); + case QContainerImplHelper::Subset: + return std::move(*this).sliced(p, l); } Q_UNREACHABLE_RETURN(QString()); } diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 411430a189..6ec7baf404 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -331,17 +331,45 @@ public: #if QT_CONFIG(regularexpression) [[nodiscard]] QString section(const QRegularExpression &re, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const; #endif - [[nodiscard]] QString left(qsizetype n) const; - [[nodiscard]] QString right(qsizetype n) const; - [[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) const; #if QT_CORE_REMOVED_SINCE(6, 7) + QString left(qsizetype n) const; + QString right(qsizetype n) const; + QString mid(qsizetype position, qsizetype n = -1) const; + QString first(qsizetype n) const; QString last(qsizetype n) const; QString sliced(qsizetype pos) const; QString sliced(qsizetype pos, qsizetype n) const; QString chopped(qsizetype n) const; #else + [[nodiscard]] QString left(qsizetype n) const & + { + if (size_t(n) >= size_t(size())) + return *this; + return first(n); + } + [[nodiscard]] QString left(qsizetype n) && + { + if (size_t(n) >= size_t(size())) + return std::move(*this); + return std::move(*this).first(n); + } + [[nodiscard]] QString right(qsizetype n) const & + { + if (size_t(n) >= size_t(size())) + return *this; + return last(n); + } + [[nodiscard]] QString right(qsizetype n) && + { + if (size_t(n) >= size_t(size())) + return std::move(*this); + return std::move(*this).last(n); + } + [[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) const &; + [[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) &&; + [[nodiscard]] QString first(qsizetype n) const & { verify(0, n); return sliced(0, n); } [[nodiscard]] QString last(qsizetype n) const & |