2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2023 Mike Tzou (Chocobo1)
4 * Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
5 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * In addition, as a special exception, the copyright holders give permission to
22 * link this program with the OpenSSL project's "OpenSSL" library (or with
23 * modified versions of it that use the same license as the "OpenSSL" library),
24 * and distribute the linked executables. You must obey the GNU General Public
25 * License in all respects for all of the code used other than "OpenSSL". If you
26 * modify file(s), you may extend this exception to your version of the file(s),
27 * but you are not obligated to do so. If you do not wish to do so, delete this
28 * exception statement from your version.
39 #include <QtContainerFwd>
41 #include "base/concepts/explicitlyconvertibleto.h"
42 #include "base/global.h"
44 namespace Utils::String
46 QString
wildcardToRegexPattern(const QString
&pattern
);
49 T
unquote(const T
&str
, const QString
"es
= u
"\""_s
)
51 if (str
.length() < 2) return str
;
53 for (const QChar quote
: quotes
)
55 if (str
.startsWith(quote
) && str
.endsWith(quote
))
56 return str
.mid(1, (str
.length() - 2));
62 std::optional
<bool> parseBool(const QString
&string
);
63 std::optional
<int> parseInt(const QString
&string
);
64 std::optional
<double> parseDouble(const QString
&string
);
66 QStringList
splitCommand(const QString
&command
);
68 QString
fromDouble(double n
, int precision
);
70 template <typename Container
>
71 QString
joinIntoString(const Container
&container
, const QString
&separator
)
72 requires ExplicitlyConvertibleTo
<typename
Container::value_type
, QString
>
74 auto iter
= container
.cbegin();
75 const auto end
= container
.cend();
79 const qsizetype totalLength
= std::accumulate(iter
, end
, (separator
.size() * (container
.size() - 1))
80 , [](const qsizetype total
, const typename
Container::value_type
&value
)
82 return total
+ QString(value
).size();
86 ret
.reserve(totalLength
);
87 ret
.append(QString(*iter
));
92 ret
.append(separator
+ QString(*iter
));
100 QString
fromEnum(const T
&value
)
101 requires
std::is_enum_v
<T
>
103 static_assert(std::is_same_v
<int, typename
std::underlying_type_t
<T
>>,
104 "Enumeration underlying type has to be int.");
106 const auto metaEnum
= QMetaEnum::fromType
<T
>();
107 return QString::fromLatin1(metaEnum
.valueToKey(static_cast<int>(value
)));
110 template <typename T
>
111 T
toEnum(const QString
&serializedValue
, const T
&defaultValue
)
112 requires
std::is_enum_v
<T
>
114 static_assert(std::is_same_v
<int, typename
std::underlying_type_t
<T
>>,
115 "Enumeration underlying type has to be int.");
117 const auto metaEnum
= QMetaEnum::fromType
<T
>();
119 const T value
= static_cast<T
>(metaEnum
.keyToValue(serializedValue
.toLatin1().constData(), &ok
));
120 return (ok
? value
: defaultValue
);