From 549ed4fb24f510cc268552ce365ecc812f9be2ba Mon Sep 17 00:00:00 2001 From: David Faure Date: Tue, 12 Sep 2023 19:48:00 +0200 Subject: [PATCH] QMimeType: simplify implementation of filterString As suggested by Ahmad Samir Change-Id: I7f3ef29436a7725b6dceab72bd0f8b01756bf930 Reviewed-by: Thiago Macieira --- src/corelib/mimetypes/qmimetype.cpp | 10 ++-------- .../qmimedatabase/tst_qmimedatabase.cpp | 20 +++++++++++++++++++ .../qmimedatabase/tst_qmimedatabase.h | 2 ++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/corelib/mimetypes/qmimetype.cpp b/src/corelib/mimetypes/qmimetype.cpp index 543fbeb28c..eab00a58ae 100644 --- a/src/corelib/mimetypes/qmimetype.cpp +++ b/src/corelib/mimetypes/qmimetype.cpp @@ -437,14 +437,8 @@ QString QMimeType::filterString() const const QStringList patterns = globPatterns(); QString filter; - if (!patterns.empty()) { - filter += comment() + " ("_L1; - for (int i = 0; i < patterns.size(); ++i) { - if (i != 0) - filter += u' '; - filter += patterns.at(i); - } - filter += u')'; + if (!patterns.isEmpty()) { + filter = comment() + " ("_L1 + patterns.join(u' ') + u')'; } return filter; diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp index 2072f41494..d16fc7a1ea 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -782,6 +782,26 @@ void tst_QMimeDatabase::knownSuffix() QCOMPARE(db.suffixForFileName(QString::fromLatin1("foo.anim2")), QString()); // the glob is anim[0-9], no way to extract the extension without expensive regexp capturing } +void tst_QMimeDatabase::filterString_data() +{ + QTest::addColumn("mimeType"); + QTest::addColumn("expectedFilterString"); + + QTest::newRow("single-pattern") << "application/pdf" + << "PDF document (*.pdf)"; + QTest::newRow("multiple-patterns-text-plain") << "text/plain" + << "plain text document (*.txt *.asc *,v)"; +} + +void tst_QMimeDatabase::filterString() +{ + QFETCH(QString, mimeType); + QFETCH(QString, expectedFilterString); + + QMimeDatabase db; + QCOMPARE(db.mimeTypeForName(mimeType).filterString(), expectedFilterString); +} + void tst_QMimeDatabase::symlinkToFifo() // QTBUG-48529 { #if defined(Q_OS_UNIX) && !defined(Q_OS_INTEGRITY) diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h index c08179bb2f..575a0cc9d4 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h @@ -45,6 +45,8 @@ private slots: void suffixes_data(); void suffixes(); void knownSuffix(); + void filterString_data(); + void filterString(); void symlinkToFifo(); void fromThreads();