summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcus Tillmanns <marcus.tillmanns@qt.io>2023-05-10 08:53:35 +0200
committerMarcus Tillmanns <marcus.tillmanns@qt.io>2023-05-10 08:56:32 +0000
commitfc95d7a73730f67584891471d912dfd7f65a9cc2 (patch)
treed785884ccea11b7320457f06034b474d4f78a6c4
parent2c433f7fe1b47b3fd497f87e51e248c70323a3f5 (diff)
downloadqt-creator-fc95d7a73730f67584891471d912dfd7f65a9cc2.tar.gz
Utils: Improve FilePath::sort
Remove the conversion to/from QString for sorting. Change-Id: I89921328b6d9e952c802d41998495bd2ffbb9f99 Reviewed-by: hjk <hjk@qt.io>
-rw-r--r--src/libs/utils/filepath.cpp16
-rw-r--r--tests/auto/utils/filepath/tst_filepath.cpp52
2 files changed, 63 insertions, 5 deletions
diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp
index 313da1cd7c..9bf4780596 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<QStringList>(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 64888937de..6e11ddb71c 100644
--- a/tests/auto/utils/filepath/tst_filepath.cpp
+++ b/tests/auto/utils/filepath/tst_filepath.cpp
@@ -4,6 +4,7 @@
#include <QRandomGenerator>
#include <QtTest>
+#include <utils/algorithm.h>
#include <utils/filepath.h>
#include <utils/hostosinfo.h>
#include <utils/link.h>
@@ -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<QStringList>("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"