From cad7100fda1b27ba56c4d9efc6bceba62859dfbc Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Sun, 13 May 2018 21:53:07 -0700 Subject: QByteArray: add compare() with case sensitivity options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Need to do the same for startsWith() and endsWith(). indexOf() is a lot harder. [ChangeLog][QtCore][QByteArray] Added compare(), which takes Qt::CaseSensitivity as one of the parameters. This function is more efficient than using toLower() or toUpper() and then comparing. Change-Id: Ib48364abee9f464c96c6fffd152e69bde4194df7 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Timur Pocheptsov --- .../corelib/tools/qbytearray/tst_qbytearray.cpp | 32 ++++++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp') diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp index ee01a632d0..a6ab49a004 100644 --- a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp @@ -858,15 +858,32 @@ void tst_QByteArray::qstricmp() if ( actual != 0 ) { actual = (actual < 0 ? -1 : 1); } - QCOMPARE(expected, actual); + QCOMPARE(actual, expected); + + actual = str1.toLatin1().compare(str2.toLatin1(), Qt::CaseInsensitive); + if ( actual != 0 ) { + actual = (actual < 0 ? -1 : 1); + } + QCOMPARE(actual, expected); + + actual = str1.toLatin1().compare(str2.toLatin1().constData(), Qt::CaseInsensitive); + if ( actual != 0 ) { + actual = (actual < 0 ? -1 : 1); + } + QCOMPARE(actual, expected); } void tst_QByteArray::qstricmp_singularities() { QCOMPARE(::qstricmp(0, 0), 0); - QVERIFY(::qstricmp(0, "a") != 0); - QVERIFY(::qstricmp("a", 0) != 0); + QVERIFY(::qstricmp(0, "a") < 0); + QVERIFY(::qstricmp("a", 0) > 0); QCOMPARE(::qstricmp("", ""), 0); + QCOMPARE(QByteArray().compare(nullptr, Qt::CaseInsensitive), 0); + QCOMPARE(QByteArray().compare("", Qt::CaseInsensitive), 0); + QVERIFY(QByteArray("a").compare(nullptr, Qt::CaseInsensitive) > 0); + QVERIFY(QByteArray("a").compare("", Qt::CaseInsensitive) > 0); + QVERIFY(QByteArray().compare("a", Qt::CaseInsensitive) < 0); } void tst_QByteArray::qstrnicmp_singularities() @@ -876,6 +893,9 @@ void tst_QByteArray::qstrnicmp_singularities() QVERIFY(::qstrnicmp("a", 0, 123) != 0); QCOMPARE(::qstrnicmp("", "", 123), 0); QCOMPARE(::qstrnicmp("a", "B", 0), 0); + QCOMPARE(QByteArray().compare(QByteArray(), Qt::CaseInsensitive), 0); + QVERIFY(QByteArray().compare(QByteArray("a"), Qt::CaseInsensitive) < 0); + QVERIFY(QByteArray("a").compare(QByteArray(), Qt::CaseInsensitive) > 0); } void tst_QByteArray::chop_data() @@ -1759,6 +1779,12 @@ void tst_QByteArray::compare() const bool isLess = result < 0; const bool isGreater = result > 0; + int cmp = str1.compare(str2); + if (cmp) + cmp = (cmp < 0 ? -1 : 1); + + QCOMPARE(cmp, result); + // basic tests: QCOMPARE(str1 == str2, isEqual); QCOMPARE(str1 < str2, isLess); -- cgit v1.2.3