2006-09-30 20:02:39 +04:00
|
|
|
/*
|
2015-04-19 18:17:47 +03:00
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
|
|
|
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
|
|
|
|
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
|
2006-09-30 20:02:39 +04:00
|
|
|
*
|
2007-07-14 18:31:59 +04:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2006-09-30 20:02:39 +04:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-14 18:31:59 +04:00
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
2009-04-05 21:00:55 +04:00
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
2006-09-30 20:02:39 +04:00
|
|
|
*/
|
|
|
|
|
2016-04-18 09:18:30 +03:00
|
|
|
#include "string.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
2017-01-19 15:10:09 +03:00
|
|
|
#include <QCollator>
|
2015-05-06 14:53:27 +03:00
|
|
|
#include <QLocale>
|
2017-02-11 08:33:18 +03:00
|
|
|
#include <QRegExp>
|
2017-11-30 12:10:30 +03:00
|
|
|
#include <QtGlobal>
|
2020-07-21 13:56:07 +03:00
|
|
|
|
|
|
|
#if defined(Q_OS_MACOS) || defined(__MINGW32__)
|
|
|
|
#define QBT_USES_QTHREADSTORAGE
|
2016-05-04 12:15:58 +03:00
|
|
|
#include <QThreadStorage>
|
2016-04-18 09:18:30 +03:00
|
|
|
#endif
|
2015-05-06 14:53:27 +03:00
|
|
|
|
2020-07-21 13:56:07 +03:00
|
|
|
#include "base/tristatebool.h"
|
2017-10-14 16:27:21 +03:00
|
|
|
|
2016-05-04 12:15:58 +03:00
|
|
|
namespace
|
2015-06-20 10:06:49 +03:00
|
|
|
{
|
2016-05-04 12:15:58 +03:00
|
|
|
class NaturalCompare
|
|
|
|
{
|
|
|
|
public:
|
2017-11-30 12:10:30 +03:00
|
|
|
explicit NaturalCompare(const Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive)
|
|
|
|
: m_caseSensitivity(caseSensitivity)
|
2016-05-04 12:15:58 +03:00
|
|
|
{
|
2017-11-30 12:10:30 +03:00
|
|
|
#ifdef Q_OS_WIN
|
2016-06-08 22:53:59 +03:00
|
|
|
// Without ICU library, QCollator uses the native API on Windows 7+. But that API
|
|
|
|
// sorts older versions of μTorrent differently than the newer ones because the
|
|
|
|
// 'μ' character is encoded differently and the native API can't cope with that.
|
|
|
|
// So default to using our custom natural sorting algorithm instead.
|
|
|
|
// See #5238 and #5240
|
2017-11-30 12:10:30 +03:00
|
|
|
// Without ICU library, QCollator doesn't support `setNumericMode(true)` on an OS older than Win7
|
|
|
|
#else
|
2016-05-04 12:15:58 +03:00
|
|
|
m_collator.setNumericMode(true);
|
2017-11-30 12:10:30 +03:00
|
|
|
m_collator.setCaseSensitivity(caseSensitivity);
|
|
|
|
#endif
|
2016-05-04 12:15:58 +03:00
|
|
|
}
|
2015-06-20 10:06:49 +03:00
|
|
|
|
2017-11-30 12:10:30 +03:00
|
|
|
int operator()(const QString &left, const QString &right) const
|
2016-05-04 12:15:58 +03:00
|
|
|
{
|
2017-11-30 12:10:30 +03:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
return compare(left, right);
|
|
|
|
#else
|
|
|
|
return m_collator.compare(left, right);
|
2015-06-20 10:06:49 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-11-30 12:10:30 +03:00
|
|
|
private:
|
|
|
|
int compare(const QString &left, const QString &right) const
|
2016-05-04 12:15:58 +03:00
|
|
|
{
|
2017-11-30 12:10:30 +03:00
|
|
|
// Return value <0: `left` is smaller than `right`
|
|
|
|
// Return value >0: `left` is greater than `right`
|
|
|
|
// Return value =0: both strings are equal
|
|
|
|
|
2016-05-04 12:15:58 +03:00
|
|
|
int posL = 0;
|
|
|
|
int posR = 0;
|
2020-11-16 10:02:11 +03:00
|
|
|
while (true)
|
|
|
|
{
|
2017-11-30 12:10:30 +03:00
|
|
|
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
|
2016-05-04 12:15:58 +03:00
|
|
|
|
2017-11-30 12:10:30 +03:00
|
|
|
const QChar leftChar = (m_caseSensitivity == Qt::CaseSensitive) ? left[posL] : left[posL].toLower();
|
|
|
|
const QChar rightChar = (m_caseSensitivity == Qt::CaseSensitive) ? right[posR] : right[posR].toLower();
|
2017-12-21 16:01:13 +03:00
|
|
|
// Compare only non-digits.
|
|
|
|
// Numbers should be compared as a whole
|
|
|
|
// otherwise the string->int conversion can yield a wrong value
|
2020-11-16 10:02:11 +03:00
|
|
|
if ((leftChar == rightChar) && !leftChar.isDigit())
|
|
|
|
{
|
2017-11-30 12:10:30 +03:00
|
|
|
// compare next character
|
2016-05-04 12:15:58 +03:00
|
|
|
++posL;
|
|
|
|
++posR;
|
|
|
|
}
|
2020-11-16 10:02:11 +03:00
|
|
|
else if (leftChar.isDigit() && rightChar.isDigit())
|
|
|
|
{
|
2017-11-30 12:10:30 +03:00
|
|
|
// Both are digits, compare the numbers
|
2019-05-28 17:50:48 +03:00
|
|
|
|
|
|
|
const auto numberView = [](const QString &str, int &pos) -> QStringRef
|
2017-11-30 12:10:30 +03:00
|
|
|
{
|
|
|
|
const int start = pos;
|
|
|
|
while ((pos < str.size()) && str[pos].isDigit())
|
|
|
|
++pos;
|
2019-05-28 17:50:48 +03:00
|
|
|
return str.midRef(start, (pos - start));
|
2017-11-30 12:10:30 +03:00
|
|
|
};
|
|
|
|
|
2019-05-28 17:50:48 +03:00
|
|
|
const QStringRef numViewL = numberView(left, posL);
|
|
|
|
const QStringRef numViewR = numberView(right, posR);
|
|
|
|
|
|
|
|
if (numViewL.length() != numViewR.length())
|
|
|
|
return (numViewL.length() - numViewR.length());
|
|
|
|
|
|
|
|
// both string/view has the same length
|
2020-11-16 10:02:11 +03:00
|
|
|
for (int i = 0; i < numViewL.length(); ++i)
|
|
|
|
{
|
2019-05-28 17:50:48 +03:00
|
|
|
const QChar numL = numViewL[i];
|
|
|
|
const QChar numR = numViewR[i];
|
|
|
|
|
|
|
|
if (numL != numR)
|
|
|
|
return (numL.unicode() - numR.unicode());
|
|
|
|
}
|
2017-11-30 12:10:30 +03:00
|
|
|
|
|
|
|
// String + digits do match and we haven't hit the end of both strings
|
|
|
|
// then continue to consume the remainings
|
|
|
|
}
|
2020-11-16 10:02:11 +03:00
|
|
|
else
|
|
|
|
{
|
2017-11-30 12:10:30 +03:00
|
|
|
return (leftChar.unicode() - rightChar.unicode());
|
|
|
|
}
|
2016-05-04 12:15:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QCollator m_collator;
|
2017-11-30 12:10:30 +03:00
|
|
|
const Qt::CaseSensitivity m_caseSensitivity;
|
2016-05-04 12:15:58 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-30 12:10:30 +03:00
|
|
|
int Utils::String::naturalCompare(const QString &left, const QString &right, const Qt::CaseSensitivity caseSensitivity)
|
2016-05-04 12:15:58 +03:00
|
|
|
{
|
|
|
|
// provide a single `NaturalCompare` instance for easy use
|
|
|
|
// https://doc.qt.io/qt-5/threads-reentrancy.html
|
2020-11-16 10:02:11 +03:00
|
|
|
if (caseSensitivity == Qt::CaseSensitive)
|
|
|
|
{
|
2020-07-21 13:56:07 +03:00
|
|
|
#ifdef QBT_USES_QTHREADSTORAGE
|
2017-11-30 12:10:30 +03:00
|
|
|
static QThreadStorage<NaturalCompare> nCmp;
|
|
|
|
if (!nCmp.hasLocalData())
|
|
|
|
nCmp.setLocalData(NaturalCompare(Qt::CaseSensitive));
|
|
|
|
return (nCmp.localData())(left, right);
|
2016-05-04 12:15:58 +03:00
|
|
|
#else
|
2017-11-30 12:10:30 +03:00
|
|
|
thread_local NaturalCompare nCmp(Qt::CaseSensitive);
|
|
|
|
return nCmp(left, right);
|
2016-05-04 12:15:58 +03:00
|
|
|
#endif
|
2017-11-30 12:10:30 +03:00
|
|
|
}
|
2015-06-20 10:06:49 +03:00
|
|
|
|
2020-07-21 13:56:07 +03:00
|
|
|
#ifdef QBT_USES_QTHREADSTORAGE
|
2016-05-04 12:15:58 +03:00
|
|
|
static QThreadStorage<NaturalCompare> nCmp;
|
2017-11-30 12:10:30 +03:00
|
|
|
if (!nCmp.hasLocalData())
|
|
|
|
nCmp.setLocalData(NaturalCompare(Qt::CaseInsensitive));
|
2016-05-04 12:15:58 +03:00
|
|
|
return (nCmp.localData())(left, right);
|
|
|
|
#else
|
2017-11-30 12:10:30 +03:00
|
|
|
thread_local NaturalCompare nCmp(Qt::CaseInsensitive);
|
2016-05-04 12:15:58 +03:00
|
|
|
return nCmp(left, right);
|
|
|
|
#endif
|
2015-06-20 10:06:49 +03:00
|
|
|
}
|
|
|
|
|
2015-05-06 14:53:27 +03:00
|
|
|
// to send numbers instead of strings with suffixes
|
2019-02-22 00:31:43 +03:00
|
|
|
QString Utils::String::fromDouble(const double n, const int precision)
|
2015-05-06 14:53:27 +03:00
|
|
|
{
|
|
|
|
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
|
|
|
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 The problem manifests when
|
|
|
|
** the number has more digits after the decimal than we want AND the digit after
|
|
|
|
** our 'wanted' is >= 5. In this case our last digit gets rounded up. So for each
|
|
|
|
** precision we add an extra 0 behind 1 in the below algorithm. */
|
|
|
|
|
2019-02-22 00:31:43 +03:00
|
|
|
const double prec = std::pow(10.0, precision);
|
2015-05-06 14:53:27 +03:00
|
|
|
return QLocale::system().toString(std::floor(n * prec) / prec, 'f', precision);
|
|
|
|
}
|
|
|
|
|
2017-02-11 08:33:18 +03:00
|
|
|
// This is marked as internal in QRegExp.cpp, but is exported. The alternative would be to
|
|
|
|
// copy the code from QRegExp::wc2rx().
|
|
|
|
QString qt_regexp_toCanonical(const QString &pattern, QRegExp::PatternSyntax patternSyntax);
|
|
|
|
|
|
|
|
QString Utils::String::wildcardToRegex(const QString &pattern)
|
|
|
|
{
|
|
|
|
return qt_regexp_toCanonical(pattern, QRegExp::Wildcard);
|
|
|
|
}
|
2017-10-14 16:27:21 +03:00
|
|
|
|
|
|
|
bool Utils::String::parseBool(const QString &string, const bool defaultValue)
|
|
|
|
{
|
|
|
|
if (defaultValue)
|
|
|
|
return (string.compare("false", Qt::CaseInsensitive) == 0) ? false : true;
|
|
|
|
return (string.compare("true", Qt::CaseInsensitive) == 0) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TriStateBool Utils::String::parseTriStateBool(const QString &string)
|
|
|
|
{
|
|
|
|
if (string.compare("true", Qt::CaseInsensitive) == 0)
|
|
|
|
return TriStateBool::True;
|
|
|
|
if (string.compare("false", Qt::CaseInsensitive) == 0)
|
|
|
|
return TriStateBool::False;
|
|
|
|
return TriStateBool::Undefined;
|
|
|
|
}
|
2019-06-04 15:46:41 +03:00
|
|
|
|
|
|
|
QString Utils::String::join(const QVector<QStringRef> &strings, const QString &separator)
|
|
|
|
{
|
|
|
|
if (strings.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
QString ret = strings[0].toString();
|
|
|
|
for (int i = 1; i < strings.count(); ++i)
|
|
|
|
ret += (separator + strings[i]);
|
|
|
|
return ret;
|
|
|
|
}
|