qBittorrent
Utils::String Namespace Reference

Functions

QString wildcardToRegexPattern (const QString &pattern)
 
template<typename T >
unquote (const T &str, const QString &quotes=QChar('"'))
 
std::optional< bool > parseBool (const QString &string)
 
std::optional< int > parseInt (const QString &string)
 
std::optional< double > parseDouble (const QString &string)
 
QString join (const QList< QStringView > &strings, QStringView separator)
 
QString fromDouble (double n, int precision)
 
template<typename T , typename std::enable_if_t< std::is_enum_v< T >, int > = 0>
QString fromEnum (const T &value)
 
template<typename T , typename std::enable_if_t< std::is_enum_v< T >, int > = 0>
toEnum (const QString &serializedValue, const T &defaultValue)
 

Function Documentation

◆ fromDouble()

QString Utils::String::fromDouble ( double  n,
int  precision 
)

Definition at line 44 of file string.cpp.

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 }

Referenced by TorrentContentModelItem::displayData(), TransferListModel::displayValue(), Utils::Misc::friendlyUnit(), anonymous_namespace{synccontroller.cpp}::getTransferInfo(), PropertiesWidget::loadDynamicData(), PreviewListDelegate::paint(), StatsDialog::update(), and PeerListWidget::updatePeer().

Here is the caller graph for this function:

◆ fromEnum()

template<typename T , typename std::enable_if_t< std::is_enum_v< T >, int > = 0>
QString Utils::String::fromEnum ( const T &  value)

Definition at line 67 of file string.h.

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  }
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64

References anonymous_namespace{preferences.cpp}::value().

Referenced by anonymous_namespace{rss_autodownloadrule.cpp}::contentLayoutToJsonValue(), AppController::preferencesAction(), anonymous_namespace{torrentfileswatcher.cpp}::serializeAddTorrentParams(), BitTorrent::BencodeResumeDataStorage::Worker::store(), BitTorrent::DBResumeDataStorage::Worker::store(), SettingsStorage::storeValue(), and anonymous_namespace{upgrade.cpp}::upgradeTorrentContentLayout().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ join()

QString Utils::String::join ( const QList< QStringView > &  strings,
QStringView  separator 
)

Definition at line 102 of file string.cpp.

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 }

Referenced by nova3.novaprinter::anySizeToBytes(), nova3.nova2::displayCapabilities(), nova3.nova2::engines_to_xml(), BitTorrent::TorrentImpl::handleFileRenamedAlert(), nova3.helpers::htmlentitydecode(), nova3.nova2::initialize_engines(), nova3.nova2::main(), nova3.novaprinter::prettyPrinter(), and nova3.helpers::retrieve_url().

Here is the caller graph for this function:

◆ parseBool()

std::optional< bool > Utils::String::parseBool ( const QString &  string)

Definition at line 72 of file string.cpp.

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 }

Referenced by TorrentsController::addAction(), TorrentsController::createCategoryAction(), TorrentsController::deleteAction(), TorrentsController::editCategoryAction(), SearchController::enablePluginAction(), TorrentsController::infoAction(), RSSController::itemsAction(), LogController::mainAction(), TorrentsController::setAutoManagementAction(), TorrentsController::setForceStartAction(), and TorrentsController::setSuperSeedingAction().

Here is the caller graph for this function:

◆ parseDouble()

std::optional< double > Utils::String::parseDouble ( const QString &  string)

Definition at line 92 of file string.cpp.

93 {
94  bool ok = false;
95  const double result = string.toDouble(&ok);
96  if (ok)
97  return result;
98 
99  return std::nullopt;
100 }

Referenced by TorrentsController::addAction().

Here is the caller graph for this function:

◆ parseInt()

std::optional< int > Utils::String::parseInt ( const QString &  string)

Definition at line 82 of file string.cpp.

83 {
84  bool ok = false;
85  const int result = string.toInt(&ok);
86  if (ok)
87  return result;
88 
89  return std::nullopt;
90 }

Referenced by TorrentsController::addAction(), and Http::RequestParser::doParse().

Here is the caller graph for this function:

◆ toEnum()

template<typename T , typename std::enable_if_t< std::is_enum_v< T >, int > = 0>
T Utils::String::toEnum ( const QString &  serializedValue,
const T &  defaultValue 
)

Definition at line 77 of file string.h.

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  }

References anonymous_namespace{preferences.cpp}::value().

Referenced by TorrentsController::addAction(), anonymous_namespace{rss_autodownloadrule.cpp}::jsonValueToContentLayout(), BitTorrent::BencodeResumeDataStorage::loadTorrentResumeData(), and AppController::setPreferencesAction().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unquote()

template<typename T >
T Utils::String::unquote ( const T &  str,
const QString &  quotes = QChar('"') 
)

Definition at line 45 of file string.h.

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  }

Referenced by anonymous_namespace{webapplication.cpp}::parseCookie(), Http::RequestParser::parseFormData(), Http::RequestParser::parsePostMessage(), and anonymous_namespace{cmdoptions.cpp}::StringOption::value().

Here is the caller graph for this function:

◆ wildcardToRegexPattern()

QString Utils::String::wildcardToRegexPattern ( const QString &  pattern)

Definition at line 57 of file string.cpp.

58 {
59  return QRegularExpression::wildcardToRegularExpression(pattern, QRegularExpression::UnanchoredWildcardConversion);
60 }

Referenced by TransferListWidget::applyNameFilter(), RSS::AutoDownloadRule::cachedRegex(), SearchJobWidget::filterSearchResults(), PropertiesWidget::filterText(), AutomatedRssDownloader::updateMustLineValidity(), AutomatedRssDownloader::updateMustNotLineValidity(), and WebApplication::validateHostHeader().

Here is the caller graph for this function: