2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
35 #include <QRegularExpression>
36 #include <QStringList>
39 // to send numbers instead of strings with suffixes
40 QString
Utils::String::fromDouble(const double n
, const int precision
)
42 /* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f', 1) == 99.9
43 ** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 The problem manifests when
44 ** the number has more digits after the decimal than we want AND the digit after
45 ** our 'wanted' is >= 5. In this case our last digit gets rounded up. So for each
46 ** precision we add an extra 0 behind 1 in the below algorithm. */
48 const double prec
= std::pow(10.0, precision
);
49 return QLocale::system().toString(std::floor(n
* prec
) / prec
, 'f', precision
);
52 QString
Utils::String::wildcardToRegexPattern(const QString
&pattern
)
54 return QRegularExpression::wildcardToRegularExpression(pattern
, QRegularExpression::UnanchoredWildcardConversion
);
57 QStringList
Utils::String::splitCommand(const QString
&command
)
62 bool inQuotes
= false;
64 for (const QChar c
: command
)
93 std::optional
<bool> Utils::String::parseBool(const QString
&string
)
95 if (string
.compare(u
"true", Qt::CaseInsensitive
) == 0)
97 if (string
.compare(u
"false", Qt::CaseInsensitive
) == 0)
103 std::optional
<int> Utils::String::parseInt(const QString
&string
)
106 const int result
= string
.toInt(&ok
);
113 std::optional
<double> Utils::String::parseDouble(const QString
&string
)
116 const double result
= string
.toDouble(&ok
);