From 1035c93245ef299c2f29be5b119b736430e81dc3 Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Wed, 11 Apr 2012 18:06:11 +0300 Subject: [PATCH] make QStringList::sort() to take a Qt::CaseSensitivity param Task-number: QTBUG-12892 Change-Id: I402e6fb12ff24ac26c5a8103bf81547946f9cc58 Reviewed-by: Oswald Buddenhagen Reviewed-by: Lars Knoll --- src/corelib/tools/qstringlist.cpp | 19 ++++++++++++++---- src/corelib/tools/qstringlist.h | 8 ++++---- .../tools/qstringlist/tst_qstringlist.cpp | 20 +++++++++++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/corelib/tools/qstringlist.cpp b/src/corelib/tools/qstringlist.cpp index 50e155db81..bfe2c5ec2d 100644 --- a/src/corelib/tools/qstringlist.cpp +++ b/src/corelib/tools/qstringlist.cpp @@ -213,9 +213,11 @@ QT_BEGIN_NAMESPACE */ /*! - \fn void QStringList::sort() + \fn void QStringList::sort(Qt::CaseSensitivity cs) - Sorts the list of strings in ascending order (case sensitively). + Sorts the list of strings in ascending order. + If \a cs is \l Qt::CaseSensitive (the default), the string comparison + is case sensitive; otherwise the comparison is case insensitive. Sorting is performed using Qt's qSort() algorithm, which operates in \l{linear-logarithmic time}, i.e. O(\e{n} log \e{n}). @@ -229,9 +231,18 @@ QT_BEGIN_NAMESPACE \sa qSort() */ -void QtPrivate::QStringList_sort(QStringList *that) + +static inline bool caseInsensitiveLessThan(const QString &s1, const QString &s2) { - qSort(*that); + return s1.compare(s2, Qt::CaseInsensitive) < 0; +} + +void QtPrivate::QStringList_sort(QStringList *that, Qt::CaseSensitivity cs) +{ + if (cs == Qt::CaseSensitive) + qSort(that->begin(), that->end()); + else + qSort(that->begin(), that->end(), caseInsensitiveLessThan); } diff --git a/src/corelib/tools/qstringlist.h b/src/corelib/tools/qstringlist.h index bf9c2e14bb..3e63e30a57 100644 --- a/src/corelib/tools/qstringlist.h +++ b/src/corelib/tools/qstringlist.h @@ -71,7 +71,7 @@ public: inline QStringList(std::initializer_list args) : QList(args) { } #endif - inline void sort(); + inline void sort(Qt::CaseSensitivity cs = Qt::CaseSensitive); inline int removeDuplicates(); inline QString join(const QString &sep) const; @@ -120,7 +120,7 @@ public: Q_DECLARE_TYPEINFO(QStringList, Q_MOVABLE_TYPE); namespace QtPrivate { - void Q_CORE_EXPORT QStringList_sort(QStringList *that); + void Q_CORE_EXPORT QStringList_sort(QStringList *that, Qt::CaseSensitivity cs); int Q_CORE_EXPORT QStringList_removeDuplicates(QStringList *that); QString Q_CORE_EXPORT QStringList_join(const QStringList *that, const QString &sep); QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, const QString &str, @@ -149,9 +149,9 @@ namespace QtPrivate { #endif // QT_BOOTSTRAPPED } -inline void QStringList::sort() +inline void QStringList::sort(Qt::CaseSensitivity cs) { - QtPrivate::QStringList_sort(this); + QtPrivate::QStringList_sort(this, cs); } inline int QStringList::removeDuplicates() diff --git a/tests/auto/corelib/tools/qstringlist/tst_qstringlist.cpp b/tests/auto/corelib/tools/qstringlist/tst_qstringlist.cpp index d02e649bdf..16a329f1dd 100644 --- a/tests/auto/corelib/tools/qstringlist/tst_qstringlist.cpp +++ b/tests/auto/corelib/tools/qstringlist/tst_qstringlist.cpp @@ -44,10 +44,13 @@ #include #include +#include + class tst_QStringList : public QObject { Q_OBJECT private slots: + void sort(); void filter(); void replaceInStrings(); void removeDuplicates(); @@ -199,6 +202,23 @@ void tst_QStringList::filter() QCOMPARE( list5, list6 ); } +void tst_QStringList::sort() +{ + QStringList list1, list2; + list1 << "alpha" << "beta" << "BETA" << "gamma" << "Gamma" << "gAmma" << "epsilon"; + list1.sort(); + list2 << "BETA" << "Gamma" << "alpha" << "beta" << "epsilon" << "gAmma" << "gamma"; + QCOMPARE( list1, list2 ); + + char *current_locale = setlocale(LC_ALL, "C"); + QStringList list3, list4; + list3 << "alpha" << "beta" << "BETA" << "gamma" << "Gamma" << "gAmma" << "epsilon"; + list3.sort(Qt::CaseInsensitive); + list4 << "alpha" << "beta" << "BETA" << "epsilon" << "Gamma" << "gAmma" << "gamma"; + QCOMPARE( list3, list4 ); + setlocale(LC_ALL, current_locale); +} + void tst_QStringList::replaceInStrings() { QStringList list1, list2;