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>
|
|
|
|
|
2015-05-06 14:53:27 +03:00
|
|
|
#include <QLocale>
|
2020-12-09 08:48:59 +03:00
|
|
|
#include <QVector>
|
2020-07-21 13:56:07 +03:00
|
|
|
|
2021-03-14 18:11:23 +03:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
|
|
|
#include <QRegularExpression>
|
|
|
|
#else
|
|
|
|
#include <QRegExp>
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
{
|
2021-03-01 09:40:30 +03:00
|
|
|
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f', 1) == 99.9
|
2015-05-06 14:53:27 +03:00
|
|
|
** 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);
|
|
|
|
}
|
|
|
|
|
2021-03-14 18:11:23 +03:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
|
|
|
QString Utils::String::wildcardToRegexPattern(const QString &pattern)
|
|
|
|
{
|
|
|
|
return QRegularExpression::wildcardToRegularExpression(pattern, QRegularExpression::UnanchoredWildcardConversion);
|
|
|
|
}
|
|
|
|
#else
|
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);
|
|
|
|
|
2021-03-14 18:11:23 +03:00
|
|
|
QString Utils::String::wildcardToRegexPattern(const QString &pattern)
|
2017-02-11 08:33:18 +03:00
|
|
|
{
|
|
|
|
return qt_regexp_toCanonical(pattern, QRegExp::Wildcard);
|
|
|
|
}
|
2021-03-14 18:11:23 +03:00
|
|
|
#endif
|
2017-10-14 16:27:21 +03:00
|
|
|
|
2021-01-03 15:33:32 +03:00
|
|
|
std::optional<bool> Utils::String::parseBool(const QString &string)
|
2017-10-14 16:27:21 +03:00
|
|
|
{
|
2021-01-03 15:33:32 +03:00
|
|
|
if (string.compare("true", Qt::CaseInsensitive) == 0)
|
|
|
|
return true;
|
|
|
|
if (string.compare("false", Qt::CaseInsensitive) == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return std::nullopt;
|
2017-10-14 16:27:21 +03:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:26:41 +03:00
|
|
|
std::optional<int> Utils::String::parseInt(const QString &string)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
const int result = string.toInt(&ok);
|
|
|
|
if (ok)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<double> Utils::String::parseDouble(const QString &string)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
const double result = string.toDouble(&ok);
|
|
|
|
if (ok)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2021-03-01 09:40:30 +03:00
|
|
|
QString Utils::String::join(const QList<QStringView> &strings, const QStringView separator)
|
2019-06-04 15:46:41 +03:00
|
|
|
{
|
|
|
|
if (strings.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
QString ret = strings[0].toString();
|
|
|
|
for (int i = 1; i < strings.count(); ++i)
|
|
|
|
ret += (separator + strings[i]);
|
|
|
|
return ret;
|
|
|
|
}
|