From d65ff871487bb39d26e7af765ce3214f034be82a Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 4 Oct 2015 20:48:43 +0800 Subject: [PATCH 1/5] Replace `naturalSort()` with `naturalCompare()`. --- src/base/utils/string.cpp | 79 +++++--------------------- src/base/utils/string.h | 6 +- src/gui/addnewtorrentdialog.cpp | 2 +- src/gui/properties/peerlistsortmodel.h | 23 +++----- src/gui/rss/automatedrssdownloader.cpp | 2 +- src/gui/search/searchsortmodel.cpp | 21 +++---- src/gui/torrentcontentfiltermodel.cpp | 28 ++++----- src/gui/transferlistfilterswidget.cpp | 28 ++++----- src/gui/transferlistsortmodel.cpp | 42 ++++++++------ 9 files changed, 82 insertions(+), 149 deletions(-) diff --git a/src/base/utils/string.cpp b/src/base/utils/string.cpp index 046b33d7e..7d43b6284 100644 --- a/src/base/utils/string.cpp +++ b/src/base/utils/string.cpp @@ -44,68 +44,6 @@ std::string Utils::String::toStdString(const QString &str) return std::string(utf8.constData(), utf8.length()); } -// uses lessThan comparison -bool Utils::String::naturalSort(const QString &left, const QString &right, bool &result) -{ - // Return value indicates if functions was successful - // result argument will contain actual comparison result if function was successful - int posL = 0; - int posR = 0; - do { - forever { - if (posL == left.size() || posR == right.size()) - return false; // No data - - QChar leftChar = left.at(posL); - QChar rightChar = right.at(posR); - bool leftCharIsDigit = leftChar.isDigit(); - bool rightCharIsDigit = rightChar.isDigit(); - if (leftCharIsDigit != rightCharIsDigit) - return false; // Digit positions mismatch - - if (leftCharIsDigit) - break; // Both are digit, break this loop and compare numbers - - if (leftChar != rightChar) - return false; // Strings' subsets before digit do not match - - ++posL; - ++posR; - } - - QString temp; - while (posL < left.size()) { - if (left.at(posL).isDigit()) - temp += left.at(posL); - else - break; - posL++; - } - int numL = temp.toInt(); - temp.clear(); - - while (posR < right.size()) { - if (right.at(posR).isDigit()) - temp += right.at(posR); - else - break; - posR++; - } - int numR = temp.toInt(); - - if (numL != numR) { - result = (numL < numR); - return true; - } - - // Strings + digits do match and we haven't hit string end - // Do another round - - } while (true); - - return false; -} - Utils::String::NaturalCompare::NaturalCompare() { #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) @@ -119,23 +57,25 @@ Utils::String::NaturalCompare::NaturalCompare() #endif } -bool Utils::String::NaturalCompare::operator()(const QString &l, const QString &r) +bool Utils::String::NaturalCompare::operator()(const QString &left, const QString &right) { + // case-insensitive comparison #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) #if defined(Q_OS_WIN) // Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7 if(QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7) - return lessThan(l, r); + return lessThan(left, right); #endif - return (m_collator.compare(l, r) < 0); + return (m_collator.compare(left, right) < 0); #else - return lessThan(l, r); + return lessThan(left, right); #endif } bool Utils::String::NaturalCompare::lessThan(const QString &left, const QString &right) { // Return value `false` indicates `right` should go before `left`, otherwise, after + // case-insensitive comparison int posL = 0; int posR = 0; while (true) { @@ -183,6 +123,13 @@ bool Utils::String::NaturalCompare::lessThan(const QString &left, const QString return false; } +bool Utils::String::naturalCompare(const QString &left, const QString &right) +{ + // provide a single `NaturalCompare` instance for easy use + static NaturalCompare nCmp; // this is thread-safe in C++11 (stated in spec 6.7.4) + return nCmp(left, right); +} + // to send numbers instead of strings with suffixes QString Utils::String::fromDouble(double n, int precision) { diff --git a/src/base/utils/string.h b/src/base/utils/string.h index 36b34e979..e379beadd 100644 --- a/src/base/utils/string.h +++ b/src/base/utils/string.h @@ -51,19 +51,19 @@ namespace Utils // Taken from https://crackstation.net/hashing-security.htm bool slowEquals(const QByteArray &a, const QByteArray &b); - bool naturalSort(const QString &left, const QString &right, bool &result); - class NaturalCompare { public: NaturalCompare(); - bool operator()(const QString &l, const QString &r); + bool operator()(const QString &left, const QString &right); bool lessThan(const QString &left, const QString &right); #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) private: QCollator m_collator; #endif }; + + bool naturalCompare(const QString &left, const QString &right); } } diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index fa441c727..3511ef73f 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -96,7 +96,7 @@ AddNewTorrentDialog::AddNewTorrentDialog(QWidget *parent) // Load categories QStringList categories = session->categories(); - std::sort(categories.begin(), categories.end(), Utils::String::NaturalCompare()); + std::sort(categories.begin(), categories.end(), Utils::String::naturalCompare); QString defaultCategory = settings()->loadValue(KEY_DEFAULTCATEGORY).toString(); if (!defaultCategory.isEmpty()) diff --git a/src/gui/properties/peerlistsortmodel.h b/src/gui/properties/peerlistsortmodel.h index d37a9ccd1..ac298a3da 100644 --- a/src/gui/properties/peerlistsortmodel.h +++ b/src/gui/properties/peerlistsortmodel.h @@ -43,21 +43,16 @@ public: protected: bool lessThan(const QModelIndex &left, const QModelIndex &right) const { - if (sortColumn() == PeerListDelegate::IP || sortColumn() == PeerListDelegate::CLIENT) { - QVariant vL = sourceModel()->data(left); - QVariant vR = sourceModel()->data(right); - if (!(vL.isValid() && vR.isValid())) - return QSortFilterProxyModel::lessThan(left, right); - Q_ASSERT(vL.isValid()); - Q_ASSERT(vR.isValid()); + switch (sortColumn()) { + case PeerListDelegate::IP: + case PeerListDelegate::CLIENT: { + QString vL = left.data().toString(); + QString vR = right.data().toString(); + return Utils::String::naturalCompare(vL, vR); + } + }; - bool res = false; - if (Utils::String::naturalSort(vL.toString(), vR.toString(), res)) - return res; - - return QSortFilterProxyModel::lessThan(left, right); - } - return QSortFilterProxyModel::lessThan(left, right); + return QSortFilterProxyModel::lessThan(left, right); } }; diff --git a/src/gui/rss/automatedrssdownloader.cpp b/src/gui/rss/automatedrssdownloader.cpp index 7721462a7..744229710 100644 --- a/src/gui/rss/automatedrssdownloader.cpp +++ b/src/gui/rss/automatedrssdownloader.cpp @@ -314,7 +314,7 @@ void AutomatedRssDownloader::initCategoryCombobox() { // Load torrent categories QStringList categories = BitTorrent::Session::instance()->categories(); - std::sort(categories.begin(), categories.end(), Utils::String::NaturalCompare()); + std::sort(categories.begin(), categories.end(), Utils::String::naturalCompare); ui->comboCategory->addItems(categories); } diff --git a/src/gui/search/searchsortmodel.cpp b/src/gui/search/searchsortmodel.cpp index 22cc20fd6..0052029eb 100644 --- a/src/gui/search/searchsortmodel.cpp +++ b/src/gui/search/searchsortmodel.cpp @@ -107,20 +107,17 @@ qint64 SearchSortModel::maxSize() const bool SearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const { - if ((sortColumn() == NAME) || (sortColumn() == ENGINE_URL)) { - QVariant vL = sourceModel()->data(left); - QVariant vR = sourceModel()->data(right); - if (!(vL.isValid() && vR.isValid())) - return base::lessThan(left, right); - Q_ASSERT(vL.isValid()); - Q_ASSERT(vR.isValid()); - - bool res = false; - if (Utils::String::naturalSort(vL.toString(), vR.toString(), res)) - return res; + switch (sortColumn()) { + case NAME: + case ENGINE_URL: { + QString vL = left.data().toString(); + QString vR = right.data().toString(); + return Utils::String::naturalCompare(vL, vR); } - return base::lessThan(left, right); + default: + return base::lessThan(left, right); + }; } bool SearchSortModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const diff --git a/src/gui/torrentcontentfiltermodel.cpp b/src/gui/torrentcontentfiltermodel.cpp index ac0a0edf5..fb4d849b1 100644 --- a/src/gui/torrentcontentfiltermodel.cpp +++ b/src/gui/torrentcontentfiltermodel.cpp @@ -82,28 +82,22 @@ bool TorrentContentFilterModel::filterAcceptsRow(int source_row, const QModelInd } bool TorrentContentFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const { - if (sortColumn() == NAME) { - QVariant vL = sourceModel()->data(left); - QVariant vR = sourceModel()->data(right); - if (!(vL.isValid() && vR.isValid())) - return QSortFilterProxyModel::lessThan(left, right); - Q_ASSERT(vL.isValid()); - Q_ASSERT(vR.isValid()); + switch (sortColumn()) { + case NAME: { // PropColumn::NAME + QString vL = left.data().toString(); + QString vR = right.data().toString(); + TorrentContentModelItem::ItemType leftType = m_model->itemType(m_model->index(left.row(), 0, left.parent())); + TorrentContentModelItem::ItemType rightType = m_model->itemType(m_model->index(right.row(), 0, right.parent())); - TorrentContentModelItem::ItemType leftType, rightType; - leftType = m_model->itemType(m_model->index(left.row(), 0, left.parent())); - rightType = m_model->itemType(m_model->index(right.row(), 0, right.parent())); - if (leftType == rightType) { - bool res = false; - if (Utils::String::naturalSort(vL.toString(), vR.toString(), res)) - return res; - return QSortFilterProxyModel::lessThan(left, right); - } + if (leftType == rightType) + return Utils::String::naturalCompare(vL, vR); else if (leftType == TorrentContentModelItem::FolderType && sortOrder() == Qt::AscendingOrder) return true; else - return false; + return false; } + }; + return QSortFilterProxyModel::lessThan(left, right); } diff --git a/src/gui/transferlistfilterswidget.cpp b/src/gui/transferlistfilterswidget.cpp index c83f48b87..a2da34141 100644 --- a/src/gui/transferlistfilterswidget.cpp +++ b/src/gui/transferlistfilterswidget.cpp @@ -237,17 +237,14 @@ void CategoryFiltersList::addItem(const QString &category, bool hasTorrent) if (exists) return; Q_ASSERT(count() >= 2); + int insPos = count(); for (int i = 2; i < count(); ++i) { - bool less = false; - if (!(Utils::String::naturalSort(category, item(i)->text(), less))) - less = (category.localeAwareCompare(item(i)->text()) < 0); - if (less) { - insertItem(i, categoryItem); - updateGeometry(); - return; + if (Utils::String::naturalCompare(category, item(i)->text())) { + insPos = i; + break; } } - QListWidget::addItem(categoryItem); + QListWidget::insertItem(insPos, categoryItem); updateGeometry(); } @@ -512,17 +509,14 @@ void TrackerFiltersList::addItem(const QString &tracker, const QString &hash) } Q_ASSERT(count() >= 4); - for (int i = 4; itext(), less))) - less = (host.localeAwareCompare(item(i)->text()) < 0); - if (less) { - insertItem(i, trackerItem); - updateGeometry(); - return; + int insPos = count(); + for (int i = 4; i < count(); ++i) { + if (Utils::String::naturalCompare(host, item(i)->text())) { + insPos = i; + break; } } - QListWidget::addItem(trackerItem); + QListWidget::insertItem(insPos, trackerItem); updateGeometry(); } diff --git a/src/gui/transferlistsortmodel.cpp b/src/gui/transferlistsortmodel.cpp index c69253666..06eeed220 100644 --- a/src/gui/transferlistsortmodel.cpp +++ b/src/gui/transferlistsortmodel.cpp @@ -73,21 +73,19 @@ void TransferListSortModel::disableTrackerFilter() bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const { - const int column = sortColumn(); - - if (column == TorrentModel::TR_NAME) { + switch (sortColumn()) { + case TorrentModel::TR_NAME: { QVariant vL = left.data(); QVariant vR = right.data(); if (!vL.isValid() || !vR.isValid() || (vL == vR)) return lowerPositionThan(left, right); - bool res = false; - if (Utils::String::naturalSort(vL.toString(), vR.toString(), res)) - return res; - - return QSortFilterProxyModel::lessThan(left, right); + return Utils::String::naturalCompare(vL.toString(), vR.toString()); } - else if (column == TorrentModel::TR_ADD_DATE || column == TorrentModel::TR_SEED_DATE || column == TorrentModel::TR_SEEN_COMPLETE_DATE) { + + case TorrentModel::TR_ADD_DATE: + case TorrentModel::TR_SEED_DATE: + case TorrentModel::TR_SEEN_COMPLETE_DATE: { QDateTime vL = left.data().toDateTime(); QDateTime vR = right.data().toDateTime(); @@ -97,10 +95,13 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex return vL < vR; } - else if (column == TorrentModel::TR_PRIORITY) { + + case TorrentModel::TR_PRIORITY: { return lowerPositionThan(left, right); } - else if (column == TorrentModel::TR_PEERS || column == TorrentModel::TR_SEEDS) { + + case TorrentModel::TR_SEEDS: + case TorrentModel::TR_PEERS: { int left_active = left.data().toInt(); int left_total = left.data(Qt::UserRole).toInt(); int right_active = right.data().toInt(); @@ -116,7 +117,8 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex return (left_active < right_active); } } - else if (column == TorrentModel::TR_ETA) { + + case TorrentModel::TR_ETA: { TorrentModel *model = qobject_cast(sourceModel()); const int prioL = model->data(model->index(left.row(), TorrentModel::TR_PRIORITY)).toInt(); const int prioR = model->data(model->index(right.row(), TorrentModel::TR_PRIORITY)).toInt(); @@ -164,7 +166,8 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex return !invalidL; } } - else if (column == TorrentModel::TR_LAST_ACTIVITY) { + + case TorrentModel::TR_LAST_ACTIVITY: { const qlonglong vL = left.data().toLongLong(); const qlonglong vR = right.data().toLongLong(); @@ -173,7 +176,8 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex return vL < vR; } - else if (column == TorrentModel::TR_RATIO_LIMIT) { + + case TorrentModel::TR_RATIO_LIMIT: { const qreal vL = left.data().toDouble(); const qreal vR = right.data().toDouble(); @@ -183,10 +187,12 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex return vL < vR; } - if (left.data() == right.data()) - return lowerPositionThan(left, right); - - return QSortFilterProxyModel::lessThan(left, right); + default: { + if (left.data() == right.data()) + return lowerPositionThan(left, right); + return QSortFilterProxyModel::lessThan(left, right); + } + }; } bool TransferListSortModel::lowerPositionThan(const QModelIndex &left, const QModelIndex &right) const From c3bde7c910fb3d6d1600150e7b576a1d7bd40339 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 11 Oct 2015 21:15:41 +0800 Subject: [PATCH 2/5] Sort labels with `naturalCompare()`. Closes #3919. --- src/gui/transferlistwidget.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/transferlistwidget.cpp b/src/gui/transferlistwidget.cpp index b2eaa7caf..d147f8c6c 100644 --- a/src/gui/transferlistwidget.cpp +++ b/src/gui/transferlistwidget.cpp @@ -769,7 +769,7 @@ void TransferListWidget::displayListMenu(const QPoint&) listMenu.addAction(&actionRename); // Category Menu QStringList categories = BitTorrent::Session::instance()->categories(); - std::sort(categories.begin(), categories.end(), Utils::String::NaturalCompare()); + std::sort(categories.begin(), categories.end(), Utils::String::naturalCompare); QList categoryActions; QMenu *categoryMenu = listMenu.addMenu(GuiIconProvider::instance()->getIcon("view-categories"), tr("Category")); categoryActions << categoryMenu->addAction(GuiIconProvider::instance()->getIcon("list-add"), tr("New...", "New category...")); From 626a224382414586a9df63f1d0c81a4168ae4c6c Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 12 Apr 2016 14:08:04 +0800 Subject: [PATCH 3/5] Minor code formatting --- src/base/utils/string.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/base/utils/string.cpp b/src/base/utils/string.cpp index 7d43b6284..d01799fe0 100644 --- a/src/base/utils/string.cpp +++ b/src/base/utils/string.cpp @@ -49,7 +49,7 @@ Utils::String::NaturalCompare::NaturalCompare() #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) #if defined(Q_OS_WIN) // Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7 - if(QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7) + if (QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7) return; #endif m_collator.setNumericMode(true); @@ -63,7 +63,7 @@ bool Utils::String::NaturalCompare::operator()(const QString &left, const QStrin #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) #if defined(Q_OS_WIN) // Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7 - if(QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7) + if (QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7) return lessThan(left, right); #endif return (m_collator.compare(left, right) < 0); @@ -80,7 +80,7 @@ bool Utils::String::NaturalCompare::lessThan(const QString &left, const QString int posR = 0; while (true) { while (true) { - if (posL == left.size() || posR == right.size()) + if ((posL == left.size()) || (posR == right.size())) return (left.size() < right.size()); // when a shorter string is another string's prefix, shorter string place before longer string QChar leftChar = left[posL].toLower(); From d25430f377c2a68011155e9fe41a0d63606e82d8 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 18 Apr 2016 14:18:30 +0800 Subject: [PATCH 4/5] Cleanup headers Move `class NaturalCompare` to .cpp file --- src/base/utils/string.cpp | 50 ++++++++++++++++++++++++++------------- src/base/utils/string.h | 18 +------------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/base/utils/string.cpp b/src/base/utils/string.cpp index d01799fe0..ccd35940c 100644 --- a/src/base/utils/string.cpp +++ b/src/base/utils/string.cpp @@ -27,24 +27,31 @@ * exception statement from your version. */ -#include -#include -#include -#include #include "string.h" -QString Utils::String::fromStdString(const std::string &str) -{ - return QString::fromUtf8(str.c_str()); -} +#include -std::string Utils::String::toStdString(const QString &str) -{ - QByteArray utf8 = str.toUtf8(); - return std::string(utf8.constData(), utf8.length()); -} +#include +#include +#include +#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) +#include +#endif -Utils::String::NaturalCompare::NaturalCompare() +class NaturalCompare +{ +public: + NaturalCompare(); + bool operator()(const QString &left, const QString &right); + bool lessThan(const QString &left, const QString &right); + +private: +#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) + QCollator m_collator; +#endif +}; + +NaturalCompare::NaturalCompare() { #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) #if defined(Q_OS_WIN) @@ -57,7 +64,7 @@ Utils::String::NaturalCompare::NaturalCompare() #endif } -bool Utils::String::NaturalCompare::operator()(const QString &left, const QString &right) +bool NaturalCompare::operator()(const QString &left, const QString &right) { // case-insensitive comparison #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) @@ -72,7 +79,7 @@ bool Utils::String::NaturalCompare::operator()(const QString &left, const QStrin #endif } -bool Utils::String::NaturalCompare::lessThan(const QString &left, const QString &right) +bool NaturalCompare::lessThan(const QString &left, const QString &right) { // Return value `false` indicates `right` should go before `left`, otherwise, after // case-insensitive comparison @@ -123,6 +130,17 @@ bool Utils::String::NaturalCompare::lessThan(const QString &left, const QString return false; } +QString Utils::String::fromStdString(const std::string &str) +{ + return QString::fromUtf8(str.c_str()); +} + +std::string Utils::String::toStdString(const QString &str) +{ + QByteArray utf8 = str.toUtf8(); + return std::string(utf8.constData(), utf8.length()); +} + bool Utils::String::naturalCompare(const QString &left, const QString &right) { // provide a single `NaturalCompare` instance for easy use diff --git a/src/base/utils/string.h b/src/base/utils/string.h index e379beadd..9a3c66843 100644 --- a/src/base/utils/string.h +++ b/src/base/utils/string.h @@ -31,13 +31,9 @@ #define UTILS_STRING_H #include -#include -#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) -#include -#endif -class QString; class QByteArray; +class QString; namespace Utils { @@ -51,18 +47,6 @@ namespace Utils // Taken from https://crackstation.net/hashing-security.htm bool slowEquals(const QByteArray &a, const QByteArray &b); - class NaturalCompare - { - public: - NaturalCompare(); - bool operator()(const QString &left, const QString &right); - bool lessThan(const QString &left, const QString &right); -#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) - private: - QCollator m_collator; -#endif - }; - bool naturalCompare(const QString &left, const QString &right); } } From 5906a4a2dea9df48f0a03bc8cbb03c312322f489 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 4 May 2016 17:15:58 +0800 Subject: [PATCH 5/5] Add case-sensitive & case-insensitive natural sort helper function Fix helper function not being thread-safe Use QBT_USES_QT5 define --- src/base/utils/string.cpp | 191 ++++++++++++++----------- src/base/utils/string.h | 3 +- src/gui/addnewtorrentdialog.cpp | 2 +- src/gui/properties/peerlistsortmodel.h | 2 +- src/gui/rss/automatedrssdownloader.cpp | 2 +- src/gui/search/searchsortmodel.cpp | 2 +- src/gui/torrentcontentfiltermodel.cpp | 2 +- src/gui/transferlistfilterswidget.cpp | 4 +- src/gui/transferlistsortmodel.cpp | 2 +- src/gui/transferlistwidget.cpp | 2 +- 10 files changed, 118 insertions(+), 94 deletions(-) diff --git a/src/base/utils/string.cpp b/src/base/utils/string.cpp index ccd35940c..37a00713e 100644 --- a/src/base/utils/string.cpp +++ b/src/base/utils/string.cpp @@ -34,100 +34,130 @@ #include #include #include -#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) +#ifdef QBT_USES_QT5 #include #endif - -class NaturalCompare -{ -public: - NaturalCompare(); - bool operator()(const QString &left, const QString &right); - bool lessThan(const QString &left, const QString &right); - -private: -#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) - QCollator m_collator; +#ifdef Q_OS_MAC +#include #endif -}; -NaturalCompare::NaturalCompare() +namespace { -#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) + class NaturalCompare + { + public: + explicit NaturalCompare(const bool caseSensitive = true) + : m_caseSensitive(caseSensitive) + { +#ifdef QBT_USES_QT5 #if defined(Q_OS_WIN) - // Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7 - if (QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7) - return; + // Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7 + if (QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7) + return; #endif - m_collator.setNumericMode(true); - m_collator.setCaseSensitivity(Qt::CaseInsensitive); + m_collator.setNumericMode(true); + m_collator.setCaseSensitivity(caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); #endif -} - -bool NaturalCompare::operator()(const QString &left, const QString &right) -{ - // case-insensitive comparison -#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) -#if defined(Q_OS_WIN) - // Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7 - if (QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7) - return lessThan(left, right); -#endif - return (m_collator.compare(left, right) < 0); -#else - return lessThan(left, right); -#endif -} - -bool NaturalCompare::lessThan(const QString &left, const QString &right) -{ - // Return value `false` indicates `right` should go before `left`, otherwise, after - // case-insensitive comparison - int posL = 0; - int posR = 0; - while (true) { - while (true) { - if ((posL == left.size()) || (posR == right.size())) - return (left.size() < right.size()); // when a shorter string is another string's prefix, shorter string place before longer string - - QChar leftChar = left[posL].toLower(); - QChar rightChar = right[posR].toLower(); - if (leftChar == rightChar) - ; // compare next character - else if (leftChar.isDigit() && rightChar.isDigit()) - break; // Both are digits, break this loop and compare numbers - else - return leftChar < rightChar; - - ++posL; - ++posR; } - int startL = posL; - while ((posL < left.size()) && left[posL].isDigit()) - ++posL; -#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) - int numL = left.midRef(startL, posL - startL).toInt(); + bool operator()(const QString &left, const QString &right) const + { +#ifdef QBT_USES_QT5 +#if defined(Q_OS_WIN) + // Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7 + if (QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7) + return lessThan(left, right); +#endif + return (m_collator.compare(left, right) < 0); #else - int numL = left.mid(startL, posL - startL).toInt(); + return lessThan(left, right); +#endif + } + + bool lessThan(const QString &left, const QString &right) const + { + // Return value `false` indicates `right` should go before `left`, otherwise, after + int posL = 0; + int posR = 0; + while (true) { + while (true) { + if ((posL == left.size()) || (posR == right.size())) + return (left.size() < right.size()); // when a shorter string is another string's prefix, shorter string place before longer string + + QChar leftChar = m_caseSensitive ? left[posL] : left[posL].toLower(); + QChar rightChar = m_caseSensitive ? right[posR] : right[posR].toLower(); + if (leftChar == rightChar) + ; // compare next character + else if (leftChar.isDigit() && rightChar.isDigit()) + break; // Both are digits, break this loop and compare numbers + else + return leftChar < rightChar; + + ++posL; + ++posR; + } + + int startL = posL; + while ((posL < left.size()) && left[posL].isDigit()) + ++posL; +#ifdef QBT_USES_QT5 + int numL = left.midRef(startL, posL - startL).toInt(); +#else + int numL = left.mid(startL, posL - startL).toInt(); #endif - int startR = posR; - while ((posR < right.size()) && right[posR].isDigit()) - ++posR; -#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) - int numR = right.midRef(startR, posR - startR).toInt(); + int startR = posR; + while ((posR < right.size()) && right[posR].isDigit()) + ++posR; +#ifdef QBT_USES_QT5 + int numR = right.midRef(startR, posR - startR).toInt(); #else - int numR = right.mid(startR, posR - startR).toInt(); + int numR = right.mid(startR, posR - startR).toInt(); #endif - if (numL != numR) - return (numL < numR); + if (numL != numR) + return (numL < numR); - // Strings + digits do match and we haven't hit string end - // Do another round - } - return false; + // Strings + digits do match and we haven't hit string end + // Do another round + } + return false; + } + + private: +#ifdef QBT_USES_QT5 + QCollator m_collator; +#endif + const bool m_caseSensitive; + }; +} + +bool Utils::String::naturalCompareCaseSensitive(const QString &left, const QString &right) +{ + // provide a single `NaturalCompare` instance for easy use + // https://doc.qt.io/qt-5/threads-reentrancy.html +#ifdef Q_OS_MAC // workaround for Apple xcode: https://stackoverflow.com/a/29929949 + static QThreadStorage nCmp; + if (!nCmp.hasLocalData()) nCmp.setLocalData(NaturalCompare(true)); + return (nCmp.localData())(left, right); +#else + thread_local NaturalCompare nCmp(true); + return nCmp(left, right); +#endif +} + +bool Utils::String::naturalCompareCaseInsensitive(const QString &left, const QString &right) +{ + // provide a single `NaturalCompare` instance for easy use + // https://doc.qt.io/qt-5/threads-reentrancy.html +#ifdef Q_OS_MAC // workaround for Apple xcode: https://stackoverflow.com/a/29929949 + static QThreadStorage nCmp; + if (!nCmp.hasLocalData()) nCmp.setLocalData(NaturalCompare(false)); + return (nCmp.localData())(left, right); +#else + thread_local NaturalCompare nCmp(false); + return nCmp(left, right); +#endif } QString Utils::String::fromStdString(const std::string &str) @@ -141,13 +171,6 @@ std::string Utils::String::toStdString(const QString &str) return std::string(utf8.constData(), utf8.length()); } -bool Utils::String::naturalCompare(const QString &left, const QString &right) -{ - // provide a single `NaturalCompare` instance for easy use - static NaturalCompare nCmp; // this is thread-safe in C++11 (stated in spec 6.7.4) - return nCmp(left, right); -} - // to send numbers instead of strings with suffixes QString Utils::String::fromDouble(double n, int precision) { diff --git a/src/base/utils/string.h b/src/base/utils/string.h index 9a3c66843..8ca2f2e45 100644 --- a/src/base/utils/string.h +++ b/src/base/utils/string.h @@ -47,7 +47,8 @@ namespace Utils // Taken from https://crackstation.net/hashing-security.htm bool slowEquals(const QByteArray &a, const QByteArray &b); - bool naturalCompare(const QString &left, const QString &right); + bool naturalCompareCaseSensitive(const QString &left, const QString &right); + bool naturalCompareCaseInsensitive(const QString &left, const QString &right); } } diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index 3511ef73f..a689c0b86 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -96,7 +96,7 @@ AddNewTorrentDialog::AddNewTorrentDialog(QWidget *parent) // Load categories QStringList categories = session->categories(); - std::sort(categories.begin(), categories.end(), Utils::String::naturalCompare); + std::sort(categories.begin(), categories.end(), Utils::String::naturalCompareCaseInsensitive); QString defaultCategory = settings()->loadValue(KEY_DEFAULTCATEGORY).toString(); if (!defaultCategory.isEmpty()) diff --git a/src/gui/properties/peerlistsortmodel.h b/src/gui/properties/peerlistsortmodel.h index ac298a3da..729d92bfa 100644 --- a/src/gui/properties/peerlistsortmodel.h +++ b/src/gui/properties/peerlistsortmodel.h @@ -48,7 +48,7 @@ protected: case PeerListDelegate::CLIENT: { QString vL = left.data().toString(); QString vR = right.data().toString(); - return Utils::String::naturalCompare(vL, vR); + return Utils::String::naturalCompareCaseSensitive(vL, vR); } }; diff --git a/src/gui/rss/automatedrssdownloader.cpp b/src/gui/rss/automatedrssdownloader.cpp index 744229710..4396d106e 100644 --- a/src/gui/rss/automatedrssdownloader.cpp +++ b/src/gui/rss/automatedrssdownloader.cpp @@ -314,7 +314,7 @@ void AutomatedRssDownloader::initCategoryCombobox() { // Load torrent categories QStringList categories = BitTorrent::Session::instance()->categories(); - std::sort(categories.begin(), categories.end(), Utils::String::naturalCompare); + std::sort(categories.begin(), categories.end(), Utils::String::naturalCompareCaseInsensitive); ui->comboCategory->addItems(categories); } diff --git a/src/gui/search/searchsortmodel.cpp b/src/gui/search/searchsortmodel.cpp index 0052029eb..e27af9fc8 100644 --- a/src/gui/search/searchsortmodel.cpp +++ b/src/gui/search/searchsortmodel.cpp @@ -112,7 +112,7 @@ bool SearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right case ENGINE_URL: { QString vL = left.data().toString(); QString vR = right.data().toString(); - return Utils::String::naturalCompare(vL, vR); + return Utils::String::naturalCompareCaseSensitive(vL, vR); } default: diff --git a/src/gui/torrentcontentfiltermodel.cpp b/src/gui/torrentcontentfiltermodel.cpp index fb4d849b1..bb957c92d 100644 --- a/src/gui/torrentcontentfiltermodel.cpp +++ b/src/gui/torrentcontentfiltermodel.cpp @@ -90,7 +90,7 @@ bool TorrentContentFilterModel::lessThan(const QModelIndex &left, const QModelIn TorrentContentModelItem::ItemType rightType = m_model->itemType(m_model->index(right.row(), 0, right.parent())); if (leftType == rightType) - return Utils::String::naturalCompare(vL, vR); + return Utils::String::naturalCompareCaseSensitive(vL, vR); else if (leftType == TorrentContentModelItem::FolderType && sortOrder() == Qt::AscendingOrder) return true; else diff --git a/src/gui/transferlistfilterswidget.cpp b/src/gui/transferlistfilterswidget.cpp index a2da34141..eaa8e9d01 100644 --- a/src/gui/transferlistfilterswidget.cpp +++ b/src/gui/transferlistfilterswidget.cpp @@ -239,7 +239,7 @@ void CategoryFiltersList::addItem(const QString &category, bool hasTorrent) Q_ASSERT(count() >= 2); int insPos = count(); for (int i = 2; i < count(); ++i) { - if (Utils::String::naturalCompare(category, item(i)->text())) { + if (Utils::String::naturalCompareCaseSensitive(category, item(i)->text())) { insPos = i; break; } @@ -511,7 +511,7 @@ void TrackerFiltersList::addItem(const QString &tracker, const QString &hash) Q_ASSERT(count() >= 4); int insPos = count(); for (int i = 4; i < count(); ++i) { - if (Utils::String::naturalCompare(host, item(i)->text())) { + if (Utils::String::naturalCompareCaseSensitive(host, item(i)->text())) { insPos = i; break; } diff --git a/src/gui/transferlistsortmodel.cpp b/src/gui/transferlistsortmodel.cpp index 06eeed220..2058ce91f 100644 --- a/src/gui/transferlistsortmodel.cpp +++ b/src/gui/transferlistsortmodel.cpp @@ -80,7 +80,7 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex if (!vL.isValid() || !vR.isValid() || (vL == vR)) return lowerPositionThan(left, right); - return Utils::String::naturalCompare(vL.toString(), vR.toString()); + return Utils::String::naturalCompareCaseSensitive(vL.toString(), vR.toString()); } case TorrentModel::TR_ADD_DATE: diff --git a/src/gui/transferlistwidget.cpp b/src/gui/transferlistwidget.cpp index d147f8c6c..c7e25409e 100644 --- a/src/gui/transferlistwidget.cpp +++ b/src/gui/transferlistwidget.cpp @@ -769,7 +769,7 @@ void TransferListWidget::displayListMenu(const QPoint&) listMenu.addAction(&actionRename); // Category Menu QStringList categories = BitTorrent::Session::instance()->categories(); - std::sort(categories.begin(), categories.end(), Utils::String::naturalCompare); + std::sort(categories.begin(), categories.end(), Utils::String::naturalCompareCaseInsensitive); QList categoryActions; QMenu *categoryMenu = listMenu.addMenu(GuiIconProvider::instance()->getIcon("view-categories"), tr("Category")); categoryActions << categoryMenu->addAction(GuiIconProvider::instance()->getIcon("list-add"), tr("New...", "New category..."));