Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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"
8#include "qvarlengtharray.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 }
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
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
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
63{
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
void ensureInitialized()
Definition qcollator_p.h:77
Qt::CaseSensitivity caseSensitivity
Definition qcollator_p.h:61
CollatorKeyType m_key
\inmodule QtCore
Definition qcollator.h:19
QExplicitlySharedDataPointer< QCollatorSortKeyPrivate > d
Definition qcollator.h:37
int compare(const QCollatorSortKey &key) const
Compares this key to otherKey, which must have been created by the same QCollator's sortKey() as this...
QCollatorSortKey sortKey(const QString &string) const
Returns a sortKey for string.
int compare(const QString &s1, const QString &s2) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qcollator.h:70
Qt::CaseSensitivity caseSensitivity() const
Returns case sensitivity of the collator.
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition qlocale.cpp:2862
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Combined button and popup list for selecting options.
@ CaseSensitive
static void stringToWCharArray(QVarLengthArray< wchar_t > &ret, QStringView string)
#define qWarning
Definition qlogging.h:166
return ret
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
GLenum GLuint GLenum GLsizei length
GLuint64EXT * result
[6]
GLenum GLsizei len
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define s2
ptrdiff_t qsizetype
Definition qtypes.h:165