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
5#include "qcollator_p.h"
6#include "qstringlist.h"
7#include "qstring.h"
9
10#include <cstring>
11#include <cwchar>
12
14
16{
17 if (!isC()) {
18 if (locale != QLocale::system().collation()) {
19 qWarning("Only the C and system collation locales are supported "
20 "with the POSIX collation implementation");
21 }
22 if (caseSensitivity != Qt::CaseSensitive)
23 qWarning("Case insensitive sorting unsupported in the posix collation implementation");
24 }
25 if (numericMode)
26 qWarning("Numeric mode unsupported in the posix collation implementation");
28 qWarning("Ignoring punctuation unsupported in the posix collation implementation");
29 dirty = false;
30}
31
33{
34}
35
36static void stringToWCharArray(QVarLengthArray<wchar_t> &ret, QStringView string)
37{
38 ret.resize(string.length());
39 qsizetype len = string.toWCharArray(ret.data());
40 ret.resize(len+1);
41 ret[len] = 0;
42}
43
44int QCollator::compare(QStringView s1, QStringView s2) const
45{
46 if (!s1.size())
47 return s2.size() ? -1 : 0;
48 if (!s2.size())
49 return +1;
50
51 if (d->isC())
52 return s1.compare(s2, caseSensitivity());
53
54 d->ensureInitialized();
55
56 QVarLengthArray<wchar_t> array1, array2;
57 stringToWCharArray(array1, s1);
58 stringToWCharArray(array2, s2);
59 return std::wcscoll(array1.constData(), array2.constData());
60}
61
62QCollatorSortKey QCollator::sortKey(const QString &string) const
63{
64 d->ensureInitialized();
65
66 QVarLengthArray<wchar_t> original;
67 stringToWCharArray(original, string);
68 QList<wchar_t> result(original.size());
69 if (d->isC()) {
70 std::copy(original.cbegin(), original.cend(), result.begin());
71 } else {
72 auto availableSizeIncludingNullTerminator = result.size();
73 size_t neededSizeExcludingNullTerminator = std::wcsxfrm(
74 result.data(), original.constData(), availableSizeIncludingNullTerminator);
75 if (neededSizeExcludingNullTerminator > size_t(availableSizeIncludingNullTerminator - 1)) {
76 result.resize(neededSizeExcludingNullTerminator + 1);
77 availableSizeIncludingNullTerminator = result.size();
78 neededSizeExcludingNullTerminator = std::wcsxfrm(result.data(), original.constData(),
79 availableSizeIncludingNullTerminator);
80 Q_ASSERT(neededSizeExcludingNullTerminator
81 == size_t(availableSizeIncludingNullTerminator - 1));
82 }
83 result.resize(neededSizeExcludingNullTerminator + 1);
84 result[neededSizeExcludingNullTerminator] = 0;
85 }
86 return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(result)));
87}
88
89int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const
90{
91 return std::wcscmp(d->m_key.constData(), otherKey.d->m_key.constData());
92}
93
94QT_END_NAMESPACE
static void stringToWCharArray(QVarLengthArray< wchar_t > &ret, QStringView string)