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 if (!d->collator) {
95 // What should (or even *can*) we do here ? (See init()'s comment.)
96 qWarning("QCollator doesn't support sort keys for the C locale on Darwin");
97 return QCollatorSortKey(nullptr);
98 }
99
100 auto text = reinterpret_cast<const UniChar *>(string.constData());
101 // Documentation recommends having it 5 times as big as the input
102 QList<UCCollationValue> ret(string.size() * 5);
103 ItemCount actualSize;
104 int status = UCGetCollationKey(d->collator, text, string.size(),
105 ret.size(), &actualSize, ret.data());
106
107 ret.resize(actualSize + 1);
108 if (status == kUCOutputBufferTooSmall) {
109 status = UCGetCollationKey(d->collator, text, string.size(),
110 ret.size(), &actualSize, ret.data());
111 Q_ASSERT(status != kUCOutputBufferTooSmall);
112 Q_ASSERT(ret.size() == qsizetype(actualSize + 1));
113 }
114 ret[actualSize] = 0;
115 return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(ret)));
116}
117
118int QCollatorSortKey::compare(const QCollatorSortKey &key) const
119{
120 if (!d.data())
121 return 0;
122
123 SInt32 order;
124 UCCompareCollationKeys(d->m_key.data(), d->m_key.size(),
125 key.d->m_key.data(), key.d->m_key.size(),
126 nullptr, &order);
127 return order;
128}
129
130QT_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:601