diff options
author | Mårten Nordheim <marten.nordheim@qt.io> | 2021-04-29 12:36:12 +0200 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2021-05-20 17:02:38 +0200 |
commit | a5dc381b4c987d933f1207035e275cdc24669de6 (patch) | |
tree | 2c79e8c6e1488b3df246e1c7720c831ae8620a85 | |
parent | e8297ba17602475e0fc94e3a67a625dc5bc8c136 (diff) |
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>
-rw-r--r-- | src/corelib/text/qbytearrayview.h | 8 | ||||
-rw-r--r-- | src/corelib/text/qbytearrayview.qdoc | 12 | ||||
-rw-r--r-- | tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp | 15 |
3 files changed, 35 insertions, 0 deletions
diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index 367fdb58b2..9d35e21415 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -270,6 +270,8 @@ public: [[nodiscard]] qsizetype count(char ch) const noexcept { return QtPrivate::count(*this, QByteArrayView(&ch, 1)); } + inline int compare(QByteArrayView a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + // // STL compatibility API: // @@ -320,6 +322,12 @@ template<typename QByteArrayLike, [[nodiscard]] inline QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept { 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) QT_DEPRECATED_VERSION_X_6_0("Use the QByteArrayView overload.") inline quint16 qChecksum(const char *s, qsizetype len, diff --git a/src/corelib/text/qbytearrayview.qdoc b/src/corelib/text/qbytearrayview.qdoc index 91048c5dad..30d8d47685 100644 --- a/src/corelib/text/qbytearrayview.qdoc +++ b/src/corelib/text/qbytearrayview.qdoc @@ -354,6 +354,18 @@ */ /*! + \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 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the diff --git a/tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp b/tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp index 35f883068d..d65731fdbd 100644 --- a/tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp +++ b/tests/auto/corelib/text/qbytearrayview/tst_qbytearrayview.cpp @@ -193,6 +193,7 @@ private slots: } void comparison() const; + void compare() const; private: template <typename Data> @@ -635,5 +636,19 @@ void tst_QByteArrayView::comparison() const 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) #include "tst_qbytearrayview.moc" |