1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "qcollator_p.h"
42#include "qlocale_p.h"
43#include "qstringlist.h"
44#include "qstring.h"
45
46#include <unicode/utypes.h>
47#include <unicode/ucol.h>
48#include <unicode/ustring.h>
49#include <unicode/ures.h>
50
51#include "qdebug.h"
52
53QT_BEGIN_NAMESPACE
54
55void QCollatorPrivate::init()
56{
57 cleanup();
58 if (isC())
59 return;
60
61 UErrorCode status = U_ZERO_ERROR;
62 QByteArray name = QLocalePrivate::get(locale)->bcp47Name('_');
63 collator = ucol_open(name.constData(), &status);
64 if (U_FAILURE(status)) {
65 qWarning("Could not create collator: %d", status);
66 collator = nullptr;
67 dirty = false;
68 return;
69 }
70
71 // enable normalization by default
72 ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
73
74 // The strength attribute in ICU is rather badly documented. Basically UCOL_PRIMARY
75 // ignores differences between base characters and accented characters as well as case.
76 // So A and A-umlaut would compare equal.
77 // UCOL_SECONDARY ignores case differences. UCOL_TERTIARY is the default in most languages
78 // and does case sensitive comparison.
79 // UCOL_QUATERNARY is used as default in a few languages such as Japanese to take care of some
80 // additional differences in those languages.
81 UColAttributeValue val = (caseSensitivity == Qt::CaseSensitive)
82 ? UCOL_DEFAULT_STRENGTH : UCOL_SECONDARY;
83
84 status = U_ZERO_ERROR;
85 ucol_setAttribute(collator, UCOL_STRENGTH, val, &status);
86 if (U_FAILURE(status))
87 qWarning("ucol_setAttribute: Case First failed: %d", status);
88
89 status = U_ZERO_ERROR;
90 ucol_setAttribute(collator, UCOL_NUMERIC_COLLATION, numericMode ? UCOL_ON : UCOL_OFF, &status);
91 if (U_FAILURE(status))
92 qWarning("ucol_setAttribute: numeric collation failed: %d", status);
93
94 status = U_ZERO_ERROR;
95 ucol_setAttribute(collator, UCOL_ALTERNATE_HANDLING,
96 ignorePunctuation ? UCOL_SHIFTED : UCOL_NON_IGNORABLE, &status);
97 if (U_FAILURE(status))
98 qWarning("ucol_setAttribute: Alternate handling failed: %d", status);
99
100 dirty = false;
101}
102
103void QCollatorPrivate::cleanup()
104{
105 if (collator)
106 ucol_close(collator);
107 collator = nullptr;
108}
109
110int QCollator::compare(QStringView s1, QStringView s2) const
111{
112 if (d->dirty)
113 d->init();
114
115 if (d->collator) {
116 return ucol_strcoll(d->collator,
117 reinterpret_cast<const UChar *>(s1.data()), s1.size(),
118 reinterpret_cast<const UChar *>(s2.data()), s2.size());
119 }
120
121 return QString::compare_helper(s1.data(), s1.size(),
122 s2.data(), s2.size(),
123 d->caseSensitivity);
124}
125
126QCollatorSortKey QCollator::sortKey(const QString &string) const
127{
128 if (d->dirty)
129 d->init();
130 if (d->isC())
131 return QCollatorSortKey(new QCollatorSortKeyPrivate(string.toUtf8()));
132
133 if (d->collator) {
134 QByteArray result(16 + string.size() + (string.size() >> 2), Qt::Uninitialized);
135 int size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
136 string.size(), (uint8_t *)result.data(), result.size());
137 if (size > result.size()) {
138 result.resize(size);
139 size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
140 string.size(), (uint8_t *)result.data(), result.size());
141 }
142 result.truncate(size);
143 return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(result)));
144 }
145
146 return QCollatorSortKey(new QCollatorSortKeyPrivate(QByteArray()));
147}
148
149int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const
150{
151 return qstrcmp(d->m_key, otherKey.d->m_key);
152}
153
154QT_END_NAMESPACE
155