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_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{
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;
38 if (caseSensitivity == Qt::CaseInsensitive)
39 options |= kUCCollateCaseInsensitiveMask;
40 if (numericMode)
41 options |= kUCCollateDigitsAsNumberMask | kUCCollateDigitsOverrideMask;
42 if (!ignorePunctuation)
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
59int QCollator::compare(QStringView s1, QStringView s2) const
60{
61 if (!s1.size())
62 return s2.size() ? -1 : 0;
63 if (!s2.size())
64 return +1;
65
66 d->ensureInitialized();
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
83QCollatorSortKey QCollator::sortKey(const QString &string) const
84{
85 d->ensureInitialized();
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
111int QCollatorSortKey::compare(const QCollatorSortKey &key) const
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
123QT_END_NAMESPACE
CollatorType collator
Definition qcollator_p.h:66