Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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
18
20{
21 cleanup();
22 if (isC())
23 return;
24
25 UErrorCode status = U_ZERO_ERROR;
26 QByteArray name = QLocalePrivate::get(locale)->bcp47Name('_');
27 collator = ucol_open(name.constData(), &status);
28 if (U_FAILURE(status)) {
29 qWarning("Could not create collator: %d", status);
30 collator = nullptr;
31 dirty = false;
32 return;
33 }
34
35 // enable normalization by default
36 ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &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(collator, UCOL_STRENGTH, val, &status);
50 if (U_FAILURE(status))
51 qWarning("ucol_setAttribute: Case First failed: %d", status);
52
53 status = U_ZERO_ERROR;
54 ucol_setAttribute(collator, UCOL_NUMERIC_COLLATION, numericMode ? UCOL_ON : UCOL_OFF, &status);
55 if (U_FAILURE(status))
56 qWarning("ucol_setAttribute: numeric collation failed: %d", status);
57
58 status = U_ZERO_ERROR;
59 ucol_setAttribute(collator, UCOL_ALTERNATE_HANDLING,
60 ignorePunctuation ? UCOL_SHIFTED : UCOL_NON_IGNORABLE, &status);
61 if (U_FAILURE(status))
62 qWarning("ucol_setAttribute: Alternate handling failed: %d", status);
63
64 dirty = false;
65}
66
68{
69 if (collator)
70 ucol_close(collator);
71 collator = nullptr;
72}
73
75{
76 if (!s1.size())
77 return s2.size() ? -1 : 0;
78 if (!s2.size())
79 return +1;
80
82
83 if (d->collator) {
84 // truncating sizes (QTBUG-105038)
85 return ucol_strcoll(d->collator,
86 reinterpret_cast<const UChar *>(s1.data()), s1.size(),
87 reinterpret_cast<const UChar *>(s2.data()), s2.size());
88 }
89
91}
92
94{
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(d->collator, (const UChar *)string.constData(),
104 string.size(), (uint8_t *)result.data(), result.size());
105 if (size > result.size()) {
106 result.resize(size);
107 size = ucol_getSortKey(d->collator, (const UChar *)string.constData(),
108 string.size(), (uint8_t *)result.data(), result.size());
109 }
110 result.truncate(size);
111 return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(result)));
112 }
113
115}
116
118{
119 return qstrcmp(d->m_key, otherKey.d->m_key);
120}
121
\inmodule QtCore
Definition qbytearray.h:57
void ensureInitialized()
Definition qcollator_p.h:77
CollatorType collator
Definition qcollator_p.h:66
Qt::CaseSensitivity caseSensitivity
Definition qcollator_p.h:61
CollatorKeyType m_key
\inmodule QtCore
Definition qcollator.h:19
QExplicitlySharedDataPointer< QCollatorSortKeyPrivate > d
Definition qcollator.h:37
int compare(const QCollatorSortKey &key) const
Compares this key to otherKey, which must have been created by the same QCollator's sortKey() as this...
QCollatorSortKey sortKey(const QString &string) const
Returns a sortKey for string.
int compare(const QString &s1, const QString &s2) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qcollator.h:70
static const QLocalePrivate * get(const QLocale &l)
Definition qlocale_p.h:514
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Combined button and popup list for selecting options.
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs=Qt::CaseSensitive) noexcept
@ CaseSensitive
constexpr Initialization Uninitialized
Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2)
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
#define qWarning
Definition qlogging.h:166
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
GLuint name
GLuint GLfloat * val
GLuint64EXT * result
[6]
#define s2