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
22// implemented in qlocale_win.cpp
23extern LCID qt_inIsoNametoLCID(const char *name);
24
26{
27 collator = 0;
28 if (isC())
29 return;
30
31 localeID = qt_inIsoNametoLCID(QLocalePrivate::get(locale)->bcp47Name().constData());
32
33 if (caseSensitivity == Qt::CaseInsensitive)
34 collator |= NORM_IGNORECASE;
35
36 // WINE does not support SORT_DIGITSASNUMBERS :-(
37 // (and its std::sort() crashes on bad comparisons, QTBUG-74209)
38 if (numericMode)
40
41 if (ignorePunctuation)
42 collator |= NORM_IGNORESYMBOLS;
43
44 dirty = false;
45}
46
48{
49}
50
51int QCollator::compare(QStringView s1, QStringView s2) const
52{
53 if (!s1.size())
54 return s2.size() ? -1 : 0;
55 if (!s2.size())
56 return +1;
57
58 if (d->isC())
59 return s1.compare(s2, d->caseSensitivity);
60
61 d->ensureInitialized();
62
63 //* from Windows documentation *
64 // Returns one of the following values if successful. To maintain the C
65 // runtime convention of comparing strings, the value 2 can be subtracted
66 // from a nonzero return value. Then, the meaning of <0, ==0, and >0 is
67 // consistent with the C runtime.
68 // [...] The function returns 0 if it does not succeed.
69 // https://docs.microsoft.com/en-us/windows/desktop/api/stringapiset/nf-stringapiset-comparestringex#return-value
70
71 const int ret = CompareString(d->localeID, d->collator,
72 reinterpret_cast<const wchar_t *>(s1.data()), s1.size(),
73 reinterpret_cast<const wchar_t *>(s2.data()), s2.size());
74 if (Q_LIKELY(ret))
75 return ret - 2;
76
77 switch (DWORD error = GetLastError()) {
78 case ERROR_INVALID_FLAGS:
79 qWarning("Unsupported flags (%d) used in QCollator", int(d->collator));
80 break;
81 case ERROR_INVALID_PARAMETER:
82 qWarning("Invalid parameter for QCollator::compare()");
83 break;
84 default:
85 qWarning("Failed (%ld) comparison in QCollator::compare()", long(error));
86 break;
87 }
88 // We have no idea what to return, so pretend we think they're equal.
89 // At least that way we'll be consistent if we get the same values swapped ...
90 return 0;
91}
92
93QCollatorSortKey QCollator::sortKey(const QString &string) const
94{
95 d->ensureInitialized();
96
97 if (d->isC())
98 return QCollatorSortKey(new QCollatorSortKeyPrivate(string));
99
100 // truncating sizes (QTBUG-105038)
101 int size = LCMapStringW(d->localeID, LCMAP_SORTKEY | d->collator,
102 reinterpret_cast<const wchar_t*>(string.constData()), string.size(),
103 0, 0);
104
105 QString ret(size, Qt::Uninitialized);
106 int finalSize = LCMapStringW(d->localeID, LCMAP_SORTKEY | d->collator,
107 reinterpret_cast<const wchar_t*>(string.constData()), string.size(),
108 reinterpret_cast<wchar_t*>(ret.data()), ret.size());
109 if (finalSize == 0) {
110 qWarning()
111 << "there were problems when generating the ::sortKey by LCMapStringW with error:"
112 << GetLastError();
113 }
114 return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(ret)));
115}
116
117int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const
118{
119 return d->m_key.compare(otherKey.d->m_key);
120}
121
122QT_END_NAMESPACE
CollatorType collator
Definition qcollator_p.h:67
Combined button and popup list for selecting options.
#define SORT_DIGITSASNUMBERS
LCID qt_inIsoNametoLCID(const char *name)