From fc95d7a73730f67584891471d912dfd7f65a9cc2 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Wed, 10 May 2023 08:53:35 +0200 Subject: [PATCH] Utils: Improve FilePath::sort Remove the conversion to/from QString for sorting. Change-Id: I89921328b6d9e952c802d41998495bd2ffbb9f99 Reviewed-by: hjk --- src/libs/utils/filepath.cpp | 16 ++++--- tests/auto/utils/filepath/tst_filepath.cpp | 52 ++++++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp index 313da1cd7c9..9bf47805964 100644 --- a/src/libs/utils/filepath.cpp +++ b/src/libs/utils/filepath.cpp @@ -1531,11 +1531,17 @@ void FilePath::removeDuplicates(FilePaths &files) void FilePath::sort(FilePaths &files) { - // FIXME: Improve. - // FIXME: This drops the osType information, which is not correct. - QStringList list = transform(files, &FilePath::toString); - list.sort(); - files = FileUtils::toFilePathList(list); + std::sort(files.begin(), files.end(), [](const FilePath &a, const FilePath &b) { + const int scheme = a.scheme().compare(b.scheme()); + if (scheme != 0) + return scheme < 0; + + const int host = a.host().compare(b.host()); + if (host != 0) + return host < 0; + + return a.pathView() < b.pathView(); + }); } void join(QString &left, const QString &right) diff --git a/tests/auto/utils/filepath/tst_filepath.cpp b/tests/auto/utils/filepath/tst_filepath.cpp index 64888937dec..6e11ddb71cd 100644 --- a/tests/auto/utils/filepath/tst_filepath.cpp +++ b/tests/auto/utils/filepath/tst_filepath.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -109,6 +110,9 @@ private slots: void searchInWithFilter(); + void sort(); + void sort_data(); + private: QTemporaryDir tempDir; QString rootPath; @@ -1657,6 +1661,54 @@ void tst_filepath::tmp() } } +void tst_filepath::sort() +{ + QFETCH(QStringList, input); + + FilePaths filePaths = Utils::transform(input, &FilePath::fromString); + QStringList sorted = input; + sorted.sort(); + + FilePath::sort(filePaths); + QStringList sortedPaths = Utils::transform(filePaths, &FilePath::toString); + + QCOMPARE(sortedPaths, sorted); +} + +void tst_filepath::sort_data() +{ + QTest::addColumn("input"); + + QTest::addRow("empty") << QStringList{}; + + QTest::addRow("one") << QStringList{"foo"}; + QTest::addRow("two") << QStringList{"foo", "bar"}; + QTest::addRow("three") << QStringList{"foo", "bar", "baz"}; + + QTest::addRow("one-absolute") << QStringList{"/foo"}; + QTest::addRow("two-absolute") << QStringList{"/foo", "/bar"}; + + QTest::addRow("one-relative") << QStringList{"foo"}; + + QTest::addRow("one-absolute-one-relative") << QStringList{"/foo", "bar"}; + + QTest::addRow("host") << QStringList{"ssh://test/blah", "ssh://gulp/blah", "ssh://zzz/blah"}; + + QTest::addRow("scheme") << QStringList{"ssh://test/blah", + "ssh://gulp/blah", + "ssh://zzz/blah", + "aaa://gulp/blah", + "xyz://test/blah"}; + + QTest::addRow("others") << QStringList{"a://a//a", + "a://b//a", + "a://a//b", + "a://b//b", + "b://b//b"}; + QTest::addRow("others-reversed") + << QStringList{"b://b//b", "a://b//b", "a://a//b", "a://b//a", "a://a//a"}; +} + QTEST_GUILESS_MAIN(tst_filepath) #include "tst_filepath.moc"