Change URL seed error message
[qBittorrent.git] / src / base / utils / string.h
blob1f5aa2c279308e739e564e849b597ac3c6cb91c9
1 /*
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.
31 #pragma once
33 #include <numeric>
34 #include <optional>
35 #include <string_view>
37 #include <QChar>
38 #include <QMetaEnum>
39 #include <QString>
40 #include <QtContainerFwd>
42 #include "base/concepts/explicitlyconvertibleto.h"
43 #include "base/global.h"
45 namespace Utils::String
47 QString wildcardToRegexPattern(const QString &pattern);
49 template <typename T>
50 T unquote(const T &str, const QString &quotes = u"\""_s)
52 if (str.length() < 2) return str;
54 for (const QChar quote : quotes)
56 if (str.startsWith(quote) && str.endsWith(quote))
57 return str.mid(1, (str.length() - 2));
60 return str;
63 std::optional<bool> parseBool(const QString &string);
64 std::optional<int> parseInt(const QString &string);
65 std::optional<double> parseDouble(const QString &string);
67 QStringList splitCommand(const QString &command);
69 QString fromDouble(double n, int precision);
70 QString fromLatin1(std::string_view string);
71 QString fromLocal8Bit(std::string_view string);
73 template <typename Container>
74 QString joinIntoString(const Container &container, const QString &separator)
75 requires ExplicitlyConvertibleTo<typename Container::value_type, QString>
77 auto iter = container.cbegin();
78 const auto end = container.cend();
79 if (iter == end)
80 return {};
82 const qsizetype totalLength = std::accumulate(iter, end, (separator.size() * (container.size() - 1))
83 , [](const qsizetype total, const typename Container::value_type &value)
85 return total + QString(value).size();
86 });
88 QString ret;
89 ret.reserve(totalLength);
90 ret.append(QString(*iter));
91 ++iter;
93 while (iter != end)
95 ret.append(separator + QString(*iter));
96 ++iter;
99 return ret;
102 template <typename T>
103 QString fromEnum(const T &value)
104 requires std::is_enum_v<T>
106 static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
107 "Enumeration underlying type has to be int.");
109 const auto metaEnum = QMetaEnum::fromType<T>();
110 return QString::fromLatin1(metaEnum.valueToKey(static_cast<int>(value)));
113 template <typename T>
114 T toEnum(const QString &serializedValue, const T &defaultValue)
115 requires std::is_enum_v<T>
117 static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
118 "Enumeration underlying type has to be int.");
120 const auto metaEnum = QMetaEnum::fromType<T>();
121 bool ok = false;
122 const T value = static_cast<T>(metaEnum.keyToValue(serializedValue.toLatin1().constData(), &ok));
123 return (ok ? value : defaultValue);