Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcollator_icu.cpp
Go to the documentation of this file.
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// Qt-Security score:critical reason:data-parser
5
6#include "qcollator_p.h"
7#include "qlocale_p.h"
8#include "qstringlist.h"
9#include "qstring.h"
10
11#include <unicode/utypes.h>
12#include <unicode/ucol.h>
13#include <unicode/ustring.h>
14#include <unicode/ures.h>
15
16#include "qdebug.h"
17
19
21{
23 if (isC())
24 return;
25
26 UErrorCode status = U_ZERO_ERROR;
27 QByteArray name = QLocalePrivate::get(locale)->bcp47Name('_');
28 collator = ucol_open(name.constData(), &status);
29 if (U_FAILURE(status)) {
30 qWarning("Could not create collator: %d", status);
31 collator = nullptr;
32 dirty = false;
33 return;
34 }
35
36 // enable normalization by default
37 ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
38
39 // The strength attribute in ICU is rather badly documented. Basically UCOL_PRIMARY
40 // ignores differences between base characters and accented characters as well as case.
41 // So A and A-umlaut would compare equal.
42 // UCOL_SECONDARY ignores case differences. UCOL_TERTIARY is the default in most languages
43 // and does case sensitive comparison.
44 // UCOL_QUATERNARY is used as default in a few languages such as Japanese to take care of some
45 // additional differences in those languages.
46 if (options.testFlag(Opt::DiacriticInsensitive)) {
47 // UCOL_PRIMARY ignores both diacritics and case
48 status = U_ZERO_ERROR;
49 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
50 if (U_FAILURE(status))
51 qWarning("ucol_setAttribute: Diacritic and case insensitivity failed: %d", status);
52
53 if (!options.testFlag(Opt::CaseInsensitive)) {
54 // Re-add case distinction if CaseInsensitive hasn't been set
55 status = U_ZERO_ERROR;
56 ucol_setAttribute(collator, UCOL_CASE_LEVEL, UCOL_ON, &status);
57 if (U_FAILURE(status)) {
58 qWarning("ucol_setAttribute: Diacritic insensitivity with case distinction failed:"
59 " %d", status);
60 }
61 }
62 } else {
63 const UColAttributeValue strength
64 = options.testFlag(Opt::CaseInsensitive) ? UCOL_SECONDARY : UCOL_DEFAULT_STRENGTH;
65 // Case sensitivity setting only
66 status = U_ZERO_ERROR;
67 ucol_setAttribute(collator, UCOL_STRENGTH, strength, &status);
68 if (U_FAILURE(status))
69 qWarning("ucol_setAttribute: Case sensitivity failed: %d", status);
70 }
71
72 status = U_ZERO_ERROR;
73 ucol_setAttribute(collator, UCOL_NUMERIC_COLLATION,
74 options.testFlag(Opt::NumericSort) ? UCOL_ON : UCOL_OFF, &status);
75 if (U_FAILURE(status))
76 qWarning("ucol_setAttribute: numeric collation failed: %d", status);
77
78 status = U_ZERO_ERROR;
79 ucol_setAttribute(collator, UCOL_ALTERNATE_HANDLING,
80 options.testFlag(Opt::IgnorePunctuation) ? UCOL_SHIFTED
81 : UCOL_NON_IGNORABLE, &status);
82 if (U_FAILURE(status))
83 qWarning("ucol_setAttribute: Alternate handling failed: %d", status);
84
85 dirty = false;
86}
87
89{
90 if (collator)
91 ucol_close(collator);
92 collator = nullptr;
93}
94
95int QCollator::compare(QStringView s1, QStringView s2) const
96{
97 if (!s1.size())
98 return s2.size() ? -1 : 0;
99 if (!s2.size())
100 return +1;
101
102 if (!d)
103 d = new QCollatorPrivate(QLocale().collation());
104
105 d->ensureInitialized();
106
107 if (d->collator) {
108 // truncating sizes (QTBUG-105038)
109 return ucol_strcoll(d->collator,
110 reinterpret_cast<const UChar *>(s1.data()), s1.size(),
111 reinterpret_cast<const UChar *>(s2.data()), s2.size());
112 }
113
114 return QtPrivate::compareStrings(s1, s2, caseSensitivity());
115}
116
117QCollatorSortKey QCollator::sortKey(const QString &string) const
118{
119 if (!d)
120 d = new QCollatorPrivate(QLocale().collation());
121
122 d->ensureInitialized();
123
124 if (d->isC())
125 return QCollatorPrivate::sortKeyFromData(string.toUtf8());
126
127 if (d->collator) {
128 QByteArray result(16 + string.size() + (string.size() >> 2), Qt::Uninitialized);
129 // truncating sizes (QTBUG-105038)
130 int size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
131 string.size(), (uint8_t *)result.data(), result.size());
132 if (size > result.size()) {
133 result.resize(size);
134 size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
135 string.size(), (uint8_t *)result.data(), result.size());
136 }
137 result.truncate(size);
138 return QCollatorPrivate::sortKeyFromData(std::move(result));
139 }
140
141 return QCollatorPrivate::sortKeyFromData(QByteArray());
142}
143
144int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const noexcept
145{
146 return d->m_key.compare(otherKey.d->m_key);
147}
148
149QT_END_NAMESPACE
bool isC() const
Definition qcollator_p.h:68
CollatorType collator
Definition qcollator_p.h:64
static const QLocalePrivate * get(const QLocale &l)
Definition qlocale_p.h:697
Combined button and popup list for selecting options.