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 UColAttributeValue val = (caseSensitivity == Qt::CaseSensitive)
47 ? UCOL_DEFAULT_STRENGTH : UCOL_SECONDARY;
48
49 status = U_ZERO_ERROR;
50 ucol_setAttribute(collator, UCOL_STRENGTH, val, &status);
51 if (U_FAILURE(status))
52 qWarning("ucol_setAttribute: Case First failed: %d", status);
53
54 status = U_ZERO_ERROR;
55 ucol_setAttribute(collator, UCOL_NUMERIC_COLLATION, numericMode ? UCOL_ON : UCOL_OFF, &status);
56 if (U_FAILURE(status))
57 qWarning("ucol_setAttribute: numeric collation failed: %d", status);
58
59 status = U_ZERO_ERROR;
60 ucol_setAttribute(collator, UCOL_ALTERNATE_HANDLING,
61 ignorePunctuation ? UCOL_SHIFTED : UCOL_NON_IGNORABLE, &status);
62 if (U_FAILURE(status))
63 qWarning("ucol_setAttribute: Alternate handling failed: %d", status);
64
65 dirty = false;
66}
67
69{
70 if (collator)
71 ucol_close(collator);
72 collator = nullptr;
73}
74
75int QCollator::compare(QStringView s1, QStringView s2) const
76{
77 if (!s1.size())
78 return s2.size() ? -1 : 0;
79 if (!s2.size())
80 return +1;
81
82 if (!d)
83 d = new QCollatorPrivate(QLocale().collation());
84
85 d->ensureInitialized();
86
87 if (d->collator) {
88 // truncating sizes (QTBUG-105038)
89 return ucol_strcoll(d->collator,
90 reinterpret_cast<const UChar *>(s1.data()), s1.size(),
91 reinterpret_cast<const UChar *>(s2.data()), s2.size());
92 }
93
94 return QtPrivate::compareStrings(s1, s2, d->caseSensitivity);
95}
96
97QCollatorSortKey QCollator::sortKey(const QString &string) const
98{
99 if (!d)
100 d = new QCollatorPrivate(QLocale().collation());
101
102 d->ensureInitialized();
103
104 if (d->isC())
105 return QCollatorSortKey(new QCollatorSortKeyPrivate(string.toUtf8()));
106
107 if (d->collator) {
108 QByteArray result(16 + string.size() + (string.size() >> 2), Qt::Uninitialized);
109 // truncating sizes (QTBUG-105038)
110 int size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
111 string.size(), (uint8_t *)result.data(), result.size());
112 if (size > result.size()) {
113 result.resize(size);
114 size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
115 string.size(), (uint8_t *)result.data(), result.size());
116 }
117 result.truncate(size);
118 return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(result)));
119 }
120
121 return QCollatorSortKey(new QCollatorSortKeyPrivate(QByteArray()));
122}
123
124int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const
125{
126 return d->m_key.compare(otherKey.d->m_key);
127}
128
129QT_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:601
Combined button and popup list for selecting options.