From a769ab62cbcf957d049c8da74f898527dd27b328 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Fri, 7 Dec 2018 11:52:59 +0100 Subject: 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 --- src/corelib/tools/qcollator.cpp | 39 ++++++++++++++++++++++++++++------- src/corelib/tools/qcollator.h | 6 ++++++ src/corelib/tools/qcollator_icu.cpp | 35 ++++++++----------------------- src/corelib/tools/qcollator_macx.cpp | 18 ++++------------ src/corelib/tools/qcollator_posix.cpp | 14 ++----------- src/corelib/tools/qcollator_win.cpp | 23 ++++++--------------- 6 files changed, 59 insertions(+), 76 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qcollator.cpp b/src/corelib/tools/qcollator.cpp index 76dcf35833..f2c9c61c99 100644 --- a/src/corelib/tools/qcollator.cpp +++ b/src/corelib/tools/qcollator.cpp @@ -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 diff --git a/src/corelib/tools/qcollator.h b/src/corelib/tools/qcollator.h index 6fa199cb0f..700de739bb 100644 --- a/src/corelib/tools/qcollator.h +++ b/src/corelib/tools/qcollator.h @@ -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; diff --git a/src/corelib/tools/qcollator_icu.cpp b/src/corelib/tools/qcollator_icu.cpp index ab45b9a1a1..eb0ba33676 100644 --- a/src/corelib/tools/qcollator_icu.cpp +++ b/src/corelib/tools/qcollator_icu.cpp @@ -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); - - 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()); + if (d->collator) { + return ucol_strcoll(d->collator, + reinterpret_cast(s1.data()), s1.size(), + reinterpret_cast(s2.data()), 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 diff --git a/src/corelib/tools/qcollator_macx.cpp b/src/corelib/tools/qcollator_macx.cpp index 42e67e0c12..9998ec4cda 100644 --- a/src/corelib/tools/qcollator_macx.cpp +++ b/src/corelib/tools/qcollator_macx.cpp @@ -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(s1), len1, - reinterpret_cast(s2), len2, + reinterpret_cast(s1.data()), s1.size(), + reinterpret_cast(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) diff --git a/src/corelib/tools/qcollator_posix.cpp b/src/corelib/tools/qcollator_posix.cpp index 81f97a02e1..9cbc539ebe 100644 --- a/src/corelib/tools/qcollator_posix.cpp +++ b/src/corelib/tools/qcollator_posix.cpp @@ -65,7 +65,7 @@ void QCollatorPrivate::cleanup() { } -static void stringToWCharArray(QVarLengthArray &ret, const QString &string) +static void stringToWCharArray(QVarLengthArray &ret, QStringView string) { ret.resize(string.length()); int len = string.toWCharArray(ret.data()); @@ -73,12 +73,7 @@ static void stringToWCharArray(QVarLengthArray &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) diff --git a/src/corelib/tools/qcollator_win.cpp b/src/corelib/tools/qcollator_win.cpp index 35142bb8b8..9cbdd96ee0 100644 --- a/src/corelib/tools/qcollator_win.cpp +++ b/src/corelib/tools/qcollator_win.cpp @@ -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(s1), len1, - reinterpret_cast(s2), len2) - 2; + reinterpret_cast(s1.data()), s1.size(), + reinterpret_cast(s2.data()), s2.size()) - 2; #else return CompareStringEx(LPCWSTR(d->localeName.utf16()), d->collator, - reinterpret_cast(s1), len1, - reinterpret_cast(s2), len2, NULL, NULL, 0) - 2; + reinterpret_cast(s1.data()), s1.size(), + reinterpret_cast(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) -- cgit v1.2.3