1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2020 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 | |
53 | QT_BEGIN_NAMESPACE |
54 | |
55 | void 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 | |
103 | void QCollatorPrivate::cleanup() |
104 | { |
105 | if (collator) |
106 | ucol_close(collator); |
107 | collator = nullptr; |
108 | } |
109 | |
110 | int QCollator::compare(QStringView s1, QStringView s2) const |
111 | { |
112 | if (!s1.size()) |
113 | return s2.size() ? -1 : 0; |
114 | if (!s2.size()) |
115 | return +1; |
116 | |
117 | if (d->dirty) |
118 | d->init(); |
119 | |
120 | if (d->collator) { |
121 | return ucol_strcoll(d->collator, |
122 | reinterpret_cast<const UChar *>(s1.data()), s1.size(), |
123 | reinterpret_cast<const UChar *>(s2.data()), s2.size()); |
124 | } |
125 | |
126 | return QString::compare_helper(s1.data(), s1.size(), |
127 | s2.data(), s2.size(), |
128 | d->caseSensitivity); |
129 | } |
130 | |
131 | QCollatorSortKey QCollator::sortKey(const QString &string) const |
132 | { |
133 | if (d->dirty) |
134 | d->init(); |
135 | if (d->isC()) |
136 | return QCollatorSortKey(new QCollatorSortKeyPrivate(string.toUtf8())); |
137 | |
138 | if (d->collator) { |
139 | QByteArray result(16 + string.size() + (string.size() >> 2), Qt::Uninitialized); |
140 | int size = ucol_getSortKey(d->collator, (const UChar *)string.constData(), |
141 | string.size(), (uint8_t *)result.data(), result.size()); |
142 | if (size > result.size()) { |
143 | result.resize(size); |
144 | size = ucol_getSortKey(d->collator, (const UChar *)string.constData(), |
145 | string.size(), (uint8_t *)result.data(), result.size()); |
146 | } |
147 | result.truncate(size); |
148 | return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(result))); |
149 | } |
150 | |
151 | return QCollatorSortKey(new QCollatorSortKeyPrivate(QByteArray())); |
152 | } |
153 | |
154 | int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const |
155 | { |
156 | return qstrcmp(d->m_key, otherKey.d->m_key); |
157 | } |
158 | |
159 | QT_END_NAMESPACE |
160 | |