summaryrefslogtreecommitdiffstats
path: root/src/partition/jsondbcollator.cpp
blob: 869bf3f8a7f9fb0f69264ef964e260595176ebd9 (plain)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtAddOn.JsonDb module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef NO_COLLATION_SUPPORT

#include "jsondbcollator.h"
#include "jsondbcollator_p.h"

#include <unicode/utypes.h>
#include <unicode/ucol.h>
#include <unicode/ustring.h>

#include "qdebug.h"

QT_BEGIN_NAMESPACE_JSONDB_PARTITION

JsonDbCollatorPrivate::~JsonDbCollatorPrivate()
{
    if (collator)
        ucol_close(collator);
}

static const int collationStringsCount = 13;
static const char * const collationStrings[collationStringsCount] = {
    "default",
    "big5han",
    "dict",
    "direct",
    "gb2312",
    "phonebk",
    "pinyin",
    "phonetic",
    "reformed",
    "standard",
    "stroke",
    "trad",
    "unihan"
};

JsonDbCollator::JsonDbCollator(const QLocale &locale, JsonDbCollator::Collation collation)
    : d(new JsonDbCollatorPrivate)
{
    d->locale = locale;
    if ((int)collation >= 0 && (int)collation < collationStringsCount)
        d->collation = collation;

    init();
}

JsonDbCollator::JsonDbCollator(const JsonDbCollator &other)
    : d(other.d)
{
    d->ref.ref();
}

JsonDbCollator::~JsonDbCollator()
{
    if (!d->ref.deref())
        delete d;
}

JsonDbCollator &JsonDbCollator::operator=(const JsonDbCollator &other)
{
    if (this != &other) {
        if (!d->ref.deref())
            delete d;
        *d = *other.d;
        d->ref.ref();
    }
    return *this;
}

void JsonDbCollator::setLocale(const QLocale &locale)
{
    if (d->ref.load() != 1)
        detach();
    if (d->collator)
        ucol_close(d->collator);
    d->locale = locale;

    init();
}

QLocale JsonDbCollator::locale() const
{
    return d->locale;
}

void JsonDbCollator::setCollation(JsonDbCollator::Collation collation)
{
    if ((int)collation < 0 || (int)collation >= collationStringsCount)
        return;

    if (d->ref.load() != 1)
        detach();
    if (d->collator)
        ucol_close(d->collator);
    d->collation = collation;

    init();
}

JsonDbCollator::Collation JsonDbCollator::collation() const
{
    return d->collation;
}

void JsonDbCollator::init()
{
    Q_ASSERT((int)d->collation < collationStringsCount);
    const char *collationString = collationStrings[(int)d->collation];
    UErrorCode status = U_ZERO_ERROR;
    QByteArray name = (d->locale.bcp47Name().replace(QLatin1Char('-'), QLatin1Char('_')) + QLatin1String("@collation=") + QLatin1String(collationString)).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 |= JsonDbCollator::PreferUpperCase; break;
    case UCOL_LOWER_FIRST: d->options |= JsonDbCollator::PreferLowerCase; break;
    case UCOL_OFF:
    default:
        break;
    }

    switch (ucol_getAttribute(d->collator, UCOL_FRENCH_COLLATION, &status)) {
    case UCOL_ON: d->options |= JsonDbCollator::FrenchCollation; break;
    case UCOL_OFF:
    default:
        break;
    }

    switch (ucol_getAttribute(d->collator, UCOL_ALTERNATE_HANDLING, &status)) {
    case UCOL_SHIFTED: d->options |= JsonDbCollator::IgnorePunctuation; break;
    case UCOL_NON_IGNORABLE:
    default:
        break;
    }


    switch (ucol_getAttribute(d->collator, UCOL_CASE_LEVEL, &status)) {
    case UCOL_ON: d->options |= JsonDbCollator::ExtraCaseLevel; break;
    case UCOL_OFF:
    default:
        break;
    }

    switch (ucol_getAttribute(d->collator, UCOL_HIRAGANA_QUATERNARY_MODE, &status)) {
    case UCOL_ON: d->options |= JsonDbCollator::HiraganaQuaternaryMode; break;
    case UCOL_OFF:
    default:
        break;
    }

    switch (ucol_getAttribute(d->collator, UCOL_NUMERIC_COLLATION, &status)) {
    case UCOL_ON: d->options |= JsonDbCollator::NumericMode; break;
    case UCOL_OFF:
    default:
        break;
    }
}

void JsonDbCollator::detach()
{
    if (d->ref.load() != 1) {
        JsonDbCollatorPrivate *x = new JsonDbCollatorPrivate;
        x->ref.store(1);
        x->strength = d->strength;
        x->options = d->options;
        x->modified = true;
        x->collator = 0;
        if (!d->ref.deref())
            delete d;
        d = x;
    }
}

void JsonDbCollator::setStrength(JsonDbCollator::Strength strength)
{
    if (d->ref.load() != 1)
        detach();

    switch (strength) {
    case JsonDbCollator::PrimaryStrength:    ucol_setStrength(d->collator, UCOL_PRIMARY); break;
    case JsonDbCollator::SecondaryStrength:  ucol_setStrength(d->collator, UCOL_SECONDARY); break;
    case JsonDbCollator::TertiaryStrength:   ucol_setStrength(d->collator, UCOL_TERTIARY); break;
    case JsonDbCollator::QuaternaryStrength: ucol_setStrength(d->collator, UCOL_QUATERNARY); break;
    case JsonDbCollator::IdenticalStrength:  ucol_setStrength(d->collator, UCOL_IDENTICAL); break;
    default:
        qWarning("Invalid strength mode %d", strength);
    }
}

JsonDbCollator::Strength JsonDbCollator::strength() const
{
    switch (ucol_getStrength(d->collator)) {
    case UCOL_PRIMARY:   return JsonDbCollator::PrimaryStrength;
    case UCOL_SECONDARY: return JsonDbCollator::SecondaryStrength;
    case UCOL_QUATERNARY:return JsonDbCollator::QuaternaryStrength;
    case UCOL_IDENTICAL: return JsonDbCollator::IdenticalStrength;
    case UCOL_TERTIARY:
    default:
        return JsonDbCollator::TertiaryStrength;
    }
    return JsonDbCollator::TertiaryStrength;
}

void JsonDbCollator::setOptions(JsonDbCollator::Options options)
{
    if (d->ref.load() != 1)
        detach();

    d->options = options;

    UErrorCode status = U_ZERO_ERROR;

    if (options & JsonDbCollator::PreferUpperCase)
        ucol_setAttribute(d->collator, UCOL_CASE_FIRST, UCOL_UPPER_FIRST, &status);
    else if (options & JsonDbCollator::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 & JsonDbCollator::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 & JsonDbCollator::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 & JsonDbCollator::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 & JsonDbCollator::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 & JsonDbCollator::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 & JsonDbCollator::NumericMode ? UCOL_ON : UCOL_OFF, &status);
    if (U_FAILURE(status))
        qWarning("ucol_setAttribute: numeric collation failed: %d", status);
}

JsonDbCollator::Options JsonDbCollator::options() const
{
    return d->options;
}

int JsonDbCollator::compare(const QString &s1, const QString &s2) const
{
    return d->compare((ushort *)s1.constData(), s1.size(), (ushort *)s2.constData(), s2.size());
}

int JsonDbCollator::compare(const QStringRef &s1, const QStringRef &s2) const
{
    return d->compare((ushort *)s1.constData(), s1.size(), (ushort *)s2.constData(), s2.size());
}

int JsonDbCollatorPrivate::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 JsonDbCollator::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_JSONDB_PARTITION

#endif // NO_COLLATION_SUPPORT