make QStringList::sort() to take a Qt::CaseSensitivity param
authorKonstantin Ritt <ritt.ks@gmail.com>
Wed, 11 Apr 2012 15:06:11 +0000 (18:06 +0300)
committerQt by Nokia <qt-info@nokia.com>
Thu, 12 Apr 2012 08:39:22 +0000 (10:39 +0200)
Task-number: QTBUG-12892

Change-Id: I402e6fb12ff24ac26c5a8103bf81547946f9cc58
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@nokia.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
src/corelib/tools/qstringlist.cpp
src/corelib/tools/qstringlist.h
tests/auto/corelib/tools/qstringlist/tst_qstringlist.cpp

index 50e155d..bfe2c5e 100644 (file)
@@ -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)
+{
+   return s1.compare(s2, Qt::CaseInsensitive) < 0;
+}
+
+void QtPrivate::QStringList_sort(QStringList *that, Qt::CaseSensitivity cs)
 {
-    qSort(*that);
+    if (cs == Qt::CaseSensitive)
+        qSort(that->begin(), that->end());
+    else
+        qSort(that->begin(), that->end(), caseInsensitiveLessThan);
 }
 
 
index bf9c2e1..3e63e30 100644 (file)
@@ -71,7 +71,7 @@ public:
     inline QStringList(std::initializer_list<QString> args) : QList<QString>(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()
index d02e649..16a329f 100644 (file)
 #include <qregularexpression.h>
 #include <qstringlist.h>
 
+#include <locale.h>
+
 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;