1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/** This file is part of QtCollator **
Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).*
Contact: Nokia Corporation (info@qt.nokia.com)**
GNU Lesser General Public License Usage
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, Nokia gives you certain additional rights.
These rights are described in the Nokia 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.
Other Usage
Alternatively, this file may be used in accordance with the terms and
conditions contained in a signed written agreement between you and Nokia.
*/
#include "qtcollator.h"
#include <unicode/utypes.h>
#include <unicode/ucol.h>
#include <unicode/ustring.h>
#include "qdebug.h"
QT_BEGIN_NAMESPACE
class QtCollatorPrivate
{
public:
QBasicAtomicInt ref;
bool modified;
QLocale locale;
QtCollator::Strength strength;
QtCollator::Options options;
UCollator *collator;
QtCollatorPrivate()
: modified(true),
strength(QtCollator::TertiaryStrength),
options(0),
collator(0)
{ ref = 1; }
int compare(ushort *s1, int len1, ushort *s2, int len2);
};
QtCollator::QtCollator(const QLocale &locale)
: d(new QtCollatorPrivate)
{
setLocale(locale);
}
QtCollator::QtCollator(const QtCollator &other)
: d(other.d)
{
d->ref.ref();
}
QtCollator::~QtCollator()
{
if (!d->ref.deref()) {
if (d->collator)
ucol_close(d->collator);
}
}
QtCollator &QtCollator::operator=(const QtCollator &other)
{
if (this != &other) {
if (!d->ref.deref()) {
if (d->collator)
ucol_close(d->collator);
}
*d = *other.d;
d->ref.ref();
}
return *this;
}
void QtCollator::setLocale(const QLocale &locale)
{
if (d->ref != 1)
detach();
if (d->collator)
ucol_close(d->collator);
d->locale = locale;
UErrorCode status = U_ZERO_ERROR;
QByteArray name = locale.bcp47Name().replace(QLatin1Char('-'), QLatin1Char('_')).toLatin1();
d->collator = ucol_open(name.constData(), &status);
if (U_FAILURE(status))
qWarning("Could not create collator: %d", status);
// enable normalization by default
ucol_setAttribute(d->collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
// fetch options from the collator
d->options = 0;
switch (ucol_getAttribute(d->collator, UCOL_CASE_FIRST, &status)) {
case UCOL_UPPER_FIRST: d->options |= QtCollator::PreferUpperCase; break;
case UCOL_LOWER_FIRST: d->options |= QtCollator::PreferLowerCase; break;
case UCOL_OFF:
default:
break;
}
switch (ucol_getAttribute(d->collator, UCOL_FRENCH_COLLATION, &status)) {
case UCOL_ON: d->options |= QtCollator::FrenchCollation; break;
case UCOL_OFF:
default:
break;
}
switch (ucol_getAttribute(d->collator, UCOL_ALTERNATE_HANDLING, &status)) {
case UCOL_SHIFTED: d->options |= QtCollator::IgnorePunctuation; break;
case UCOL_NON_IGNORABLE:
default:
break;
}
switch (ucol_getAttribute(d->collator, UCOL_CASE_LEVEL, &status)) {
case UCOL_ON: d->options |= QtCollator::ExtraCaseLevel; break;
case UCOL_OFF:
default:
break;
}
switch (ucol_getAttribute(d->collator, UCOL_HIRAGANA_QUATERNARY_MODE, &status)) {
case UCOL_ON: d->options |= QtCollator::HiraganaQuaternaryMode; break;
case UCOL_OFF:
default:
break;
}
switch (ucol_getAttribute(d->collator, UCOL_NUMERIC_COLLATION, &status)) {
case UCOL_ON: d->options |= QtCollator::NumericMode; break;
case UCOL_OFF:
default:
break;
}
}
QLocale QtCollator::locale() const
{
return d->locale;
}
void QtCollator::detach()
{
if (d->ref != 1) {
QtCollatorPrivate *x = new QtCollatorPrivate;
x->ref = 1;
x->strength = d->strength;
x->options = d->options;
x->modified = true;
x->collator = 0;
if (!d->ref.deref()) {
if (d->collator)
ucol_close(d->collator);
}
d = x;
}
}
void QtCollator::setStrength(QtCollator::Strength strength)
{
if (d->ref != 1)
detach();
switch (strength) {
case QtCollator::PrimaryStrength: ucol_setStrength(d->collator, UCOL_PRIMARY); break;
case QtCollator::SecondaryStrength: ucol_setStrength(d->collator, UCOL_SECONDARY); break;
case QtCollator::TertiaryStrength: ucol_setStrength(d->collator, UCOL_TERTIARY); break;
case QtCollator::QuaternaryStrength: ucol_setStrength(d->collator, UCOL_QUATERNARY); break;
case QtCollator::IdenticalStrength: ucol_setStrength(d->collator, UCOL_IDENTICAL); break;
default:
qWarning("Invalid strength mode %d", strength);
}
}
QtCollator::Strength QtCollator::strength() const
{
switch (ucol_getStrength(d->collator)) {
case UCOL_PRIMARY: return QtCollator::PrimaryStrength;
case UCOL_SECONDARY: return QtCollator::SecondaryStrength;
case UCOL_QUATERNARY:return QtCollator::QuaternaryStrength;
case UCOL_IDENTICAL: return QtCollator::IdenticalStrength;
case UCOL_TERTIARY:
default:
return QtCollator::TertiaryStrength;
}
return QtCollator::TertiaryStrength;
}
void QtCollator::setOptions(QtCollator::Options options)
{
if (d->ref != 1)
detach();
d->options = options;
UErrorCode status = U_ZERO_ERROR;
if (options & QtCollator::PreferUpperCase)
ucol_setAttribute(d->collator, UCOL_CASE_FIRST, UCOL_UPPER_FIRST, &status);
else if (options & QtCollator::PreferLowerCase)
ucol_setAttribute(d->collator, UCOL_CASE_FIRST, UCOL_LOWER_FIRST, &status);
else
ucol_setAttribute(d->collator, UCOL_CASE_FIRST, UCOL_OFF, &status);
if (U_FAILURE(status))
qWarning("ucol_setAttribute: Case First failed: %d", status);
ucol_setAttribute(d->collator, UCOL_FRENCH_COLLATION,
options & QtCollator::FrenchCollation ? UCOL_ON : UCOL_OFF, &status);
if (U_FAILURE(status))
qWarning("ucol_setAttribute: French collation failed: %d", status);
ucol_setAttribute(d->collator, UCOL_NORMALIZATION_MODE,
options & QtCollator::DisableNormalization ? UCOL_OFF : UCOL_ON, &status);
if (U_FAILURE(status))
qWarning("ucol_setAttribute: Normalization mode failed: %d", status);
ucol_setAttribute(d->collator, UCOL_ALTERNATE_HANDLING,
(options & QtCollator::IgnorePunctuation) ? UCOL_SHIFTED : UCOL_NON_IGNORABLE, &status);
if (U_FAILURE(status))
qWarning("ucol_setAttribute: Alternate handling failed: %d", status);
ucol_setAttribute(d->collator, UCOL_CASE_LEVEL,
options & QtCollator::ExtraCaseLevel ? UCOL_ON : UCOL_OFF, &status);
if (U_FAILURE(status))
qWarning("ucol_setAttribute: Case level failed: %d", status);
ucol_setAttribute(d->collator, UCOL_HIRAGANA_QUATERNARY_MODE,
options & QtCollator::HiraganaQuaternaryMode ? UCOL_ON : UCOL_OFF, &status);
if (U_FAILURE(status))
qWarning("ucol_setAttribute: hiragana mode failed: %d", status);
ucol_setAttribute(d->collator, UCOL_NUMERIC_COLLATION,
options & QtCollator::NumericMode ? UCOL_ON : UCOL_OFF, &status);
if (U_FAILURE(status))
qWarning("ucol_setAttribute: numeric collation failed: %d", status);
}
QtCollator::Options QtCollator::options() const
{
return d->options;
}
int QtCollator::compare(const QString &s1, const QString &s2) const
{
return d->compare((ushort *)s1.constData(), s1.size(), (ushort *)s2.constData(), s2.size());
}
int QtCollator::compare(const QStringRef &s1, const QStringRef &s2) const
{
return d->compare((ushort *)s1.constData(), s1.size(), (ushort *)s2.constData(), s2.size());
}
int QtCollatorPrivate::compare(ushort *s1, int len1, ushort *s2, int len2)
{
const UCollationResult result =
ucol_strcoll(collator, (const UChar *)s1, len1, (const UChar *)s2, len2);
return result;
}
QByteArray QtCollator::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()) {
#ifdef _DEBUG
qDebug() << "### sortKey realloc from" << result.size() << "to" << size << "for string" << string.size();
#endif
result.resize(size);
size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
string.size(), (uint8_t *)result.data(), result.size());
}
result.truncate(size);
return result;
}
QT_END_NAMESPACE
|