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_macx.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 Aleix Pol Gonzalez <aleixpol@kde.org>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qcollator_p.h"
5#include "qlocale_p.h"
6#include "qstringlist.h"
7#include "qstring.h"
8
9#include <QtCore/private/qcore_mac_p.h>
10
11#include <CoreFoundation/CoreFoundation.h>
12#include <CoreFoundation/CFLocale.h>
13
14#include <cstring>
15#include <QDebug>
16
18
20{
21 cleanup();
22 /*
23 LocaleRefFromLocaleString() will accept "POSIX" as the locale name, but
24 the locale it produces (named "pos") doesn't implement the [A-Z] < [a-z]
25 behavior we expect of the C locale. We can use QStringView to get round
26 that for collation, but this leaves no way to do a sort key.
27 */
28 if (isC())
29 return;
30
31 LocaleRef localeRef;
32 OSStatus status =
33 LocaleRefFromLocaleString(QLocalePrivate::get(locale)->bcp47Name().constData(), &localeRef);
34 if (status != 0)
35 qWarning("Couldn't initialize the locale (%d)", int(status));
36
37 UInt32 options = 0;
39 options |= kUCCollateCaseInsensitiveMask;
40 if (numericMode)
41 options |= kUCCollateDigitsAsNumberMask | kUCCollateDigitsOverrideMask;
43 options |= kUCCollatePunctuationSignificantMask;
44
45 status = UCCreateCollator(localeRef, 0, options, &collator);
46 if (status != 0)
47 qWarning("Couldn't initialize the collator (%d)", int(status));
48
49 dirty = false;
50}
51
53{
54 if (collator)
55 UCDisposeCollator(&collator);
56 collator = 0;
57}
58
60{
61 if (!s1.size())
62 return s2.size() ? -1 : 0;
63 if (!s2.size())
64 return +1;
65
67
68 if (!d->collator)
69 return s1.compare(s2, caseSensitivity());
70
71 SInt32 result;
72 Boolean equivalent;
73 UCCompareText(d->collator,
74 reinterpret_cast<const UniChar *>(s1.data()), s1.size(),
75 reinterpret_cast<const UniChar *>(s2.data()), s2.size(),
76 &equivalent,
77 &result);
78 if (equivalent)
79 return 0;
80 return result < 0 ? -1 : 1;
81}
82
84{
86
87 if (!d->collator) {
88 // What should (or even *can*) we do here ? (See init()'s comment.)
89 qWarning("QCollator doesn't support sort keys for the C locale on Darwin");
90 return QCollatorSortKey(nullptr);
91 }
92
93 auto text = reinterpret_cast<const UniChar *>(string.constData());
94 // Documentation recommends having it 5 times as big as the input
95 QList<UCCollationValue> ret(string.size() * 5);
96 ItemCount actualSize;
97 int status = UCGetCollationKey(d->collator, text, string.size(),
98 ret.size(), &actualSize, ret.data());
99
100 ret.resize(actualSize + 1);
101 if (status == kUCOutputBufferTooSmall) {
102 status = UCGetCollationKey(d->collator, text, string.size(),
103 ret.size(), &actualSize, ret.data());
104 Q_ASSERT(status != kUCOutputBufferTooSmall);
105 Q_ASSERT(ret.size() == qsizetype(actualSize + 1));
106 }
107 ret[actualSize] = 0;
108 return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(ret)));
109}
110
112{
113 if (!d.data())
114 return 0;
115
116 SInt32 order;
117 UCCompareCollationKeys(d->m_key.data(), d->m_key.size(),
118 key.d->m_key.data(), key.d->m_key.size(),
119 nullptr, &order);
120 return order;
121}
122
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
Qt::CaseSensitivity caseSensitivity() const
Returns case sensitivity of the collator.
T * data() const noexcept
Returns a pointer to the shared data object.
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
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
QString text
Combined button and popup list for selecting options.
@ CaseInsensitive
#define qWarning
Definition qlogging.h:166
return ret
GLuint64 key
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
GLuint64EXT * result
[6]
GLfixed GLfixed GLint GLint order
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define s2
ptrdiff_t qsizetype
Definition qtypes.h:165