|
|
|
@ -34,100 +34,130 @@
@@ -34,100 +34,130 @@
|
|
|
|
|
#include <QByteArray> |
|
|
|
|
#include <QtGlobal> |
|
|
|
|
#include <QLocale> |
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) |
|
|
|
|
#ifdef QBT_USES_QT5 |
|
|
|
|
#include <QCollator> |
|
|
|
|
#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 <QThreadStorage> |
|
|
|
|
#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)) |
|
|
|
|
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); |
|
|
|
|
// 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); |
|
|
|
|
return (m_collator.compare(left, right) < 0); |
|
|
|
|
#else |
|
|
|
|
return lessThan(left, right); |
|
|
|
|
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 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(); |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Strings + digits do match and we haven't hit string end
|
|
|
|
|
// Do another round
|
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
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) |
|
|
|
@ -141,13 +171,6 @@ std::string Utils::String::toStdString(const QString &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) |
|
|
|
|
{ |
|
|
|
|