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_posix.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:critical reason:data-parser
5
6#include "qcollator_p.h"
7#include "qstringlist.h"
8#include "qstring.h"
10
11#include <cstring>
12#include <cwchar>
13
15
17{
18 if (!isC()) {
19 if (locale != QLocale::system().collation()) {
20 qWarning("Only the C and system collation locales are supported "
21 "with the POSIX collation implementation");
22 }
23 if (options.testFlag(Opt::CaseInsensitive))
24 qWarning("Case insensitive sorting unsupported in the posix collation implementation");
25 }
26 if (options.testFlag(Opt::NumericSort))
27 qWarning("Numeric mode unsupported in the posix collation implementation");
28 if (options.testFlag(Opt::IgnorePunctuation))
29 qWarning("Ignoring punctuation unsupported in the posix collation implementation");
30 if (options.testFlag(Opt::DiacriticInsensitive))
31 qWarning("Ignoring diacritic marks unsupported in the POSIX collation implementation.");
32
33 dirty = false;
34}
35
37{
38}
39
40static void stringToWCharArray(QVarLengthArray<wchar_t> &ret, QStringView string)
41{
42 ret.resize(string.length());
43 qsizetype len = string.toWCharArray(ret.data());
44 ret.resize(len+1);
45 ret[len] = 0;
46}
47
48int QCollator::compare(QStringView s1, QStringView s2) const
49{
50 if (!s1.size())
51 return s2.size() ? -1 : 0;
52 if (!s2.size())
53 return +1;
54
55 if (!d)
56 d = new QCollatorPrivate(QLocale().collation());
57
58 if (d->isC())
59 return s1.compare(s2, caseSensitivity());
60
61 d->ensureInitialized();
62
63 QVarLengthArray<wchar_t> array1, array2;
64 stringToWCharArray(array1, s1);
65 stringToWCharArray(array2, s2);
66 return std::wcscoll(array1.constData(), array2.constData());
67}
68
69QCollatorSortKey QCollator::sortKey(const QString &string) const
70{
71 if (!d)
72 d = new QCollatorPrivate(QLocale().collation());
73
74 d->ensureInitialized();
75
76 QVarLengthArray<wchar_t> original;
77 stringToWCharArray(original, string);
78 QList<wchar_t> result(original.size());
79 if (d->isC()) {
80 std::copy(original.cbegin(), original.cend(), result.begin());
81 } else {
82 auto availableSizeIncludingNullTerminator = result.size();
83 size_t neededSizeExcludingNullTerminator = std::wcsxfrm(
84 result.data(), original.constData(), availableSizeIncludingNullTerminator);
85 if (neededSizeExcludingNullTerminator > size_t(availableSizeIncludingNullTerminator - 1)) {
86 result.resize(neededSizeExcludingNullTerminator + 1);
87 availableSizeIncludingNullTerminator = result.size();
88 neededSizeExcludingNullTerminator = std::wcsxfrm(result.data(), original.constData(),
89 availableSizeIncludingNullTerminator);
90 Q_ASSERT(neededSizeExcludingNullTerminator
91 == size_t(availableSizeIncludingNullTerminator - 1));
92 }
93 result.resize(neededSizeExcludingNullTerminator + 1);
94 result[neededSizeExcludingNullTerminator] = 0;
95 }
96 return QCollatorPrivate::sortKeyFromData(std::move(result));
97}
98
99int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const noexcept
100{
101 return std::wcscmp(d->m_key.constData(), otherKey.d->m_key.constData());
102}
103
104QT_END_NAMESPACE
bool isC() const
Definition qcollator_p.h:68
static void stringToWCharArray(QVarLengthArray< wchar_t > &ret, QStringView string)