QByteArrayView: add compare

There was previously no way to compare QByteArrayView to with another
QByteArrayView case-insensitively without allocating memory.

[ChangeLog][QtCore][QByteArrayView] Added compare(), enabling case
sensitive and insensitive comparison with other QByteArrayViews.

Change-Id: I7582cc414563ddbde26da35a568421edcc649f93
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Mårten Nordheim 2021-04-29 12:36:12 +02:00
parent e8297ba176
commit a5dc381b4c
3 changed files with 35 additions and 0 deletions

View File

@ -270,6 +270,8 @@ public:
[[nodiscard]] qsizetype count(char ch) const noexcept [[nodiscard]] qsizetype count(char ch) const noexcept
{ return QtPrivate::count(*this, QByteArrayView(&ch, 1)); } { return QtPrivate::count(*this, QByteArrayView(&ch, 1)); }
inline int compare(QByteArrayView a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
// //
// STL compatibility API: // STL compatibility API:
// //
@ -320,6 +322,12 @@ template<typename QByteArrayLike,
[[nodiscard]] inline QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept [[nodiscard]] inline QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept
{ return QByteArrayView(b.data(), b.size()); } { return QByteArrayView(b.data(), b.size()); }
inline int QByteArrayView::compare(QByteArrayView a, Qt::CaseSensitivity cs) const noexcept
{
return cs == Qt::CaseSensitive ? QtPrivate::compareMemory(*this, a) :
qstrnicmp(data(), size(), a.data(), a.size());
}
#if QT_DEPRECATED_SINCE(6, 0) #if QT_DEPRECATED_SINCE(6, 0)
QT_DEPRECATED_VERSION_X_6_0("Use the QByteArrayView overload.") QT_DEPRECATED_VERSION_X_6_0("Use the QByteArrayView overload.")
inline quint16 qChecksum(const char *s, qsizetype len, inline quint16 qChecksum(const char *s, qsizetype len,

View File

@ -353,6 +353,18 @@
\sa data(), begin(), end() \sa data(), begin(), end()
*/ */
/*!
\fn int QByteArrayView::compare(QByteArrayView bv, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
\since 6.2
Returns an integer less than, equal to, or greater than zero depending on
whether this QByteArrayView sorts before, at the same position as, or after
the QByteArrayView \a bv. The comparison is performed according to case
sensitivity \a cs.
\sa operator==()
*/
/*! /*!
\fn QByteArrayView::const_iterator QByteArrayView::begin() const \fn QByteArrayView::const_iterator QByteArrayView::begin() const

View File

@ -193,6 +193,7 @@ private slots:
} }
void comparison() const; void comparison() const;
void compare() const;
private: private:
template <typename Data> template <typename Data>
@ -635,5 +636,19 @@ void tst_QByteArrayView::comparison() const
QVERIFY(bb > aa); QVERIFY(bb > aa);
} }
void tst_QByteArrayView::compare() const
{
QByteArrayView alpha = "original";
QVERIFY(alpha.compare("original", Qt::CaseSensitive) == 0);
QVERIFY(alpha.compare("Original", Qt::CaseSensitive) > 0);
QVERIFY(alpha.compare("Original", Qt::CaseInsensitive) == 0);
QByteArrayView beta = "unoriginal";
QVERIFY(alpha.compare(beta, Qt::CaseInsensitive) < 0);
beta = "Unoriginal";
QVERIFY(alpha.compare(beta, Qt::CaseInsensitive) < 0);
QVERIFY(alpha.compare(beta, Qt::CaseSensitive) > 0);
}
QTEST_APPLESS_MAIN(tst_QByteArrayView) QTEST_APPLESS_MAIN(tst_QByteArrayView)
#include "tst_qbytearrayview.moc" #include "tst_qbytearrayview.moc"