From a5dc381b4c987d933f1207035e275cdc24669de6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Thu, 29 Apr 2021 12:36:12 +0200 Subject: [PATCH] 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 --- src/corelib/text/qbytearrayview.h | 8 ++++++++ src/corelib/text/qbytearrayview.qdoc | 12 ++++++++++++ .../text/qbytearrayview/tst_qbytearrayview.cpp | 15 +++++++++++++++ 3 files changed, 35 insertions(+) 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 @@ -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"