mirror of
https://github.com/transmission/transmission
synced 2025-02-21 21:57:01 +00:00
refactor: tr_torrentFindFromHashString() takes a std::string_view (#2082)
* refactor: tr_torrentFindFromHashString() takes a string_view
This commit is contained in:
parent
5594b16358
commit
e5225ba8a2
4 changed files with 33 additions and 15 deletions
|
@ -119,7 +119,7 @@ static auto getTorrents(tr_session* session, tr_variant* args)
|
|||
auto torrents = std::vector<tr_torrent*>{};
|
||||
|
||||
auto id = int64_t{};
|
||||
char const* str = nullptr;
|
||||
auto sv = std::string_view{};
|
||||
tr_variant* ids = nullptr;
|
||||
|
||||
if (tr_variantDictFindList(args, TR_KEY_ids, &ids))
|
||||
|
@ -136,9 +136,9 @@ static auto getTorrents(tr_session* session, tr_variant* args)
|
|||
{
|
||||
tor = tr_torrentFindFromId(session, id);
|
||||
}
|
||||
else if (tr_variantGetStr(node, &str, nullptr))
|
||||
else if (tr_variantGetStrView(node, &sv))
|
||||
{
|
||||
tor = tr_torrentFindFromHashString(session, str);
|
||||
tor = tr_torrentFindFromHashString(session, sv);
|
||||
}
|
||||
|
||||
if (tor != nullptr)
|
||||
|
@ -150,15 +150,14 @@ static auto getTorrents(tr_session* session, tr_variant* args)
|
|||
else if (tr_variantDictFindInt(args, TR_KEY_ids, &id) || tr_variantDictFindInt(args, TR_KEY_id, &id))
|
||||
{
|
||||
tr_torrent* const tor = tr_torrentFindFromId(session, id);
|
||||
|
||||
if (tor != nullptr)
|
||||
{
|
||||
torrents.push_back(tor);
|
||||
}
|
||||
}
|
||||
else if (tr_variantDictFindStr(args, TR_KEY_ids, &str, nullptr))
|
||||
else if (tr_variantDictFindStrView(args, TR_KEY_ids, &sv))
|
||||
{
|
||||
if (strcmp(str, "recently-active") == 0)
|
||||
if (sv == "recently-active"sv)
|
||||
{
|
||||
time_t const cutoff = tr_time() - RECENTLY_ACTIVE_SECONDS;
|
||||
|
||||
|
@ -171,8 +170,7 @@ static auto getTorrents(tr_session* session, tr_variant* args)
|
|||
}
|
||||
else
|
||||
{
|
||||
tr_torrent* const tor = tr_torrentFindFromHashString(session, str);
|
||||
|
||||
tr_torrent* const tor = tr_torrentFindFromHashString(session, sv);
|
||||
if (tor != nullptr)
|
||||
{
|
||||
torrents.push_back(tor);
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include <list>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
|
@ -101,11 +102,30 @@ struct CompareHash
|
|||
}
|
||||
};
|
||||
|
||||
struct CompareHashString
|
||||
struct CaseInsensitiveStringCompare // case-insensitive string compare
|
||||
{
|
||||
bool operator()(char const* const a, char const* const b) const
|
||||
int compare(std::string_view a, std::string_view b) const // <=>
|
||||
{
|
||||
return evutil_ascii_strcasecmp(a, b) < 0;
|
||||
auto const alen = std::size(a);
|
||||
auto const blen = std::size(b);
|
||||
|
||||
auto i = evutil_ascii_strncasecmp(std::data(a), std::data(b), std::min(alen, blen));
|
||||
if (i != 0)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
|
||||
if (alen != blen)
|
||||
{
|
||||
return alen < blen ? -1 : 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool operator()(std::string_view a, std::string_view b) const // less than
|
||||
{
|
||||
return compare(a, b) < 0;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -194,7 +214,7 @@ struct tr_session
|
|||
std::unordered_set<tr_torrent*> torrents;
|
||||
std::map<int, tr_torrent*> torrentsById;
|
||||
std::map<uint8_t const*, tr_torrent*, CompareHash> torrentsByHash;
|
||||
std::map<char const*, tr_torrent*, CompareHashString> torrentsByHashString;
|
||||
std::map<std::string_view, tr_torrent*, CaseInsensitiveStringCompare> torrentsByHashString;
|
||||
|
||||
std::array<std::string, TR_SCRIPT_N_TYPES> scripts;
|
||||
|
||||
|
|
|
@ -86,10 +86,10 @@ tr_torrent* tr_torrentFindFromId(tr_session* session, int id)
|
|||
return it == std::end(src) ? nullptr : it->second;
|
||||
}
|
||||
|
||||
tr_torrent* tr_torrentFindFromHashString(tr_session* session, char const* hashstr)
|
||||
tr_torrent* tr_torrentFindFromHashString(tr_session* session, std::string_view hash_string)
|
||||
{
|
||||
auto& src = session->torrentsByHashString;
|
||||
auto it = src.find(hashstr);
|
||||
auto it = src.find(hash_string);
|
||||
return it == std::end(src) ? nullptr : it->second;
|
||||
}
|
||||
|
||||
|
|
|
@ -64,7 +64,7 @@ void tr_torrentSetHasPiece(tr_torrent* tor, tr_piece_index_t pieceIndex, bool ha
|
|||
|
||||
void tr_torrentChangeMyPort(tr_torrent* session);
|
||||
|
||||
tr_torrent* tr_torrentFindFromHashString(tr_session* session, char const* hashString);
|
||||
tr_torrent* tr_torrentFindFromHashString(tr_session* session, std::string_view hash_string);
|
||||
|
||||
tr_torrent* tr_torrentFindFromObfuscatedHash(tr_session* session, uint8_t const* hash);
|
||||
|
||||
|
|
Loading…
Reference in a new issue