make QStringList::sort() to take a Qt::CaseSensitivity param
Task-number: QTBUG-12892 Change-Id: I402e6fb12ff24ac26c5a8103bf81547946f9cc58 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@nokia.com> Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
parent
3127f23e3a
commit
1035c93245
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -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()
|
||||
|
@ -44,10 +44,13 @@
|
||||
#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;
|
||||
|
Loading…
Reference in New Issue
Block a user