qBittorrent
string.h
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 #pragma once
31 
32 #include <optional>
33 
34 #include <QChar>
35 #include <QMetaEnum>
36 #include <QString>
37 #include <Qt>
38 #include <QtContainerFwd>
39 
40 namespace Utils::String
41 {
42  QString wildcardToRegexPattern(const QString &pattern);
43 
44  template <typename T>
45  T unquote(const T &str, const QString &quotes = QChar('"'))
46  {
47  if (str.length() < 2) return str;
48 
49  for (const QChar quote : quotes)
50  {
51  if (str.startsWith(quote) && str.endsWith(quote))
52  return str.mid(1, (str.length() - 2));
53  }
54 
55  return str;
56  }
57 
58  std::optional<bool> parseBool(const QString &string);
59  std::optional<int> parseInt(const QString &string);
60  std::optional<double> parseDouble(const QString &string);
61 
62  QString join(const QList<QStringView> &strings, QStringView separator);
63 
64  QString fromDouble(double n, int precision);
65 
66  template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
67  QString fromEnum(const T &value)
68  {
69  static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
70  "Enumeration underlying type has to be int.");
71 
72  const auto metaEnum = QMetaEnum::fromType<T>();
73  return QString::fromLatin1(metaEnum.valueToKey(static_cast<int>(value)));
74  }
75 
76  template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
77  T toEnum(const QString &serializedValue, const T &defaultValue)
78  {
79  static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
80  "Enumeration underlying type has to be int.");
81 
82  const auto metaEnum = QMetaEnum::fromType<T>();
83  bool ok = false;
84  const T value = static_cast<T>(metaEnum.keyToValue(serializedValue.toLatin1().constData(), &ok));
85  return (ok ? value : defaultValue);
86  }
87 }
std::optional< bool > parseBool(const QString &string)
Definition: string.cpp:72
QString fromEnum(const T &value)
Definition: string.h:67
std::optional< int > parseInt(const QString &string)
Definition: string.cpp:82
T unquote(const T &str, const QString &quotes=QChar('"'))
Definition: string.h:45
QString fromDouble(double n, int precision)
Definition: string.cpp:44
T toEnum(const QString &serializedValue, const T &defaultValue)
Definition: string.h:77
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
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64