Add case-sensitive & case-insensitive natural sort helper function

Fix helper function not being thread-safe
Use QBT_USES_QT5 define
This commit is contained in:
Chocobo1 2016-05-04 17:15:58 +08:00
parent d25430f377
commit 5906a4a2de
10 changed files with 118 additions and 94 deletions

View File

@ -34,100 +34,130 @@
#include <QByteArray> #include <QByteArray>
#include <QtGlobal> #include <QtGlobal>
#include <QLocale> #include <QLocale>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) #ifdef QBT_USES_QT5
#include <QCollator> #include <QCollator>
#endif #endif
#ifdef Q_OS_MAC
class NaturalCompare #include <QThreadStorage>
{
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 #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) #if defined(Q_OS_WIN)
// Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7 // 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; return;
#endif #endif
m_collator.setNumericMode(true); m_collator.setNumericMode(true);
m_collator.setCaseSensitivity(Qt::CaseInsensitive); m_collator.setCaseSensitivity(caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
#endif #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; bool operator()(const QString &left, const QString &right) const
while ((posL < left.size()) && left[posL].isDigit()) {
++posL; #ifdef QBT_USES_QT5
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) #if defined(Q_OS_WIN)
int numL = left.midRef(startL, posL - startL).toInt(); // 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 #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 #endif
int startR = posR; int startR = posR;
while ((posR < right.size()) && right[posR].isDigit()) while ((posR < right.size()) && right[posR].isDigit())
++posR; ++posR;
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) #ifdef QBT_USES_QT5
int numR = right.midRef(startR, posR - startR).toInt(); int numR = right.midRef(startR, posR - startR).toInt();
#else #else
int numR = right.mid(startR, posR - startR).toInt(); int numR = right.mid(startR, posR - startR).toInt();
#endif #endif
if (numL != numR) if (numL != numR)
return (numL < numR); return (numL < numR);
// Strings + digits do match and we haven't hit string end // Strings + digits do match and we haven't hit string end
// Do another round // Do another round
} }
return false; 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<NaturalCompare> 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<NaturalCompare> 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) 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()); 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 // to send numbers instead of strings with suffixes
QString Utils::String::fromDouble(double n, int precision) QString Utils::String::fromDouble(double n, int precision)
{ {

View File

@ -47,7 +47,8 @@ namespace Utils
// Taken from https://crackstation.net/hashing-security.htm // Taken from https://crackstation.net/hashing-security.htm
bool slowEquals(const QByteArray &a, const QByteArray &b); 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);
} }
} }

View File

@ -96,7 +96,7 @@ AddNewTorrentDialog::AddNewTorrentDialog(QWidget *parent)
// Load categories // Load categories
QStringList categories = session->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(); QString defaultCategory = settings()->loadValue(KEY_DEFAULTCATEGORY).toString();
if (!defaultCategory.isEmpty()) if (!defaultCategory.isEmpty())

View File

@ -48,7 +48,7 @@ protected:
case PeerListDelegate::CLIENT: { case PeerListDelegate::CLIENT: {
QString vL = left.data().toString(); QString vL = left.data().toString();
QString vR = right.data().toString(); QString vR = right.data().toString();
return Utils::String::naturalCompare(vL, vR); return Utils::String::naturalCompareCaseSensitive(vL, vR);
} }
}; };

View File

@ -314,7 +314,7 @@ void AutomatedRssDownloader::initCategoryCombobox()
{ {
// Load torrent categories // Load torrent categories
QStringList categories = BitTorrent::Session::instance()->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); ui->comboCategory->addItems(categories);
} }

View File

@ -112,7 +112,7 @@ bool SearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right
case ENGINE_URL: { case ENGINE_URL: {
QString vL = left.data().toString(); QString vL = left.data().toString();
QString vR = right.data().toString(); QString vR = right.data().toString();
return Utils::String::naturalCompare(vL, vR); return Utils::String::naturalCompareCaseSensitive(vL, vR);
} }
default: default:

View File

@ -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())); TorrentContentModelItem::ItemType rightType = m_model->itemType(m_model->index(right.row(), 0, right.parent()));
if (leftType == rightType) if (leftType == rightType)
return Utils::String::naturalCompare(vL, vR); return Utils::String::naturalCompareCaseSensitive(vL, vR);
else if (leftType == TorrentContentModelItem::FolderType && sortOrder() == Qt::AscendingOrder) else if (leftType == TorrentContentModelItem::FolderType && sortOrder() == Qt::AscendingOrder)
return true; return true;
else else

View File

@ -239,7 +239,7 @@ void CategoryFiltersList::addItem(const QString &category, bool hasTorrent)
Q_ASSERT(count() >= 2); Q_ASSERT(count() >= 2);
int insPos = count(); int insPos = count();
for (int i = 2; i < count(); ++i) { 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; insPos = i;
break; break;
} }
@ -511,7 +511,7 @@ void TrackerFiltersList::addItem(const QString &tracker, const QString &hash)
Q_ASSERT(count() >= 4); Q_ASSERT(count() >= 4);
int insPos = count(); int insPos = count();
for (int i = 4; i < count(); ++i) { 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; insPos = i;
break; break;
} }

View File

@ -80,7 +80,7 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex
if (!vL.isValid() || !vR.isValid() || (vL == vR)) if (!vL.isValid() || !vR.isValid() || (vL == vR))
return lowerPositionThan(left, right); 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: case TorrentModel::TR_ADD_DATE:

View File

@ -769,7 +769,7 @@ void TransferListWidget::displayListMenu(const QPoint&)
listMenu.addAction(&actionRename); listMenu.addAction(&actionRename);
// Category Menu // Category Menu
QStringList categories = BitTorrent::Session::instance()->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);
QList<QAction*> categoryActions; QList<QAction*> categoryActions;
QMenu *categoryMenu = listMenu.addMenu(GuiIconProvider::instance()->getIcon("view-categories"), tr("Category")); QMenu *categoryMenu = listMenu.addMenu(GuiIconProvider::instance()->getIcon("view-categories"), tr("Category"));
categoryActions << categoryMenu->addAction(GuiIconProvider::instance()->getIcon("list-add"), tr("New...", "New category...")); categoryActions << categoryMenu->addAction(GuiIconProvider::instance()->getIcon("list-add"), tr("New...", "New category..."));