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// Qt-Security score:critical reason:data-parser
4
5#include "qcollator_p.h"
6#include "qlocale_p.h"
7#include "qstringlist.h"
8#include "qstring.h"
9
10#include <QtCore/private/qcore_mac_p.h>
11
12#include <CoreFoundation/CoreFoundation.h>
13#include <CoreFoundation/CFLocale.h>
14
15#include <cstring>
16#include <QDebug>
17
19
21{
23 /*
24 LocaleRefFromLocaleString() will accept "POSIX" as the locale name, but
25 the locale it produces (named "pos") doesn't implement the [A-Z] < [a-z]
26 behavior we expect of the C locale. We can use QStringView to get round
27 that for collation, but this leaves no way to do a sort key.
28 */
29 if (isC())
30 return;
31
32 LocaleRef localeRef;
33 OSStatus status =
34 LocaleRefFromLocaleString(QLocalePrivate::get(locale)->bcp47Name().constData(), &localeRef);
35 if (status != 0)
36 qWarning("Couldn't initialize the locale (%d)", int(status));
37
38 UInt32 options = 0;
39 if (caseSensitivity == Qt::CaseInsensitive)
40 options |= kUCCollateCaseInsensitiveMask;
41 if (numericMode)
42 options |= kUCCollateDigitsAsNumberMask | kUCCollateDigitsOverrideMask;
43 if (!ignorePunctuation)
44 options |= kUCCollatePunctuationSignificantMask;
45
46 status = UCCreateCollator(localeRef, 0, options, &collator);
47 if (status != 0)
48 qWarning("Couldn't initialize the collator (%d)", int(status));
49
50 dirty = false;
51}
52
54{
55 if (collator)
56 UCDisposeCollator(&collator);
57 collator = 0;
58}
59
60int QCollator::compare(QStringView s1, QStringView s2) const
61{
62 if (!s1.size())
63 return s2.size() ? -1 : 0;
64 if (!s2.size())
65 return +1;
66
67 if (!d)
68 d = new QCollatorPrivate(QLocale().collation());
69
70 d->ensureInitialized();
71
72 if (!d->collator)
73 return s1.compare(s2, caseSensitivity());
74
75 SInt32 result;
76 Boolean equivalent;
77 UCCompareText(d->collator,
78 reinterpret_cast<const UniChar *>(s1.data()), s1.size(),
79 reinterpret_cast<const UniChar *>(s2.data()), s2.size(),
80 &equivalent,
81 &result);
82 if (equivalent)
83 return 0;
84 return result < 0 ? -1 : 1;
85}
86
87QCollatorSortKey QCollator::sortKey(const QString &string) const
88{
89 if (!d)
90 d = new QCollatorPrivate(QLocale().collation());
91
92 d->ensureInitialized();
93
94 QList<UCCollationValue> ret;
95 if (!d->collator) {
96 // What should (or even *can*) we do here ? (See init()'s comment.)
97 qWarning("QCollator doesn't support sort keys for the C locale on Darwin");
98 return QCollatorPrivate::sortKeyFromData(std::move(ret));
99 }
100
101 auto text = reinterpret_cast<const UniChar *>(string.constData());
102 // Documentation recommends having it 5 times as big as the input
103 ret.resizeForOverwrite(string.size() * 5);
104 ItemCount actualSize;
105 int status = UCGetCollationKey(d->collator, text, string.size(),
106 ret.size(), &actualSize, ret.data());
107
108 ret.resize(actualSize + 1);
109 if (status == kUCOutputBufferTooSmall) {
110 status = UCGetCollationKey(d->collator, text, string.size(),
111 ret.size(), &actualSize, ret.data());
112 Q_ASSERT(status != kUCOutputBufferTooSmall);
113 Q_ASSERT(ret.size() == qsizetype(actualSize + 1));
114 }
115 ret[actualSize] = 0;
116 return QCollatorPrivate::sortKeyFromData(std::move(ret));
117}
118
119int QCollatorSortKey::compare(const QCollatorSortKey &key) const noexcept
120{
121 SInt32 order;
122 UCCompareCollationKeys(d->m_key.data(), d->m_key.size(),
123 key.d->m_key.data(), key.d->m_key.size(),
124 nullptr, &order);
125 return order;
126}
127
128QT_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:697