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 dirty = false;
43}
44
46{
47}
48
49int QCollator::compare(QStringView s1, QStringView s2) const
50{
51 if (!s1.size())
52 return s2.size() ? -1 : 0;
53 if (!s2.size())
54 return +1;
55
56 if (!d)
57 d = new QCollatorPrivate(QCollatorPrivate(QLocale().collation()));
58
59 if (d->isC())
60 return s1.compare(s2, caseSensitivity());
61
62 d->ensureInitialized();
63
64 //* from Windows documentation *
65 // Returns one of the following values if successful. To maintain the C
66 // runtime convention of comparing strings, the value 2 can be subtracted
67 // from a nonzero return value. Then, the meaning of <0, ==0, and >0 is
68 // consistent with the C runtime.
69 // [...] The function returns 0 if it does not succeed.
70 // https://docs.microsoft.com/en-us/windows/desktop/api/stringapiset/nf-stringapiset-comparestringex#return-value
71
72 const QString locale = d->locale.bcp47Name();
73 const int ret = CompareStringEx(reinterpret_cast<const wchar_t *>(locale.constData()),
74 d->collator,
75 reinterpret_cast<const wchar_t *>(s1.data()), s1.size(),
76 reinterpret_cast<const wchar_t *>(s2.data()), s2.size(),
77 nullptr, nullptr, 0);
78 if (Q_LIKELY(ret))
79 return ret - 2;
80
81 switch (DWORD error = GetLastError()) {
82 case ERROR_INVALID_FLAGS:
83 qWarning("Unsupported flags (%d) used in QCollator", int(d->collator));
84 break;
85 case ERROR_INVALID_PARAMETER:
86 qWarning("Invalid parameter for QCollator::compare()");
87 break;
88 default:
89 qErrnoWarning(error, "Failed comparison in QCollator::compare()");
90 break;
91 }
92 // We have no idea what to return, so pretend we think they're equal.
93 // At least that way we'll be consistent if we get the same values swapped ...
94 return 0;
95}
96
97QCollatorSortKey QCollator::sortKey(const QString &string) const
98{
99 if (string.isEmpty()) {
100 // empty strings sort before everything and LCMapString doesn't
101 // like them
102 return QCollatorPrivate::sortKeyFromData(QByteArray());
103 }
104
105 if (!d)
106 d = new QCollatorPrivate(QCollatorPrivate(QLocale().collation()));
107 d->ensureInitialized();
108
109 if (d->isC())
110 return QCollatorPrivate::sortKeyFromData(string.toUtf8());
111
112 const QString localeName = d->locale.bcp47Name();
113 auto callLcMapString = [&](LPWSTR lpDestStr, int cchDest) {
114 // note: truncating sizes (QTBUG-105038)
115 return LCMapStringEx(reinterpret_cast<const wchar_t *>(localeName.constData()),
116 LCMAP_SORTKEY | d->collator,
117 reinterpret_cast<const wchar_t*>(string.constData()), string.size(),
118 lpDestStr, cchDest, nullptr, nullptr, 0);
119 };
120
121 int size = callLcMapString(nullptr, 0);
122 CollatorKeyType ret(size, Qt::Uninitialized);
123 size = callLcMapString(reinterpret_cast<wchar_t*>(ret.data()), ret.size());
124 if (size != ret.size())
125 ret.truncate(size);
126 if (size == 0)
127 qErrnoWarning("Error when generating the ::sortKey by LCMapStringEx");
128
129 return QCollatorPrivate::sortKeyFromData(std::move(ret));
130}
131
132int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const noexcept
133{
134 return d->m_key.compare(otherKey.d->m_key);
135}
136
137QT_END_NAMESPACE
bool isC() const
Definition qcollator_p.h:68
CollatorType collator
Definition qcollator_p.h:64
Combined button and popup list for selecting options.
#define SORT_DIGITSASNUMBERS