Extend QCollator to support QStringView
This enables some simplification of the existing implementations. Refined wording of the documentation in the process. [ChangeLog][QtCore][QCollator] Added support for QStringView. Change-Id: Idffaae8d109173d47c7be076828f4b58dc334957 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
e89fbd8c3a
commit
a769ab62cb
@ -278,35 +278,60 @@ bool QCollator::ignorePunctuation() const
|
||||
}
|
||||
|
||||
/*!
|
||||
\fn int QCollator::compare(const QString &s1, const QString &s2) const
|
||||
\since 5.13
|
||||
\fn bool QCollator::operator()(QStringView s1, QStringView s2) const
|
||||
\internal
|
||||
*/
|
||||
|
||||
/*!
|
||||
\since 5.13
|
||||
\fn int QCollator::compare(QStringView s1, QStringView s2) const
|
||||
|
||||
Compares \a s1 with \a s2. Returns an integer less than, equal to, or greater than zero
|
||||
depending on whether \a s1 is smaller, equal or larger than \a s2.
|
||||
depending on whether \a s1 sorts before, with or after \a s2.
|
||||
*/
|
||||
|
||||
#if QT_STRINGVIEW_LEVEL < 2
|
||||
/*!
|
||||
\fn bool QCollator::operator()(const QString &s1, const QString &s2) const
|
||||
\internal
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const
|
||||
\overload
|
||||
|
||||
Compares \a s1 with \a s2. Returns an integer less than, equal to, or greater than zero
|
||||
depending on whether \a s1 is smaller, equal or larger than \a s2.
|
||||
depending on whether \a s1 sorts before, with or after \a s2.
|
||||
*/
|
||||
int QCollator::compare(const QString &s1, const QString &s2) const
|
||||
{
|
||||
return compare(QStringView(s1), QStringView(s2));
|
||||
}
|
||||
|
||||
/*!
|
||||
\overload
|
||||
|
||||
Compares \a s1 with \a s2. Returns an integer less than, equal to, or greater than zero
|
||||
depending on whether \a s1 sorts before, with or after \a s2.
|
||||
*/
|
||||
int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const
|
||||
{
|
||||
return compare(QStringView(s1), QStringView(s2));
|
||||
}
|
||||
|
||||
/*!
|
||||
\fn int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const
|
||||
\overload
|
||||
|
||||
Compares \a s1 with \a s2. \a len1 and \a len2 specify the length of the
|
||||
QChar arrays pointer to by \a s1 and \a s2.
|
||||
|
||||
Returns an integer less than, equal to, or greater than zero
|
||||
depending on whether \a s1 is smaller, equal or larger than \a s2.
|
||||
depending on whether \a s1 sorts before, with or after \a s2.
|
||||
*/
|
||||
int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const
|
||||
{
|
||||
return compare(QStringView(s1, len1), QStringView(s2, len2));
|
||||
}
|
||||
#endif // QT_STRINGVIEW_LEVEL < 2
|
||||
|
||||
/*!
|
||||
\fn QCollatorSortKey QCollator::sortKey(const QString &string) const
|
||||
|
@ -109,12 +109,18 @@ public:
|
||||
void setIgnorePunctuation(bool on);
|
||||
bool ignorePunctuation() const;
|
||||
|
||||
#if QT_STRINGVIEW_LEVEL < 2
|
||||
int compare(const QString &s1, const QString &s2) const;
|
||||
int compare(const QStringRef &s1, const QStringRef &s2) const;
|
||||
int compare(const QChar *s1, int len1, const QChar *s2, int len2) const;
|
||||
|
||||
bool operator()(const QString &s1, const QString &s2) const
|
||||
{ return compare(s1, s2) < 0; }
|
||||
#endif
|
||||
int compare(QStringView s1, QStringView s2) const;
|
||||
|
||||
bool operator()(QStringView s1, QStringView s2) const
|
||||
{ return compare(s1, s2) < 0; }
|
||||
|
||||
QCollatorSortKey sortKey(const QString &string) const;
|
||||
|
||||
|
@ -105,37 +105,20 @@ void QCollatorPrivate::cleanup()
|
||||
collator = nullptr;
|
||||
}
|
||||
|
||||
int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const
|
||||
int QCollator::compare(QStringView s1, QStringView s2) const
|
||||
{
|
||||
if (d->dirty)
|
||||
d->init();
|
||||
|
||||
if (d->collator)
|
||||
return ucol_strcoll(d->collator, (const UChar *)s1, len1, (const UChar *)s2, len2);
|
||||
if (d->collator) {
|
||||
return ucol_strcoll(d->collator,
|
||||
reinterpret_cast<const UChar *>(s1.data()), s1.size(),
|
||||
reinterpret_cast<const UChar *>(s2.data()), s2.size());
|
||||
}
|
||||
|
||||
return QString::compare_helper(s1, len1, s2, len2, d->caseSensitivity);
|
||||
}
|
||||
|
||||
int QCollator::compare(const QString &s1, const QString &s2) const
|
||||
{
|
||||
if (d->dirty)
|
||||
d->init();
|
||||
|
||||
if (d->collator)
|
||||
return compare(s1.constData(), s1.size(), s2.constData(), s2.size());
|
||||
|
||||
return QString::compare(s1, s2, d->caseSensitivity);
|
||||
}
|
||||
|
||||
int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const
|
||||
{
|
||||
if (d->dirty)
|
||||
d->init();
|
||||
|
||||
if (d->collator)
|
||||
return compare(s1.constData(), s1.size(), s2.constData(), s2.size());
|
||||
|
||||
return QStringRef::compare(s1, s2, d->caseSensitivity);
|
||||
return QString::compare_helper(s1.data(), s1.size(),
|
||||
s2.data(), s2.size(),
|
||||
d->caseSensitivity);
|
||||
}
|
||||
|
||||
QCollatorSortKey QCollator::sortKey(const QString &string) const
|
||||
|
@ -97,18 +97,18 @@ void QCollatorPrivate::cleanup()
|
||||
collator = 0;
|
||||
}
|
||||
|
||||
int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const
|
||||
int QCollator::compare(QStringView s1, QStringView s2) const
|
||||
{
|
||||
if (d->dirty)
|
||||
d->init();
|
||||
if (!d->collator)
|
||||
return QStringView(s1, len1).compare(QStringView(s2, len2), caseSensitivity());
|
||||
return s1.compare(s2, caseSensitivity());
|
||||
|
||||
SInt32 result;
|
||||
Boolean equivalent;
|
||||
UCCompareText(d->collator,
|
||||
reinterpret_cast<const UniChar *>(s1), len1,
|
||||
reinterpret_cast<const UniChar *>(s2), len2,
|
||||
reinterpret_cast<const UniChar *>(s1.data()), s1.size(),
|
||||
reinterpret_cast<const UniChar *>(s2.data()), s2.size(),
|
||||
&equivalent,
|
||||
&result);
|
||||
if (equivalent)
|
||||
@ -116,16 +116,6 @@ int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) con
|
||||
return result < 0 ? -1 : 1;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
QCollatorSortKey QCollator::sortKey(const QString &string) const
|
||||
{
|
||||
if (d->dirty)
|
||||
|
@ -65,7 +65,7 @@ void QCollatorPrivate::cleanup()
|
||||
{
|
||||
}
|
||||
|
||||
static void stringToWCharArray(QVarLengthArray<wchar_t> &ret, const QString &string)
|
||||
static void stringToWCharArray(QVarLengthArray<wchar_t> &ret, QStringView string)
|
||||
{
|
||||
ret.resize(string.length());
|
||||
int len = string.toWCharArray(ret.data());
|
||||
@ -73,12 +73,7 @@ static void stringToWCharArray(QVarLengthArray<wchar_t> &ret, const QString &str
|
||||
ret[len] = 0;
|
||||
}
|
||||
|
||||
int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const
|
||||
{
|
||||
return compare(QString::fromRawData(s1, len1), QString::fromRawData(s2, len2));
|
||||
}
|
||||
|
||||
int QCollator::compare(const QString &s1, const QString &s2) const
|
||||
int QCollator::compare(QStringView s1, QStringView s2) const
|
||||
{
|
||||
if (d->isC())
|
||||
return s1.compare(s2, caseSensitivity());
|
||||
@ -91,11 +86,6 @@ int QCollator::compare(const QString &s1, const QString &s2) const
|
||||
return std::wcscoll(array1.constData(), array2.constData());
|
||||
}
|
||||
|
||||
int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const
|
||||
{
|
||||
return compare(s1.toString(), s2.toString());
|
||||
}
|
||||
|
||||
QCollatorSortKey QCollator::sortKey(const QString &string) const
|
||||
{
|
||||
if (d->dirty)
|
||||
|
@ -85,11 +85,10 @@ 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 QString::compare_helper(s1, len1, s2, len2, d->caseSensitivity);
|
||||
return s1.compare(s2, d->caseSensitivity);
|
||||
|
||||
if (d->dirty)
|
||||
d->init();
|
||||
@ -101,25 +100,15 @@ int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) con
|
||||
|
||||
#ifndef USE_COMPARESTRINGEX
|
||||
return CompareString(d->localeID, d->collator,
|
||||
reinterpret_cast<const wchar_t*>(s1), len1,
|
||||
reinterpret_cast<const wchar_t*>(s2), len2) - 2;
|
||||
reinterpret_cast<const wchar_t*>(s1.data()), s1.size(),
|
||||
reinterpret_cast<const wchar_t*>(s2.data()), s2.size()) - 2;
|
||||
#else
|
||||
return CompareStringEx(LPCWSTR(d->localeName.utf16()), d->collator,
|
||||
reinterpret_cast<LPCWSTR>(s1), len1,
|
||||
reinterpret_cast<LPCWSTR>(s2), len2, NULL, NULL, 0) - 2;
|
||||
reinterpret_cast<LPCWSTR>(s1.data()), s1.size(),
|
||||
reinterpret_cast<LPCWSTR>(s2.data()), s2.size(), NULL, NULL, 0) - 2;
|
||||
#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());
|
||||
}
|
||||
|
||||
QCollatorSortKey QCollator::sortKey(const QString &string) const
|
||||
{
|
||||
if (d->dirty)
|
||||
|
Loading…
x
Reference in New Issue
Block a user