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_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
4#include "qcollator_p.h"
5#include "qlocale_p.h"
6#include "qstringlist.h"
7#include "qstring.h"
8
9#include <QDebug>
10
11#include <qt_windows.h>
12#include <qsysinfo.h>
13
15
16//NOTE: SORT_DIGITSASNUMBERS is available since win7
17#ifndef SORT_DIGITSASNUMBERS
18#define SORT_DIGITSASNUMBERS 8
19#endif
20
21// implemented in qlocale_win.cpp
22extern LCID qt_inIsoNametoLCID(const char *name);
23
25{
26 collator = 0;
27 if (isC())
28 return;
29
30 localeID = qt_inIsoNametoLCID(QLocalePrivate::get(locale)->bcp47Name().constData());
31
33 collator |= NORM_IGNORECASE;
34
35 // WINE does not support SORT_DIGITSASNUMBERS :-(
36 // (and its std::sort() crashes on bad comparisons, QTBUG-74209)
37 if (numericMode)
39
41 collator |= NORM_IGNORESYMBOLS;
42
43 dirty = false;
44}
45
47{
48}
49
51{
52 if (!s1.size())
53 return s2.size() ? -1 : 0;
54 if (!s2.size())
55 return +1;
56
57 if (d->isC())
58 return s1.compare(s2, d->caseSensitivity);
59
61
62 //* from Windows documentation *
63 // Returns one of the following values if successful. To maintain the C
64 // runtime convention of comparing strings, the value 2 can be subtracted
65 // from a nonzero return value. Then, the meaning of <0, ==0, and >0 is
66 // consistent with the C runtime.
67 // [...] The function returns 0 if it does not succeed.
68 // https://docs.microsoft.com/en-us/windows/desktop/api/stringapiset/nf-stringapiset-comparestringex#return-value
69
70 const int ret = CompareString(d->localeID, d->collator,
71 reinterpret_cast<const wchar_t *>(s1.data()), s1.size(),
72 reinterpret_cast<const wchar_t *>(s2.data()), s2.size());
73 if (Q_LIKELY(ret))
74 return ret - 2;
75
76 switch (DWORD error = GetLastError()) {
77 case ERROR_INVALID_FLAGS:
78 qWarning("Unsupported flags (%d) used in QCollator", int(d->collator));
79 break;
80 case ERROR_INVALID_PARAMETER:
81 qWarning("Invalid parameter for QCollator::compare()");
82 break;
83 default:
84 qWarning("Failed (%ld) comparison in QCollator::compare()", long(error));
85 break;
86 }
87 // We have no idea what to return, so pretend we think they're equal.
88 // At least that way we'll be consistent if we get the same values swapped ...
89 return 0;
90}
91
93{
95
96 if (d->isC())
97 return QCollatorSortKey(new QCollatorSortKeyPrivate(string));
98
99 // truncating sizes (QTBUG-105038)
100 int size = LCMapStringW(d->localeID, LCMAP_SORTKEY | d->collator,
101 reinterpret_cast<const wchar_t*>(string.constData()), string.size(),
102 0, 0);
103
105 int finalSize = LCMapStringW(d->localeID, LCMAP_SORTKEY | d->collator,
106 reinterpret_cast<const wchar_t*>(string.constData()), string.size(),
107 reinterpret_cast<wchar_t*>(ret.data()), ret.size());
108 if (finalSize == 0) {
109 qWarning()
110 << "there were problems when generating the ::sortKey by LCMapStringW with error:"
111 << GetLastError();
112 }
113 return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(ret)));
114}
115
116int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const
117{
118 return d->m_key.compare(otherKey.d->m_key);
119}
120
void ensureInitialized()
Definition qcollator_p.h:77
CollatorType collator
Definition qcollator_p.h:66
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
static const QLocalePrivate * get(const QLocale &l)
Definition qlocale_p.h:514
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Combined button and popup list for selecting options.
@ CaseInsensitive
constexpr Initialization Uninitialized
#define SORT_DIGITSASNUMBERS
LCID qt_inIsoNametoLCID(const char *name)
#define Q_LIKELY(x)
DBusConnection const char DBusError * error
#define qWarning
Definition qlogging.h:166
return ret
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
GLuint name
#define s2