qBittorrent
string.cpp
Go to the documentation of this file.
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>
5  *
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.
10  *
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.
15  *
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.
19  *
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.
28  */
29 
30 #include "string.h"
31 
32 #include <cmath>
33 
34 #include <QLocale>
35 #include <QVector>
36 
37 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
38 #include <QRegularExpression>
39 #else
40 #include <QRegExp>
41 #endif
42 
43 // to send numbers instead of strings with suffixes
44 QString Utils::String::fromDouble(const double n, const int precision)
45 {
46  /* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f', 1) == 99.9
47  ** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 The problem manifests when
48  ** the number has more digits after the decimal than we want AND the digit after
49  ** our 'wanted' is >= 5. In this case our last digit gets rounded up. So for each
50  ** precision we add an extra 0 behind 1 in the below algorithm. */
51 
52  const double prec = std::pow(10.0, precision);
53  return QLocale::system().toString(std::floor(n * prec) / prec, 'f', precision);
54 }
55 
56 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
57 QString Utils::String::wildcardToRegexPattern(const QString &pattern)
58 {
59  return QRegularExpression::wildcardToRegularExpression(pattern, QRegularExpression::UnanchoredWildcardConversion);
60 }
61 #else
62 // This is marked as internal in QRegExp.cpp, but is exported. The alternative would be to
63 // copy the code from QRegExp::wc2rx().
64 QString qt_regexp_toCanonical(const QString &pattern, QRegExp::PatternSyntax patternSyntax);
65 
66 QString Utils::String::wildcardToRegexPattern(const QString &pattern)
67 {
68  return qt_regexp_toCanonical(pattern, QRegExp::Wildcard);
69 }
70 #endif
71 
72 std::optional<bool> Utils::String::parseBool(const QString &string)
73 {
74  if (string.compare("true", Qt::CaseInsensitive) == 0)
75  return true;
76  if (string.compare("false", Qt::CaseInsensitive) == 0)
77  return false;
78 
79  return std::nullopt;
80 }
81 
82 std::optional<int> Utils::String::parseInt(const QString &string)
83 {
84  bool ok = false;
85  const int result = string.toInt(&ok);
86  if (ok)
87  return result;
88 
89  return std::nullopt;
90 }
91 
92 std::optional<double> Utils::String::parseDouble(const QString &string)
93 {
94  bool ok = false;
95  const double result = string.toDouble(&ok);
96  if (ok)
97  return result;
98 
99  return std::nullopt;
100 }
101 
102 QString Utils::String::join(const QList<QStringView> &strings, const QStringView separator)
103 {
104  if (strings.empty())
105  return {};
106 
107  QString ret = strings[0].toString();
108  for (int i = 1; i < strings.count(); ++i)
109  ret += (separator + strings[i]);
110  return ret;
111 }
std::optional< bool > parseBool(const QString &string)
Definition: string.cpp:72
std::optional< int > parseInt(const QString &string)
Definition: string.cpp:82
QString fromDouble(double n, int precision)
Definition: string.cpp:44
QString wildcardToRegexPattern(const QString &pattern)
Definition: string.cpp:57
QString join(const QList< QStringView > &strings, QStringView separator)
Definition: string.cpp:102
std::optional< double > parseDouble(const QString &string)
Definition: string.cpp:92