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 (caseSensitivity != Qt::CaseSensitive)
24 qWarning("Case insensitive sorting unsupported in the posix collation implementation");
25 }
26 if (numericMode)
27 qWarning("Numeric mode unsupported in the posix collation implementation");
29 qWarning("Ignoring punctuation unsupported in the posix collation implementation");
30 dirty = false;
31}
32
34{
35}
36
37static void stringToWCharArray(QVarLengthArray<wchar_t> &ret, QStringView string)
38{
39 ret.resize(string.length());
40 qsizetype len = string.toWCharArray(ret.data());
41 ret.resize(len+1);
42 ret[len] = 0;
43}
44
45int QCollator::compare(QStringView s1, QStringView s2) const
46{
47 if (!s1.size())
48 return s2.size() ? -1 : 0;
49 if (!s2.size())
50 return +1;
51
52 if (!d)
53 d = new QCollatorPrivate(QLocale().collation());
54
55 if (d->isC())
56 return s1.compare(s2, caseSensitivity());
57
58 d->ensureInitialized();
59
60 QVarLengthArray<wchar_t> array1, array2;
61 stringToWCharArray(array1, s1);
62 stringToWCharArray(array2, s2);
63 return std::wcscoll(array1.constData(), array2.constData());
64}
65
66QCollatorSortKey QCollator::sortKey(const QString &string) const
67{
68 if (!d)
69 d = new QCollatorPrivate(QLocale().collation());
70
71 d->ensureInitialized();
72
73 QVarLengthArray<wchar_t> original;
74 stringToWCharArray(original, string);
75 QList<wchar_t> result(original.size());
76 if (d->isC()) {
77 std::copy(original.cbegin(), original.cend(), result.begin());
78 } else {
79 auto availableSizeIncludingNullTerminator = result.size();
80 size_t neededSizeExcludingNullTerminator = std::wcsxfrm(
81 result.data(), original.constData(), availableSizeIncludingNullTerminator);
82 if (neededSizeExcludingNullTerminator > size_t(availableSizeIncludingNullTerminator - 1)) {
83 result.resize(neededSizeExcludingNullTerminator + 1);
84 availableSizeIncludingNullTerminator = result.size();
85 neededSizeExcludingNullTerminator = std::wcsxfrm(result.data(), original.constData(),
86 availableSizeIncludingNullTerminator);
87 Q_ASSERT(neededSizeExcludingNullTerminator
88 == size_t(availableSizeIncludingNullTerminator - 1));
89 }
90 result.resize(neededSizeExcludingNullTerminator + 1);
91 result[neededSizeExcludingNullTerminator] = 0;
92 }
93 return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(result)));
94}
95
96int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const
97{
98 return std::wcscmp(d->m_key.constData(), otherKey.d->m_key.constData());
99}
100
101QT_END_NAMESPACE
bool isC() const
Definition qcollator_p.h:68
static void stringToWCharArray(QVarLengthArray< wchar_t > &ret, QStringView string)