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 d->ensureInitialized();
83
84 if (d->collator) {
85 // truncating sizes (QTBUG-105038)
86 return ucol_strcoll(d->collator,
87 reinterpret_cast<const UChar *>(s1.data()), s1.size(),
88 reinterpret_cast<const UChar *>(s2.data()), s2.size());
89 }
90
91 return QtPrivate::compareStrings(s1, s2, d->caseSensitivity);
92}
93
94QCollatorSortKey QCollator::sortKey(const QString &string) const
95{
96 d->ensureInitialized();
97
98 if (d->isC())
99 return QCollatorSortKey(new QCollatorSortKeyPrivate(string.toUtf8()));
100
101 if (d->collator) {
102 QByteArray result(16 + string.size() + (string.size() >> 2), Qt::Uninitialized);
103 // truncating sizes (QTBUG-105038)
104 int size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
105 string.size(), (uint8_t *)result.data(), result.size());
106 if (size > result.size()) {
107 result.resize(size);
108 size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
109 string.size(), (uint8_t *)result.data(), result.size());
110 }
111 result.truncate(size);
112 return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(result)));
113 }
114
115 return QCollatorSortKey(new QCollatorSortKeyPrivate(QByteArray()));
116}
117
118int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const
119{
120 return qstrcmp(d->m_key, otherKey.d->m_key);
121}
122
123QT_END_NAMESPACE
CollatorType collator
Definition qcollator_p.h:67
Combined button and popup list for selecting options.