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->isC())
53 return s1.compare(s2, caseSensitivity());
54
55 d->ensureInitialized();
56
57 QVarLengthArray<wchar_t> array1, array2;
58 stringToWCharArray(array1, s1);
59 stringToWCharArray(array2, s2);
60 return std::wcscoll(array1.constData(), array2.constData());
61}
62
63QCollatorSortKey QCollator::sortKey(const QString &string) const
64{
65 d->ensureInitialized();
66
67 QVarLengthArray<wchar_t> original;
68 stringToWCharArray(original, string);
69 QList<wchar_t> result(original.size());
70 if (d->isC()) {
71 std::copy(original.cbegin(), original.cend(), result.begin());
72 } else {
73 auto availableSizeIncludingNullTerminator = result.size();
74 size_t neededSizeExcludingNullTerminator = std::wcsxfrm(
75 result.data(), original.constData(), availableSizeIncludingNullTerminator);
76 if (neededSizeExcludingNullTerminator > size_t(availableSizeIncludingNullTerminator - 1)) {
77 result.resize(neededSizeExcludingNullTerminator + 1);
78 availableSizeIncludingNullTerminator = result.size();
79 neededSizeExcludingNullTerminator = std::wcsxfrm(result.data(), original.constData(),
80 availableSizeIncludingNullTerminator);
81 Q_ASSERT(neededSizeExcludingNullTerminator
82 == size_t(availableSizeIncludingNullTerminator - 1));
83 }
84 result.resize(neededSizeExcludingNullTerminator + 1);
85 result[neededSizeExcludingNullTerminator] = 0;
86 }
87 return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(result)));
88}
89
90int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const
91{
92 return std::wcscmp(d->m_key.constData(), otherKey.d->m_key.constData());
93}
94
95QT_END_NAMESPACE
static void stringToWCharArray(QVarLengthArray< wchar_t > &ret, QStringView string)