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 collationOptions = 0;
39 // enable normalization by default
40 collationOptions |= kUCCollateComposeInsensitiveMask;
41
42 if (options.testFlag(Opt::CaseInsensitive))
43 collationOptions |= kUCCollateCaseInsensitiveMask;
44 if (options.testFlag(Opt::NumericSort))
45 collationOptions |= kUCCollateDigitsAsNumberMask | kUCCollateDigitsOverrideMask;
46 if (!options.testFlag(Opt::IgnorePunctuation))
47 collationOptions |= kUCCollatePunctuationSignificantMask;
48 if (options.testFlag(Opt::DiacriticInsensitive))
49 collationOptions |= kUCCollateDiacritInsensitiveMask;
50
51 status = UCCreateCollator(localeRef, 0, collationOptions, &collator);
52 if (status != 0)
53 qWarning("Couldn't initialize the collator (%d)", int(status));
54
55 dirty = false;
56}
57
59{
60 if (collator)
61 UCDisposeCollator(&collator);
62 collator = 0;
63}
64
65int QCollator::compare(QStringView s1, QStringView s2) const
66{
67 if (!s1.size())
68 return s2.size() ? -1 : 0;
69 if (!s2.size())
70 return +1;
71
72 if (!d)
73 d = new QCollatorPrivate(QLocale().collation());
74
75 d->ensureInitialized();
76
77 if (!d->collator)
78 return s1.compare(s2, caseSensitivity());
79
80 SInt32 result;
81 Boolean equivalent;
82 UCCompareText(d->collator,
83 reinterpret_cast<const UniChar *>(s1.data()), s1.size(),
84 reinterpret_cast<const UniChar *>(s2.data()), s2.size(),
85 &equivalent,
86 &result);
87 if (equivalent)
88 return 0;
89 return result < 0 ? -1 : 1;
90}
91
92QCollatorSortKey QCollator::sortKey(const QString &string) const
93{
94 if (!d)
95 d = new QCollatorPrivate(QLocale().collation());
96
97 d->ensureInitialized();
98
99 QList<UCCollationValue> ret;
100 if (!d->collator) {
101 // What should (or even *can*) we do here ? (See init()'s comment.)
102 qWarning("QCollator doesn't support sort keys for the C locale on Darwin");
103 return QCollatorPrivate::sortKeyFromData(std::move(ret));
104 }
105
106 auto text = reinterpret_cast<const UniChar *>(string.constData());
107 // Documentation recommends having it 5 times as big as the input
108 ret.resizeForOverwrite(string.size() * 5);
109 ItemCount actualSize;
110 int status = UCGetCollationKey(d->collator, text, string.size(),
111 ret.size(), &actualSize, ret.data());
112
113 ret.resize(actualSize + 1);
114 if (status == kUCOutputBufferTooSmall) {
115 status = UCGetCollationKey(d->collator, text, string.size(),
116 ret.size(), &actualSize, ret.data());
117 Q_ASSERT(status != kUCOutputBufferTooSmall);
118 Q_ASSERT(ret.size() == qsizetype(actualSize + 1));
119 }
120 ret[actualSize] = 0;
121 return QCollatorPrivate::sortKeyFromData(std::move(ret));
122}
123
124int QCollatorSortKey::compare(const QCollatorSortKey &key) const noexcept
125{
126 SInt32 order;
127 UCCompareCollationKeys(d->m_key.data(), d->m_key.size(),
128 key.d->m_key.data(), key.d->m_key.size(),
129 nullptr, &order);
130 return order;
131}
132
133QT_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