summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qcollator_win.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/tools/qcollator_win.cpp')
-rw-r--r--src/corelib/tools/qcollator_win.cpp65
1 files changed, 43 insertions, 22 deletions
diff --git a/src/corelib/tools/qcollator_win.cpp b/src/corelib/tools/qcollator_win.cpp
index 5a838c1b50..9d81de882f 100644
--- a/src/corelib/tools/qcollator_win.cpp
+++ b/src/corelib/tools/qcollator_win.cpp
@@ -60,6 +60,8 @@ extern LCID qt_inIsoNametoLCID(const char *name);
void QCollatorPrivate::init()
{
collator = 0;
+ if (isC())
+ return;
#ifndef USE_COMPARESTRINGEX
localeID = qt_inIsoNametoLCID(QLocalePrivate::get(locale)->bcp47Name().constData());
@@ -70,6 +72,8 @@ void QCollatorPrivate::init()
if (caseSensitivity == Qt::CaseInsensitive)
collator |= NORM_IGNORECASE;
+ // WINE does not support SORT_DIGITSASNUMBERS :-(
+ // (and its std::sort() crashes on bad comparisons, QTBUG-74209)
if (numericMode)
collator |= SORT_DIGITSASNUMBERS;
@@ -83,42 +87,57 @@ void QCollatorPrivate::cleanup()
{
}
-
-int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const
+int QCollator::compare(QStringView s1, QStringView s2) const
{
+ if (d->isC())
+ return s1.compare(s2, d->caseSensitivity);
+
if (d->dirty)
d->init();
//* from Windows documentation *
- // Returns one of the following values if successful. To maintain the C runtime convention of
- // comparing strings, the value 2 can be subtracted from a nonzero return value. Then, the
- // meaning of <0, ==0, and >0 is consistent with the C runtime.
+ // Returns one of the following values if successful. To maintain the C
+ // runtime convention of comparing strings, the value 2 can be subtracted
+ // from a nonzero return value. Then, the meaning of <0, ==0, and >0 is
+ // consistent with the C runtime.
+ // [...] The function returns 0 if it does not succeed.
+ // https://docs.microsoft.com/en-us/windows/desktop/api/stringapiset/nf-stringapiset-comparestringex#return-value
#ifndef USE_COMPARESTRINGEX
- return CompareString(d->localeID, d->collator,
- reinterpret_cast<const wchar_t*>(s1), len1,
- reinterpret_cast<const wchar_t*>(s2), len2) - 2;
+ const int ret = CompareString(d->localeID, d->collator,
+ reinterpret_cast<const wchar_t *>(s1.data()), s1.size(),
+ reinterpret_cast<const wchar_t *>(s2.data()), s2.size());
#else
- return CompareStringEx(LPCWSTR(d->localeName.utf16()), d->collator,
- reinterpret_cast<LPCWSTR>(s1), len1,
- reinterpret_cast<LPCWSTR>(s2), len2, NULL, NULL, 0) - 2;
+ const int ret = CompareStringEx(LPCWSTR(d->localeName.utf16()), d->collator,
+ reinterpret_cast<LPCWSTR>(s1.data()), s1.size(),
+ reinterpret_cast<LPCWSTR>(s2.data()), s2.size(),
+ nullptr, nullptr, 0);
#endif
-}
-
-int QCollator::compare(const QString &str1, const QString &str2) const
-{
- return compare(str1.constData(), str1.size(), str2.constData(), str2.size());
-}
-
-int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const
-{
- return compare(s1.constData(), s1.size(), s2.constData(), s2.size());
+ if (Q_LIKELY(ret))
+ return ret - 2;
+
+ switch (DWORD error = GetLastError()) {
+ case ERROR_INVALID_FLAGS:
+ qWarning("Unsupported flags (%d) used in QCollator", int(d->collator));
+ break;
+ case ERROR_INVALID_PARAMETER:
+ qWarning("Invalid parameter for QCollator::compare()");
+ break;
+ default:
+ qWarning("Failed (%ld) comparison in QCollator::compare()", long(error));
+ break;
+ }
+ // We have no idea what to return, so pretend we think they're equal.
+ // At least that way we'll be consistent if we get the same values swapped ...
+ return 0;
}
QCollatorSortKey QCollator::sortKey(const QString &string) const
{
if (d->dirty)
d->init();
+ if (d->isC())
+ return QCollatorSortKey(new QCollatorSortKeyPrivate(string));
#ifndef USE_COMPARESTRINGEX
int size = LCMapStringW(d->localeID, LCMAP_SORTKEY | d->collator,
@@ -141,7 +160,9 @@ QCollatorSortKey QCollator::sortKey(const QString &string) const
NULL, NULL, 0);
#endif
if (finalSize == 0) {
- qWarning() << "there were problems when generating the ::sortKey by LCMapStringW with error:" << GetLastError();
+ qWarning()
+ << "there were problems when generating the ::sortKey by LCMapStringW with error:"
+ << GetLastError();
}
return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(ret)));
}