refactor: prefer functions return std::string instead of tr_strbuf (#5949)
This commit is contained in:
parent
cb2462aacd
commit
fc4b7ed9f4
|
@ -166,29 +166,29 @@ std::optional<tr_sha256_digest_t> parseHash2(std::string_view sv)
|
|||
|
||||
// ---
|
||||
|
||||
tr_urlbuf tr_magnet_metainfo::magnet() const
|
||||
std::string tr_magnet_metainfo::magnet() const
|
||||
{
|
||||
auto s = tr_urlbuf{ "magnet:?xt=urn:btih:"sv, info_hash_string() };
|
||||
auto buf = tr_urlbuf{ "magnet:?xt=urn:btih:"sv, info_hash_string() };
|
||||
|
||||
if (!std::empty(name_))
|
||||
{
|
||||
s += "&dn="sv;
|
||||
tr_urlPercentEncode(std::back_inserter(s), name_);
|
||||
buf += "&dn="sv;
|
||||
tr_urlPercentEncode(std::back_inserter(buf), name_);
|
||||
}
|
||||
|
||||
for (auto const& tracker : this->announce_list())
|
||||
{
|
||||
s += "&tr="sv;
|
||||
tr_urlPercentEncode(std::back_inserter(s), tracker.announce.sv());
|
||||
buf += "&tr="sv;
|
||||
tr_urlPercentEncode(std::back_inserter(buf), tracker.announce.sv());
|
||||
}
|
||||
|
||||
for (auto const& webseed : webseed_urls_)
|
||||
{
|
||||
s += "&ws="sv;
|
||||
tr_urlPercentEncode(std::back_inserter(s), webseed);
|
||||
buf += "&ws="sv;
|
||||
tr_urlPercentEncode(std::back_inserter(buf), webseed);
|
||||
}
|
||||
|
||||
return s;
|
||||
return std::string{ buf.sv() };
|
||||
}
|
||||
|
||||
void tr_magnet_metainfo::add_webseed(std::string_view webseed)
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
#include "libtransmission/announce-list.h"
|
||||
#include "libtransmission/crypto-utils.h"
|
||||
#include "libtransmission/tr-macros.h" // TR_CONSTEXPR20, tr_sha1_digest_t
|
||||
#include "libtransmission/tr-strbuf.h" // tr_urlbuf
|
||||
#include "libtransmission/utils.h" // tr_strv_convert_utf8()
|
||||
|
||||
struct tr_error;
|
||||
|
@ -25,7 +24,7 @@ class tr_magnet_metainfo
|
|||
public:
|
||||
bool parseMagnet(std::string_view magnet_link, tr_error** error = nullptr);
|
||||
|
||||
[[nodiscard]] tr_urlbuf magnet() const;
|
||||
[[nodiscard]] std::string magnet() const;
|
||||
|
||||
[[nodiscard]] constexpr auto const& info_hash() const noexcept
|
||||
{
|
||||
|
|
|
@ -130,7 +130,7 @@ bool tr_torrentUseMetainfoFromFile(
|
|||
tr_error** error)
|
||||
{
|
||||
// add .torrent file
|
||||
if (!tr_sys_path_copy(filename_in, tor->torrent_file(), error))
|
||||
if (!tr_sys_path_copy(filename_in, tor->torrent_file().c_str(), error))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -401,7 +401,7 @@ double tr_torrentGetMetadataPercent(tr_torrent const* tor)
|
|||
|
||||
std::string tr_torrentGetMagnetLink(tr_torrent const* tor)
|
||||
{
|
||||
return std::string{ tor->metainfo_.magnet().sv() };
|
||||
return tor->metainfo_.magnet();
|
||||
}
|
||||
|
||||
size_t tr_torrentGetMagnetLinkToBuf(tr_torrent const* tor, char* buf, size_t buflen)
|
||||
|
|
|
@ -672,7 +672,7 @@ bool tr_torrent_metainfo::parse_torrent_file(std::string_view filename, std::vec
|
|||
return tr_file_read(filename, *contents, error) && parse_benc({ std::data(*contents), std::size(*contents) }, error);
|
||||
}
|
||||
|
||||
tr_pathbuf tr_torrent_metainfo::make_filename(
|
||||
std::string tr_torrent_metainfo::make_filename(
|
||||
std::string_view dirname,
|
||||
std::string_view name,
|
||||
std::string_view info_hash_string,
|
||||
|
@ -681,8 +681,17 @@ tr_pathbuf tr_torrent_metainfo::make_filename(
|
|||
{
|
||||
// `${dirname}/${name}.${info_hash}${suffix}`
|
||||
// `${dirname}/${info_hash}${suffix}`
|
||||
return format == BasenameFormat::Hash ? tr_pathbuf{ dirname, '/', info_hash_string, suffix } :
|
||||
tr_pathbuf{ dirname, '/', name, '.', info_hash_string.substr(0, 16), suffix };
|
||||
auto filename = tr_pathbuf{ dirname, '/' };
|
||||
if (format == BasenameFormat::Hash)
|
||||
{
|
||||
filename.append(info_hash_string);
|
||||
}
|
||||
else
|
||||
{
|
||||
filename.append(name, '.', info_hash_string.substr(0, 16));
|
||||
}
|
||||
filename.append(suffix);
|
||||
return std::string{ filename.sv() };
|
||||
}
|
||||
|
||||
bool tr_torrent_metainfo::migrate_file(
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
#include "block-info.h"
|
||||
#include "magnet-metainfo.h"
|
||||
#include "torrent-files.h"
|
||||
#include "tr-strbuf.h"
|
||||
|
||||
struct tr_error;
|
||||
|
||||
|
@ -205,7 +204,7 @@ private:
|
|||
NameAndPartialHash
|
||||
};
|
||||
|
||||
[[nodiscard]] static tr_pathbuf make_filename(
|
||||
[[nodiscard]] static std::string make_filename(
|
||||
std::string_view dirname,
|
||||
std::string_view name,
|
||||
std::string_view info_hash_string,
|
||||
|
|
|
@ -45,7 +45,7 @@ TEST_F(TorrentMetainfoTest, magnetLink)
|
|||
EXPECT_TRUE(metainfo.parseMagnet(MagnetLink));
|
||||
EXPECT_EQ(0U, metainfo.file_count()); // because it's a magnet link
|
||||
EXPECT_EQ(2U, std::size(metainfo.announce_list()));
|
||||
EXPECT_EQ(MagnetLink, metainfo.magnet().sv());
|
||||
EXPECT_EQ(MagnetLink, metainfo.magnet());
|
||||
}
|
||||
|
||||
#define BEFORE_PATH \
|
||||
|
|
Loading…
Reference in New Issue