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_win.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 Aleix Pol Gonzalez <aleixpol@kde.org>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:critical reason:data-parser
4
5#include "qcollator_p.h"
6#include "qlocale_p.h"
7#include "qstringlist.h"
8#include "qstring.h"
9
10#include <QDebug>
11
12#include <qt_windows.h>
13#include <qsysinfo.h>
14
16
17//NOTE: SORT_DIGITSASNUMBERS is available since win7
18#ifndef SORT_DIGITSASNUMBERS
19#define SORT_DIGITSASNUMBERS 8
20#endif
21
23{
24 collator = 0;
25 if (isC())
26 return;
27
28 if (options.testFlag(Opt::CaseInsensitive))
29 collator |= NORM_IGNORECASE;
30
31 // WINE does not support SORT_DIGITSASNUMBERS :-(
32 // (and its std::sort() crashes on bad comparisons, QTBUG-74209)
33 if (options.testFlag(Opt::NumericSort))
35
36 if (options.testFlag(Opt::IgnorePunctuation))
37 collator |= NORM_IGNORESYMBOLS;
38
39 if (options.testFlag(Opt::DiacriticInsensitive))
40 collator |= LINGUISTIC_IGNOREDIACRITIC;
41
42 if (options.testFlag(Opt::WidthInsensitive))
43 collator |= NORM_IGNOREWIDTH;
44
45 dirty = false;
46}
47
49{
50}
51
52int QCollator::compare(QStringView s1, QStringView s2) const
53{
54 if (!s1.size())
55 return s2.size() ? -1 : 0;
56 if (!s2.size())
57 return +1;
58
59 if (!d)
60 d = new QCollatorPrivate(QCollatorPrivate(QLocale().collation()));
61
62 if (d->isC())
63 return s1.compare(s2, caseSensitivity());
64
65 d->ensureInitialized();
66
67 //* from Windows documentation *
68 // Returns one of the following values if successful. To maintain the C
69 // runtime convention of comparing strings, the value 2 can be subtracted
70 // from a nonzero return value. Then, the meaning of <0, ==0, and >0 is
71 // consistent with the C runtime.
72 // [...] The function returns 0 if it does not succeed.
73 // https://docs.microsoft.com/en-us/windows/desktop/api/stringapiset/nf-stringapiset-comparestringex#return-value
74
75 const QString locale = d->locale.bcp47Name();
76 const int ret = CompareStringEx(reinterpret_cast<const wchar_t *>(locale.constData()),
77 d->collator,
78 reinterpret_cast<const wchar_t *>(s1.data()), s1.size(),
79 reinterpret_cast<const wchar_t *>(s2.data()), s2.size(),
80 nullptr, nullptr, 0);
81 if (Q_LIKELY(ret))
82 return ret - 2;
83
84 switch (DWORD error = GetLastError()) {
85 case ERROR_INVALID_FLAGS:
86 qCWarning(lcQCollator, "Unsupported flags (%d) used in QCollator", int(d->collator));
87 break;
88 case ERROR_INVALID_PARAMETER:
89 qCWarning(lcQCollator, "Invalid parameter for QCollator::compare()");
90 break;
91 default:
92 qErrnoWarning(error, "Failed comparison in QCollator::compare()");
93 break;
94 }
95 // We have no idea what to return, so pretend we think they're equal.
96 // At least that way we'll be consistent if we get the same values swapped ...
97 return 0;
98}
99
100QCollatorSortKey QCollator::sortKey(const QString &string) const
101{
102 if (string.isEmpty()) {
103 // empty strings sort before everything and LCMapString doesn't
104 // like them
105 return QCollatorPrivate::sortKeyFromData(QByteArray());
106 }
107
108 if (!d)
109 d = new QCollatorPrivate(QCollatorPrivate(QLocale().collation()));
110 d->ensureInitialized();
111
112 if (d->isC())
113 return QCollatorPrivate::sortKeyFromData(string.toUtf8());
114
115 const QString localeName = d->locale.bcp47Name();
116 auto callLcMapString = [&](LPWSTR lpDestStr, int cchDest) {
117 // note: truncating sizes (QTBUG-105038)
118 return LCMapStringEx(reinterpret_cast<const wchar_t *>(localeName.constData()),
119 LCMAP_SORTKEY | d->collator,
120 reinterpret_cast<const wchar_t*>(string.constData()), string.size(),
121 lpDestStr, cchDest, nullptr, nullptr, 0);
122 };
123
124 int size = callLcMapString(nullptr, 0);
125 CollatorKeyType ret(size, Qt::Uninitialized);
126 size = callLcMapString(reinterpret_cast<wchar_t*>(ret.data()), ret.size());
127 if (size != ret.size())
128 ret.truncate(size);
129 if (size == 0)
130 qErrnoWarning("Error when generating the ::sortKey by LCMapStringEx");
131
132 return QCollatorPrivate::sortKeyFromData(std::move(ret));
133}
134
135int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const noexcept
136{
137 return d->m_key.compare(otherKey.d->m_key);
138}
139
140QT_END_NAMESPACE
bool isC() const
Definition qcollator_p.h:85
CollatorType collator
Definition qcollator_p.h:78
Combined button and popup list for selecting options.
#define SORT_DIGITSASNUMBERS