summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/tools/qcollator.cpp260
-rw-r--r--src/corelib/tools/qcollator.h2
-rw-r--r--src/corelib/tools/qcollator_icu.cpp164
-rw-r--r--src/corelib/tools/qcollator_mac.cpp179
-rw-r--r--src/corelib/tools/qcollator_p.h122
-rw-r--r--src/corelib/tools/qcollator_posix.cpp148
-rw-r--r--src/corelib/tools/qcollator_win.cpp168
-rw-r--r--src/corelib/tools/tools.pri9
8 files changed, 838 insertions, 214 deletions
diff --git a/src/corelib/tools/qcollator.cpp b/src/corelib/tools/qcollator.cpp
index 28d42f4dca..7d5915a1cb 100644
--- a/src/corelib/tools/qcollator.cpp
+++ b/src/corelib/tools/qcollator.cpp
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -39,74 +40,15 @@
**
****************************************************************************/
-#include "qcollator.h"
+#include "qcollator_p.h"
#include "qstringlist.h"
#include "qstring.h"
-#ifdef QT_USE_ICU
-#include <unicode/utypes.h>
-#include <unicode/ucol.h>
-#include <unicode/ustring.h>
-#include <unicode/ures.h>
-#endif
+#include "qdebug.h"
QT_BEGIN_NAMESPACE
-class QCollatorPrivate
-{
-public:
- QAtomicInt ref;
- QLocale locale;
-
-#ifdef QT_USE_ICU
- UCollator *collator;
-#else
- void *collator;
-#endif
-
- void clear() {
-#ifdef QT_USE_ICU
- if (collator)
- ucol_close(collator);
-#endif
- collator = 0;
- }
-
- QCollatorPrivate()
- : ref(1),
- collator(0)
- {
- }
- ~QCollatorPrivate();
-
- void init();
-
-private:
- Q_DISABLE_COPY(QCollatorPrivate)
-};
-
-class QCollatorSortKeyPrivate : public QSharedData
-{
- friend class QCollator;
-public:
- QCollatorSortKeyPrivate(const QByteArray &key) :
- QSharedData(),
- m_key(key)
- {
- }
-
- QByteArray m_key;
-
-private:
- Q_DISABLE_COPY(QCollatorSortKeyPrivate)
-};
-
-QCollatorPrivate::~QCollatorPrivate()
-{
- clear();
-}
-
/*!
\class QCollator
\inmodule QtCore
@@ -128,13 +70,11 @@ QCollatorPrivate::~QCollatorPrivate()
In addition to the locale and collation strategy, several optional flags can be set that influence
the result of the collation.
-
- QCollator currently depends on Qt being compiled with ICU support enabled.
*/
/*!
- Constructs a QCollator from \a locale. If \a locale is not specified QLocale::default()
- is being used.
+ Constructs a QCollator from \a locale. If \a locale is not specified
+ QLocale::default() will be used.
\sa setLocale()
*/
@@ -177,24 +117,6 @@ QCollator &QCollator::operator=(const QCollator &other)
return *this;
}
-
-/*!
- \internal
- */
-void QCollatorPrivate::init()
-{
-#ifdef QT_USE_ICU
- UErrorCode status = U_ZERO_ERROR;
- QByteArray name = locale.bcp47Name().replace(QLatin1Char('-'), QLatin1Char('_')).toLocal8Bit();
- collator = ucol_open(name.constData(), &status);
- if (U_FAILURE(status))
- qWarning("Could not create collator: %d", status);
-
- // enable normalization by default
- ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
-#endif
-}
-
/*!
\internal
*/
@@ -204,9 +126,11 @@ void QCollator::detach()
QCollatorPrivate *x = new QCollatorPrivate;
x->ref.store(1);
x->locale = d->locale;
+ x->collator = 0;
if (!d->ref.deref())
delete d;
d = x;
+ d->init();
}
}
@@ -215,11 +139,9 @@ void QCollator::detach()
*/
void QCollator::setLocale(const QLocale &locale)
{
- if (d->ref.load() != 1)
- detach();
+ detach();
d->clear();
d->locale = locale;
-
d->init();
}
@@ -232,131 +154,81 @@ QLocale QCollator::locale() const
}
/*!
- Sets the case \a cs of the collator.
+ \fn void QCollator::setCaseSensitivity(Qt::CaseSensitivity sensitivity)
+
+ Sets the case \a sensitivity of the collator.
+
+ \sa caseSensitivity()
*/
-void QCollator::setCaseSensitivity(Qt::CaseSensitivity cs)
-{
- if (d->ref.load() != 1)
- detach();
-
-#ifdef QT_USE_ICU
- UColAttributeValue val = (cs == Qt::CaseSensitive) ? UCOL_UPPER_FIRST : UCOL_OFF;
-
- UErrorCode status = U_ZERO_ERROR;
- ucol_setAttribute(d->collator, UCOL_CASE_FIRST, val, &status);
- if (U_FAILURE(status))
- qWarning("ucol_setAttribute: Case First failed: %d", status);
-#else
- Q_UNUSED(cs);
-#endif
-}
/*!
- Returns case preference of the collator.
+ \fn Qt::CaseSensitivity QCollator::caseSensitivity() const
+
+ Returns case sensitivity of the collator.
+
+ \sa setCaseSensitivity()
*/
-Qt::CaseSensitivity QCollator::caseSensitivity() const
-{
-#ifdef QT_USE_ICU
- UErrorCode status = U_ZERO_ERROR;
- UColAttributeValue attribute = ucol_getAttribute(d->collator, UCOL_CASE_FIRST, &status);
- return (attribute == UCOL_OFF) ? Qt::CaseInsensitive : Qt::CaseSensitive;
-#endif
- return Qt::CaseSensitive;
-}
/*!
+ \fn void QCollator::setNumericMode(bool on)
+
Enables numeric sorting mode when \a on is set to true.
This will enable proper sorting of numeric digits, so that e.g. 100 sorts after 99.
By default this mode is off.
+
+ \sa numericMode()
*/
-void QCollator::setNumericMode(bool on)
-{
- if (d->ref.load() != 1)
- detach();
-
-#ifdef QT_USE_ICU
- UErrorCode status = U_ZERO_ERROR;
- ucol_setAttribute(d->collator, UCOL_NUMERIC_COLLATION, on ? UCOL_ON : UCOL_OFF, &status);
- if (U_FAILURE(status))
- qWarning("ucol_setAttribute: numeric collation failed: %d", status);
-#else
- Q_UNUSED(on);
-#endif
-}
/*!
+ \fn bool QCollator::numericMode() const
+
Returns true if numeric sorting is enabled, false otherwise.
\sa setNumericMode()
*/
-bool QCollator::numericMode() const
-{
-#ifdef QT_USE_ICU
- UErrorCode status;
- if (ucol_getAttribute(d->collator, UCOL_NUMERIC_COLLATION, &status) == UCOL_ON)
- return true;
-#endif
- return false;
-}
/*!
+ \fn void QCollator::setIgnorePunctuation(bool on)
+
If \a on is set to true, punctuation characters and symbols are ignored when determining sort order.
The default is locale dependent.
+
+ \sa ignorePunctuation()
*/
-void QCollator::setIgnorePunctuation(bool on)
-{
- if (d->ref.load() != 1)
- detach();
-
-#ifdef QT_USE_ICU
- UErrorCode status;
- ucol_setAttribute(d->collator, UCOL_ALTERNATE_HANDLING, on ? UCOL_SHIFTED : UCOL_NON_IGNORABLE, &status);
- if (U_FAILURE(status))
- qWarning("ucol_setAttribute: Alternate handling failed: %d", status);
-#else
- Q_UNUSED(on);
-#endif
-}
/*!
+ \fn bool QCollator::ignorePunctuation() const
+
Returns true if punctuation characters and symbols are ignored when determining sort order.
\sa setIgnorePunctuation()
*/
-bool QCollator::ignorePunctuation() const
-{
-#ifdef QT_USE_ICU
- UErrorCode status;
- if (ucol_getAttribute(d->collator, UCOL_ALTERNATE_HANDLING, &status) == UCOL_SHIFTED)
- return true;
-#endif
- return false;
-}
/*!
+ \fn int QCollator::compare(const QString &s1, const QString &s2) const
+
Compares \a s1 with \a s2. Returns -1, 0 or 1 depending on whether \a s1 is
smaller, equal or larger than \a s2.
*/
-int QCollator::compare(const QString &s1, const QString &s2) const
-{
- return compare(s1.constData(), s1.size(), s2.constData(), s2.size());
-}
/*!
+ \fn bool QCollator::operator()(const QString &s1, const QString &s2) const
+ \internal
+*/
+
+/*!
+ \fn int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const
\overload
Compares \a s1 with \a s2. Returns -1, 0 or 1 depending on whether \a s1 is
smaller, equal or larger than \a s2.
*/
-int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const
-{
- return compare(s1.constData(), s1.size(), s2.constData(), s2.size());
-}
/*!
+ \fn int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const
\overload
Compares \a s1 with \a s2. \a len1 and \a len2 specify the length of the
@@ -364,18 +236,10 @@ int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const
Returns -1, 0 or 1 depending on whether \a s1 is smaller, equal or larger than \a s2.
*/
-int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const
-{
-#ifdef QT_USE_ICU
- const UCollationResult result =
- ucol_strcoll(d->collator, (const UChar *)s1, len1, (const UChar *)s2, len2);
- return result;
-#else
- return QString::compare_helper((const QChar *)s1, len1, (const QChar *)s2, len2, Qt::CaseInsensitive);
-#endif
-}
/*!
+ \fn QCollatorSortKey QCollator::sortKey(const QString &string) const
+
Returns a sortKey for \a string.
Creating the sort key is usually somewhat slower, than using the compare()
@@ -383,30 +247,6 @@ int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) con
a whole list of strings), it's usually faster to create the sort keys for each
string and then sort using the keys.
*/
-QCollatorSortKey QCollator::sortKey(const QString &string) const
-{
- QByteArray key;
-#ifdef QT_USE_ICU
- QByteArray result(16 + string.size() + (string.size() >> 2), Qt::Uninitialized);
- int size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
- string.size(), (uint8_t *)result.data(), result.size());
- if (size > result.size()) {
- result.resize(size);
- size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
- string.size(), (uint8_t *)result.data(), result.size());
- }
- result.truncate(size);
- key = result;
-#else
- key = string.toLatin1();
-#endif
- return QCollatorSortKey(new QCollatorSortKeyPrivate(key));
-}
-
-/*!
- \fn bool QCollator::operator()(const QString &s1, const QString &s2) const
- \internal
-*/
/*!
\class QCollatorSortKey
@@ -435,9 +275,9 @@ QCollatorSortKey::QCollatorSortKey(QCollatorSortKeyPrivate *d)
}
/*!
- Constructs a copy of the \a other collator sorting key.
- */
-QCollatorSortKey::QCollatorSortKey(const QCollatorSortKey& other)
+ Constructs a copy of the \a other collator key.
+*/
+QCollatorSortKey::QCollatorSortKey(const QCollatorSortKey &other)
: d(other.d)
{
}
@@ -461,26 +301,22 @@ QCollatorSortKey& QCollatorSortKey::operator=(const QCollatorSortKey &other)
}
/*!
+ \fn bool QCollatorSortKey::operator<(const QCollatorSortKey &otherKey) const
+
According to the QCollator that created the key, returns true if the
key should be sorted before than \a otherKey; otherwise returns false.
\sa compare()
*/
-bool QCollatorSortKey::operator<(const QCollatorSortKey &otherKey) const
-{
- return d->m_key < otherKey.d->m_key;
-}
/*!
+ \fn int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const
+
Compares the key to \a otherKey. Returns a negative value if the key
is less than \a otherKey, 0 if the key is equal to \a otherKey or a
positive value if the key is greater than \a otherKey.
\sa operator<()
*/
-int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const
-{
- return qstrcmp(d->m_key, otherKey.d->m_key);
-}
QT_END_NAMESPACE
diff --git a/src/corelib/tools/qcollator.h b/src/corelib/tools/qcollator.h
index 677092c718..b99fd1c0cc 100644
--- a/src/corelib/tools/qcollator.h
+++ b/src/corelib/tools/qcollator.h
@@ -60,7 +60,7 @@ public:
~QCollatorSortKey();
QCollatorSortKey &operator=(const QCollatorSortKey &other);
#ifdef Q_COMPILER_RVALUE_REFS
- QCollatorSortKey &operator=(QCollatorSortKey &&other)
+ inline QCollatorSortKey &operator=(QCollatorSortKey &&other)
{ qSwap(d, other.d); return *this; }
#endif
diff --git a/src/corelib/tools/qcollator_icu.cpp b/src/corelib/tools/qcollator_icu.cpp
new file mode 100644
index 0000000000..46f830a34c
--- /dev/null
+++ b/src/corelib/tools/qcollator_icu.cpp
@@ -0,0 +1,164 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qcollator_p.h"
+#include "qstringlist.h"
+#include "qstring.h"
+
+#include <unicode/utypes.h>
+#include <unicode/ucol.h>
+#include <unicode/ustring.h>
+#include <unicode/ures.h>
+
+#include "qdebug.h"
+
+QT_BEGIN_NAMESPACE
+
+void QCollatorPrivate::init()
+{
+ UErrorCode status = U_ZERO_ERROR;
+ QByteArray name = locale.bcp47Name().replace(QLatin1Char('-'), QLatin1Char('_')).toLatin1();
+ collator = ucol_open(name.constData(), &status);
+ if (U_FAILURE(status))
+ qWarning("Could not create collator: %d", status);
+
+ // enable normalization by default
+ ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
+}
+
+void QCollatorPrivate::cleanup()
+{
+ if (collator)
+ ucol_close(collator);
+}
+
+void QCollator::setCaseSensitivity(Qt::CaseSensitivity cs)
+{
+ detach();
+
+ UColAttributeValue val = (cs == Qt::CaseSensitive) ? UCOL_UPPER_FIRST : UCOL_OFF;
+
+ UErrorCode status = U_ZERO_ERROR;
+ ucol_setAttribute(d->collator, UCOL_CASE_FIRST, val, &status);
+ if (U_FAILURE(status))
+ qWarning("ucol_setAttribute: Case First failed: %d", status);
+}
+
+Qt::CaseSensitivity QCollator::caseSensitivity() const
+{
+ UErrorCode status = U_ZERO_ERROR;
+ UColAttributeValue attribute = ucol_getAttribute(d->collator, UCOL_CASE_FIRST, &status);
+ return (attribute == UCOL_OFF) ? Qt::CaseInsensitive : Qt::CaseSensitive;
+}
+
+void QCollator::setNumericMode(bool on)
+{
+ detach();
+
+ UErrorCode status = U_ZERO_ERROR;
+ ucol_setAttribute(d->collator, UCOL_NUMERIC_COLLATION, on ? UCOL_ON : UCOL_OFF, &status);
+ if (U_FAILURE(status))
+ qWarning("ucol_setAttribute: numeric collation failed: %d", status);
+}
+
+bool QCollator::numericMode() const
+{
+ UErrorCode status;
+ return ucol_getAttribute(d->collator, UCOL_NUMERIC_COLLATION, &status) == UCOL_ON;
+}
+
+void QCollator::setIgnorePunctuation(bool on)
+{
+ detach();
+
+ UErrorCode status;
+ ucol_setAttribute(d->collator, UCOL_ALTERNATE_HANDLING, on ? UCOL_SHIFTED : UCOL_NON_IGNORABLE, &status);
+ if (U_FAILURE(status))
+ qWarning("ucol_setAttribute: Alternate handling failed: %d", status);
+}
+
+bool QCollator::ignorePunctuation() const
+{
+ UErrorCode status;
+ return ucol_getAttribute(d->collator, UCOL_ALTERNATE_HANDLING, &status) == UCOL_SHIFTED;
+}
+
+int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const
+{
+ return ucol_strcoll(d->collator, (const UChar *)s1, len1, (const UChar *)s2, len2);
+}
+
+int QCollator::compare(const QString &s1, const QString &s2) const
+{
+ return compare(s1.constData(), s1.size(), s2.constData(), s2.size());
+}
+
+int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const
+{
+ return compare(s1.constData(), s1.size(), s2.constData(), s2.size());
+}
+
+QCollatorSortKey QCollator::sortKey(const QString &string) const
+{
+ QByteArray result(16 + string.size() + (string.size() >> 2), Qt::Uninitialized);
+ int size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
+ string.size(), (uint8_t *)result.data(), result.size());
+ if (size > result.size()) {
+ result.resize(size);
+ size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
+ string.size(), (uint8_t *)result.data(), result.size());
+ }
+ result.truncate(size);
+ return QCollatorSortKey(new QCollatorSortKeyPrivate(result));
+}
+
+bool QCollatorSortKey::operator<(const QCollatorSortKey &otherKey) const
+{
+ return d->m_key < otherKey.d->m_key;
+}
+
+int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const
+{
+ return qstrcmp(d->m_key, otherKey.d->m_key);
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/tools/qcollator_mac.cpp b/src/corelib/tools/qcollator_mac.cpp
new file mode 100644
index 0000000000..8edd190fbe
--- /dev/null
+++ b/src/corelib/tools/qcollator_mac.cpp
@@ -0,0 +1,179 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qcollator_p.h"
+#include "qstringlist.h"
+#include "qstring.h"
+#include <QtCore/private/qcore_mac_p.h>
+#include <CoreFoundation/CoreFoundation.h>
+#include <CoreFoundation/CFLocale.h>
+
+#include <cstring>
+#include <QDebug>
+
+QT_BEGIN_NAMESPACE
+
+void QCollatorPrivate::init()
+{
+ cleanup();
+ LocaleRef localeRef;
+ int rc = LocaleRefFromLocaleString(locale.name().toLocal8Bit(), &localeRef);
+ if (rc != 0)
+ qWarning() << "couldn't initialize the locale";
+
+ OSStatus status = UCCreateCollator(
+ localeRef,
+ 0,
+ collator.options,
+ &collator.collator
+ );
+ if (status != 0)
+ qWarning() << "Couldn't initialize the collator";
+}
+
+void QCollatorPrivate::cleanup()
+{
+ UCDisposeCollator(&collator.collator);
+ collator.collator = 0;
+}
+
+void QCollator::setCaseSensitivity(Qt::CaseSensitivity cs)
+{
+ detach();
+
+ if (cs == Qt::CaseSensitive)
+ d->collator.options &= ~kUCCollateCaseInsensitiveMask;
+ else
+ d->collator.options |= kUCCollateCaseInsensitiveMask;
+ d->init();
+}
+
+Qt::CaseSensitivity QCollator::caseSensitivity() const
+{
+ return !(d->collator.options & kUCCollateCaseInsensitiveMask) ? Qt::CaseInsensitive : Qt::CaseSensitive;
+}
+
+void QCollator::setNumericMode(bool on)
+{
+ detach();
+
+ if (on)
+ d->collator.options |= kUCCollateDigitsAsNumberMask;
+ else
+ d->collator.options &= ~kUCCollateDigitsAsNumberMask;
+
+ d->init();
+}
+
+bool QCollator::numericMode() const
+{
+ return bool(d->collator.options & kUCCollateDigitsAsNumberMask);
+}
+
+void QCollator::setIgnorePunctuation(bool on)
+{
+ detach();
+
+ if (on)
+ d->collator.options |= kUCCollatePunctuationSignificantMask;
+ else
+ d->collator.options &= ~kUCCollatePunctuationSignificantMask;
+
+ d->init();
+}
+
+bool QCollator::ignorePunctuation() const
+{
+ return bool(d->collator.options & kUCCollatePunctuationSignificantMask);
+}
+
+int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const
+{
+ SInt32 result;
+ return UCCompareText(d->collator.collator,
+ reinterpret_cast<const UniChar *>(s1), len1,
+ reinterpret_cast<const UniChar *>(s2), len2,
+ NULL,
+ &result);
+ return result;
+}
+int QCollator::compare(const QString &str1, const QString &str2) const
+{
+ return compare(str1.constData(), str1.size(), str2.constData(), str2.size());
+}
+
+int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const
+{
+ return compare(s1.constData(), s1.size(), s2.constData(), s2.size());
+}
+
+QCollatorSortKey QCollator::sortKey(const QString &string) const
+{
+ //Documentation recommends having it 5 times as big as the input
+ QVector<UCCollationValue> ret(string.size() * 5);
+ ItemCount actualSize;
+ int status = UCGetCollationKey(d->collator.collator, reinterpret_cast<const UniChar *>(string.constData()), string.count(),
+ ret.size(), &actualSize, ret.data());
+
+ ret.resize(actualSize+1);
+ if (status == kUCOutputBufferTooSmall) {
+ UCGetCollationKey(d->collator.collator, reinterpret_cast<const UniChar *>(string.constData()), string.count(),
+ ret.size(), &actualSize, ret.data());
+ }
+ ret[actualSize] = 0;
+ return QCollatorSortKey(new QCollatorSortKeyPrivate(ret));
+}
+
+bool QCollatorSortKey::operator<(const QCollatorSortKey &key) const
+{
+ return compare(key) < 0;
+}
+
+int QCollatorSortKey::compare(const QCollatorSortKey &key) const
+{
+ SInt32 order;
+ UCCompareCollationKeys(d->m_key.data(), d->m_key.size(),
+ key.d->m_key.data(), key.d->m_key.size(),
+ 0, &order);
+ return order;
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/tools/qcollator_p.h b/src/corelib/tools/qcollator_p.h
new file mode 100644
index 0000000000..7c5f18c5b2
--- /dev/null
+++ b/src/corelib/tools/qcollator_p.h
@@ -0,0 +1,122 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QCOLLATOR_P_H
+#define QCOLLATOR_P_H
+
+#include "qcollator.h"
+#include <QVector>
+#ifdef QT_USE_ICU
+#include <unicode/ucol.h>
+#elif defined(Q_OS_DARWIN)
+#include <CoreServices/CoreServices.h>
+#endif
+
+QT_BEGIN_NAMESPACE
+
+#ifdef QT_USE_ICU
+typedef UCollator *CollatorType;
+typedef QByteArray CollatorKeyType;
+
+#elif defined(Q_OS_DARWIN)
+typedef QVector<UCCollationValue> CollatorKeyType;
+
+struct CollatorType {
+ CollatorType(int opt) : collator(NULL), options(opt) {}
+
+ CollatorRef collator;
+ UInt32 options;
+};
+#elif defined(Q_OS_WIN)
+typedef QString CollatorKeyType;
+typedef int CollatorType;
+#else //posix
+typedef QVector<wchar_t> CollatorKeyType;
+typedef int CollatorType;
+#endif
+
+class Q_CORE_EXPORT QCollatorPrivate
+{
+public:
+ QAtomicInt ref;
+ QLocale locale;
+
+ CollatorType collator;
+
+ void clear() {
+ cleanup();
+ collator = 0;
+ }
+
+ void init();
+ void cleanup();
+
+ QCollatorPrivate()
+ : ref(1), collator(0)
+ { cleanup(); }
+
+ ~QCollatorPrivate() { cleanup(); }
+
+private:
+ Q_DISABLE_COPY(QCollatorPrivate)
+};
+
+class Q_CORE_EXPORT QCollatorSortKeyPrivate : public QSharedData
+{
+ friend class QCollator;
+public:
+ QCollatorSortKeyPrivate(const CollatorKeyType &key)
+ : QSharedData()
+ , m_key(key)
+ {
+ }
+
+ CollatorKeyType m_key;
+
+private:
+ Q_DISABLE_COPY(QCollatorSortKeyPrivate)
+};
+
+
+QT_END_NAMESPACE
+
+#endif // QCOLLATOR_P_H
diff --git a/src/corelib/tools/qcollator_posix.cpp b/src/corelib/tools/qcollator_posix.cpp
new file mode 100644
index 0000000000..c6e589c174
--- /dev/null
+++ b/src/corelib/tools/qcollator_posix.cpp
@@ -0,0 +1,148 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qcollator_p.h"
+#include "qstringlist.h"
+#include "qstring.h"
+
+#include <cstring>
+
+QT_BEGIN_NAMESPACE
+
+void QCollatorPrivate::init()
+{
+}
+
+void QCollatorPrivate::cleanup()
+{
+}
+
+void QCollator::setCaseSensitivity(Qt::CaseSensitivity cs)
+{
+ Q_UNUSED(cs);
+ qWarning("unsupported in the posix collation implementation");
+}
+
+Qt::CaseSensitivity QCollator::caseSensitivity() const
+{
+ qWarning("unsupported in the posix collation implementation");
+ return Qt::CaseSensitive;
+}
+
+void QCollator::setNumericMode(bool on)
+{
+ Q_UNUSED(on);
+ qWarning("unsupported in the posix collation implementation");
+}
+
+bool QCollator::numericMode() const
+{
+ return true;
+}
+
+void QCollator::setIgnorePunctuation(bool on)
+{
+ Q_UNUSED(on);
+ qWarning("unsupported in the posix collation implementation");
+}
+
+bool QCollator::ignorePunctuation() const
+{
+ qWarning("unsupported in the posix collation implementation");
+ return false;
+}
+
+static void stringToWCharArray(QVarLengthArray<wchar_t> &ret, const QString &string)
+{
+ ret.resize(string.length());
+ int len = string.toWCharArray(ret.data());
+ ret.resize(len+1);
+ ret[len] = 0;
+}
+
+int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const
+{
+ QVarLengthArray<wchar_t> array1, array2;
+ stringToWCharArray(array1, QString(s1, len1));
+ stringToWCharArray(array2, QString(s2, len2));
+ return std::wcscoll(array1.constData(), array2.constData());
+}
+
+int QCollator::compare(const QString &s1, const QString &s2) const
+{
+ QVarLengthArray<wchar_t> array1, array2;
+ stringToWCharArray(array1, s1);
+ stringToWCharArray(array2, s2);
+ return std::wcscoll(array1.constData(), array2.constData());
+}
+
+int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const
+{
+ return compare(s1.constData(), s1.size(), s2.constData(), s2.size());
+}
+
+QCollatorSortKey QCollator::sortKey(const QString &string) const
+{
+ QVarLengthArray<wchar_t> original;
+ stringToWCharArray(original, string);
+ QVector<wchar_t> result(string.size());
+ size_t size = std::wcsxfrm(result.data(), original.constData(), string.size());
+ if (size > uint(result.size())) {
+ result.resize(size+1);
+ size = std::wcsxfrm(result.data(), original.constData(), string.size());
+ }
+ result.resize(size+1);
+ result[size] = 0;
+ return QCollatorSortKey(new QCollatorSortKeyPrivate(result));
+}
+
+bool QCollatorSortKey::operator<(const QCollatorSortKey &otherKey) const
+{
+ return compare(otherKey) < 0;
+}
+
+int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const
+{
+ return wcscmp(d->m_key.constData(),
+ otherKey.d->m_key.constData());
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/tools/qcollator_win.cpp b/src/corelib/tools/qcollator_win.cpp
new file mode 100644
index 0000000000..282711fbc6
--- /dev/null
+++ b/src/corelib/tools/qcollator_win.cpp
@@ -0,0 +1,168 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qcollator_p.h"
+#include "qstringlist.h"
+#include "qstring.h"
+
+#include <QDebug>
+
+#include <qt_windows.h>
+#include <qsysinfo.h>
+
+QT_BEGIN_NAMESPACE
+
+void QCollatorPrivate::init()
+{
+ collator = 0;
+}
+
+void QCollatorPrivate::cleanup()
+{
+}
+
+void QCollator::setCaseSensitivity(Qt::CaseSensitivity cs)
+{
+ detach();
+
+ if (cs == Qt::CaseSensitive)
+ d->collator &= ~NORM_IGNORECASE;
+ else
+ d->collator |= NORM_IGNORECASE;
+}
+
+Qt::CaseSensitivity QCollator::caseSensitivity() const
+{
+ return d->collator & NORM_IGNORECASE ? Qt::CaseInsensitive : Qt::CaseSensitive;
+}
+
+//NOTE: SORT_DIGITSASNUMBERS is available since win7
+#ifndef SORT_DIGITSASNUMBERS
+#define SORT_DIGITSASNUMBERS 8
+#endif
+void QCollator::setNumericMode(bool on)
+{
+ if (QSysInfo::windowsVersion() >= QSysInfo::WV_WINDOWS7) {
+ detach();
+
+ if (on)
+ d->collator |= SORT_DIGITSASNUMBERS;
+ else
+ d->collator &= ~SORT_DIGITSASNUMBERS;
+ } else {
+ Q_UNUSED(on);
+ qWarning() << "unsupported in the win collation implementation";
+ }
+}
+
+bool QCollator::numericMode() const
+{
+ if (QSysInfo::windowsVersion() >= QSysInfo::WV_WINDOWS7) {
+ return bool(d->collator & SORT_DIGITSASNUMBERS);
+ } else {
+ qWarning() << "unsupported in the win collation implementation";
+ return false;
+ }
+}
+
+void QCollator::setIgnorePunctuation(bool on)
+{
+ detach();
+
+ if (on)
+ d->collator |= NORM_IGNORESYMBOLS;
+ else
+ d->collator &= ~NORM_IGNORESYMBOLS;
+}
+
+bool QCollator::ignorePunctuation() const
+{
+ return bool(d->collator & NORM_IGNORESYMBOLS);
+}
+
+int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const
+{
+ //* from Windows documentation *
+ // Returns one of the following values if successful. To maintain the C runtime convention of
+ // comparing strings, the value 2 can be subtracted from a nonzero return value. Then, the
+ // meaning of <0, ==0, and >0 is consistent with the C runtime.
+
+ return CompareString(LOCALE_USER_DEFAULT, d->collator,
+ reinterpret_cast<const wchar_t*>(s1), len1,
+ reinterpret_cast<const wchar_t*>(s2), len2) - 2;
+}
+
+int QCollator::compare(const QString &str1, const QString &str2) const
+{
+ return compare(str1.constData(), str1.size(), str2.constData(), str2.size());
+}
+
+int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const
+{
+ return compare(s1.constData(), s1.size(), s2.constData(), s2.size());
+}
+
+QCollatorSortKey QCollator::sortKey(const QString &string) const
+{
+ int size = LCMapStringW(LOCALE_USER_DEFAULT, LCMAP_SORTKEY | d->collator,
+ reinterpret_cast<const wchar_t*>(string.constData()), string.size(),
+ 0, 0);
+ QString ret(size, Qt::Uninitialized);
+ int finalSize = LCMapStringW(LOCALE_USER_DEFAULT, LCMAP_SORTKEY | d->collator,
+ reinterpret_cast<const wchar_t*>(string.constData()), string.size(),
+ reinterpret_cast<wchar_t*>(ret.data()), ret.size());
+ if (finalSize == 0) {
+ qWarning() << "there were problems when generating the ::sortKey by LCMapStringW with error:" << GetLastError();
+ }
+ return QCollatorSortKey(new QCollatorSortKeyPrivate(ret));
+}
+
+bool QCollatorSortKey::operator<(const QCollatorSortKey &otherKey) const
+{
+ return d->m_key < otherKey.d->m_key;
+}
+
+int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const
+{
+ return d->m_key.compare(otherKey.d->m_key);
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/tools/tools.pri b/src/corelib/tools/tools.pri
index d3f87977fc..01f16dc2ad 100644
--- a/src/corelib/tools/tools.pri
+++ b/src/corelib/tools/tools.pri
@@ -132,7 +132,8 @@ contains(QT_CONFIG, zlib) {
}
contains(QT_CONFIG,icu) {
- SOURCES += tools/qlocale_icu.cpp
+ SOURCES += tools/qlocale_icu.cpp \
+ tools/qcollator_icu.cpp
DEFINES += QT_USE_ICU
win32 {
CONFIG(static, static|shared) {
@@ -147,6 +148,12 @@ contains(QT_CONFIG,icu) {
} else {
LIBS_PRIVATE += -licui18n -licuuc
}
+} else: win32 {
+ SOURCES += tools/qcollator_win.cpp
+} else: mac {
+ SOURCES += tools/qcollator_mac.cpp
+} else {
+ SOURCES += tools/qcollator_posix.cpp
}
pcre {