WebAPI: Add a way to download .torrent file using search plugin
[qBittorrent.git] / src / base / utils / string.cpp
blob0c64914385514bb10876061209f8b776bd0e34a2
1 /*
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.
30 #include "string.h"
32 #include <cmath>
34 #include <QLocale>
35 #include <QRegularExpression>
36 #include <QStringList>
37 #include <QVector>
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)
59 QStringList ret;
60 ret.reserve(32);
62 bool inQuotes = false;
63 QString tmp;
64 for (const QChar c : command)
66 if (c == u' ')
68 if (!inQuotes)
70 if (!tmp.isEmpty())
72 ret.append(tmp);
73 tmp.clear();
76 continue;
79 else if (c == u'"')
81 inQuotes = !inQuotes;
84 tmp.append(c);
87 if (!tmp.isEmpty())
88 ret.append(tmp);
90 return ret;
93 std::optional<bool> Utils::String::parseBool(const QString &string)
95 if (string.compare(u"true", Qt::CaseInsensitive) == 0)
96 return true;
97 if (string.compare(u"false", Qt::CaseInsensitive) == 0)
98 return false;
100 return std::nullopt;
103 std::optional<int> Utils::String::parseInt(const QString &string)
105 bool ok = false;
106 const int result = string.toInt(&ok);
107 if (ok)
108 return result;
110 return std::nullopt;
113 std::optional<double> Utils::String::parseDouble(const QString &string)
115 bool ok = false;
116 const double result = string.toDouble(&ok);
117 if (ok)
118 return result;
120 return std::nullopt;