1 | // Copyright (C) 2020 The Qt Company Ltd. |
2 | // Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org> |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #include "qcollator_p.h" |
6 | #include "qlocale_p.h" |
7 | #include "qstringlist.h" |
8 | #include "qstring.h" |
9 | |
10 | #include <unicode/utypes.h> |
11 | #include <unicode/ucol.h> |
12 | #include <unicode/ustring.h> |
13 | #include <unicode/ures.h> |
14 | |
15 | #include "qdebug.h" |
16 | |
17 | QT_BEGIN_NAMESPACE |
18 | |
19 | void QCollatorPrivate::init() |
20 | { |
21 | cleanup(); |
22 | if (isC()) |
23 | return; |
24 | |
25 | UErrorCode status = U_ZERO_ERROR; |
26 | QByteArray name = QLocalePrivate::get(l: locale)->bcp47Name(separator: '_'); |
27 | collator = ucol_open(loc: name.constData(), status: &status); |
28 | if (U_FAILURE(code: status)) { |
29 | qWarning(msg: "Could not create collator: %d" , status); |
30 | collator = nullptr; |
31 | dirty = false; |
32 | return; |
33 | } |
34 | |
35 | // enable normalization by default |
36 | ucol_setAttribute(coll: collator, attr: UCOL_NORMALIZATION_MODE, value: UCOL_ON, status: &status); |
37 | |
38 | // The strength attribute in ICU is rather badly documented. Basically UCOL_PRIMARY |
39 | // ignores differences between base characters and accented characters as well as case. |
40 | // So A and A-umlaut would compare equal. |
41 | // UCOL_SECONDARY ignores case differences. UCOL_TERTIARY is the default in most languages |
42 | // and does case sensitive comparison. |
43 | // UCOL_QUATERNARY is used as default in a few languages such as Japanese to take care of some |
44 | // additional differences in those languages. |
45 | UColAttributeValue val = (caseSensitivity == Qt::CaseSensitive) |
46 | ? UCOL_DEFAULT_STRENGTH : UCOL_SECONDARY; |
47 | |
48 | status = U_ZERO_ERROR; |
49 | ucol_setAttribute(coll: collator, attr: UCOL_STRENGTH, value: val, status: &status); |
50 | if (U_FAILURE(code: status)) |
51 | qWarning(msg: "ucol_setAttribute: Case First failed: %d" , status); |
52 | |
53 | status = U_ZERO_ERROR; |
54 | ucol_setAttribute(coll: collator, attr: UCOL_NUMERIC_COLLATION, value: numericMode ? UCOL_ON : UCOL_OFF, status: &status); |
55 | if (U_FAILURE(code: status)) |
56 | qWarning(msg: "ucol_setAttribute: numeric collation failed: %d" , status); |
57 | |
58 | status = U_ZERO_ERROR; |
59 | ucol_setAttribute(coll: collator, attr: UCOL_ALTERNATE_HANDLING, |
60 | value: ignorePunctuation ? UCOL_SHIFTED : UCOL_NON_IGNORABLE, status: &status); |
61 | if (U_FAILURE(code: status)) |
62 | qWarning(msg: "ucol_setAttribute: Alternate handling failed: %d" , status); |
63 | |
64 | dirty = false; |
65 | } |
66 | |
67 | void QCollatorPrivate::cleanup() |
68 | { |
69 | if (collator) |
70 | ucol_close(coll: collator); |
71 | collator = nullptr; |
72 | } |
73 | |
74 | int QCollator::compare(QStringView s1, QStringView s2) const |
75 | { |
76 | if (!s1.size()) |
77 | return s2.size() ? -1 : 0; |
78 | if (!s2.size()) |
79 | return +1; |
80 | |
81 | d->ensureInitialized(); |
82 | |
83 | if (d->collator) { |
84 | // truncating sizes (QTBUG-105038) |
85 | return ucol_strcoll(coll: d->collator, |
86 | source: reinterpret_cast<const UChar *>(s1.data()), sourceLength: s1.size(), |
87 | target: reinterpret_cast<const UChar *>(s2.data()), targetLength: s2.size()); |
88 | } |
89 | |
90 | return QtPrivate::compareStrings(lhs: s1, rhs: s2, cs: d->caseSensitivity); |
91 | } |
92 | |
93 | QCollatorSortKey QCollator::sortKey(const QString &string) const |
94 | { |
95 | d->ensureInitialized(); |
96 | |
97 | if (d->isC()) |
98 | return QCollatorSortKey(new QCollatorSortKeyPrivate(string.toUtf8())); |
99 | |
100 | if (d->collator) { |
101 | QByteArray result(16 + string.size() + (string.size() >> 2), Qt::Uninitialized); |
102 | // truncating sizes (QTBUG-105038) |
103 | int size = ucol_getSortKey(coll: d->collator, source: (const UChar *)string.constData(), |
104 | sourceLength: string.size(), result: (uint8_t *)result.data(), resultLength: result.size()); |
105 | if (size > result.size()) { |
106 | result.resize(size); |
107 | size = ucol_getSortKey(coll: d->collator, source: (const UChar *)string.constData(), |
108 | sourceLength: string.size(), result: (uint8_t *)result.data(), resultLength: result.size()); |
109 | } |
110 | result.truncate(pos: size); |
111 | return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(result))); |
112 | } |
113 | |
114 | return QCollatorSortKey(new QCollatorSortKeyPrivate(QByteArray())); |
115 | } |
116 | |
117 | int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const |
118 | { |
119 | return qstrcmp(str1: d->m_key, str2: otherKey.d->m_key); |
120 | } |
121 | |
122 | QT_END_NAMESPACE |
123 | |