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 d->ensureInitialized();
68
69 if (!d->collator)
70 return s1.compare(s2, caseSensitivity());
71
72 SInt32 result;
73 Boolean equivalent;
74 UCCompareText(d->collator,
75 reinterpret_cast<const UniChar *>(s1.data()), s1.size(),
76 reinterpret_cast<const UniChar *>(s2.data()), s2.size(),
77 &equivalent,
78 &result);
79 if (equivalent)
80 return 0;
81 return result < 0 ? -1 : 1;
82}
83
84QCollatorSortKey QCollator::sortKey(const QString &string) const
85{
86 d->ensureInitialized();
87
88 if (!d->collator) {
89 // What should (or even *can*) we do here ? (See init()'s comment.)
90 qWarning("QCollator doesn't support sort keys for the C locale on Darwin");
91 return QCollatorSortKey(nullptr);
92 }
93
94 auto text = reinterpret_cast<const UniChar *>(string.constData());
95 // Documentation recommends having it 5 times as big as the input
96 QList<UCCollationValue> ret(string.size() * 5);
97 ItemCount actualSize;
98 int status = UCGetCollationKey(d->collator, text, string.size(),
99 ret.size(), &actualSize, ret.data());
100
101 ret.resize(actualSize + 1);
102 if (status == kUCOutputBufferTooSmall) {
103 status = UCGetCollationKey(d->collator, text, string.size(),
104 ret.size(), &actualSize, ret.data());
105 Q_ASSERT(status != kUCOutputBufferTooSmall);
106 Q_ASSERT(ret.size() == qsizetype(actualSize + 1));
107 }
108 ret[actualSize] = 0;
109 return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(ret)));
110}
111
112int QCollatorSortKey::compare(const QCollatorSortKey &key) const
113{
114 if (!d.data())
115 return 0;
116
117 SInt32 order;
118 UCCompareCollationKeys(d->m_key.data(), d->m_key.size(),
119 key.d->m_key.data(), key.d->m_key.size(),
120 nullptr, &order);
121 return order;
122}
123
124QT_END_NAMESPACE
CollatorType collator
Definition qcollator_p.h:67