mirror of
https://github.com/transmission/transmission.git
synced 2026-05-08 09:39:08 +01:00
refactor: tr_quark_lookup() takes a std::string_view (#2081)
* refactor: tr_quark_lookup takes a std::string_view
This commit is contained in:
+11
-16
@@ -431,24 +431,20 @@ size_t constexpr quarks_are_sorted = ( //
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
static_assert(quarks_are_sorted, "Predefined quarks must be sorted by their string value");
|
static_assert(quarks_are_sorted, "Predefined quarks must be sorted by their string value");
|
||||||
|
static_assert(std::size(my_static) == TR_N_KEYS);
|
||||||
|
|
||||||
auto& my_runtime{ *new std::vector<std::string_view>{} };
|
auto& my_runtime{ *new std::vector<std::string_view>{} };
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
bool tr_quark_lookup(void const* str, size_t len, tr_quark* setme)
|
std::optional<tr_quark> tr_quark_lookup(std::string_view key)
|
||||||
{
|
{
|
||||||
auto constexpr n_static = std::size(my_static);
|
// is it in our static array?
|
||||||
static_assert(n_static == TR_N_KEYS);
|
|
||||||
|
|
||||||
/* is it in our static array? */
|
|
||||||
auto const key = std::string_view{ static_cast<char const*>(str), len };
|
|
||||||
auto constexpr sbegin = std::begin(my_static), send = std::end(my_static);
|
auto constexpr sbegin = std::begin(my_static), send = std::end(my_static);
|
||||||
auto const sit = std::lower_bound(sbegin, send, key);
|
auto const sit = std::lower_bound(sbegin, send, key);
|
||||||
if (sit != send && *sit == key)
|
if (sit != send && *sit == key)
|
||||||
{
|
{
|
||||||
*setme = std::distance(sbegin, sit);
|
return std::distance(sbegin, sit);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* was it added during runtime? */
|
/* was it added during runtime? */
|
||||||
@@ -456,23 +452,22 @@ bool tr_quark_lookup(void const* str, size_t len, tr_quark* setme)
|
|||||||
auto const rit = std::find(rbegin, rend, key);
|
auto const rit = std::find(rbegin, rend, key);
|
||||||
if (rit != rend)
|
if (rit != rend)
|
||||||
{
|
{
|
||||||
*setme = TR_N_KEYS + std::distance(rbegin, rit);
|
return TR_N_KEYS + std::distance(rbegin, rit);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
tr_quark tr_quark_new(std::string_view str)
|
tr_quark tr_quark_new(std::string_view str)
|
||||||
{
|
{
|
||||||
tr_quark ret = TR_KEY_NONE;
|
auto const prior = tr_quark_lookup(str);
|
||||||
|
if (prior)
|
||||||
if (!tr_quark_lookup(std::data(str), std::size(str), &ret))
|
|
||||||
{
|
{
|
||||||
ret = TR_N_KEYS + std::size(my_runtime);
|
return *prior;
|
||||||
my_runtime.emplace_back(tr_strndup(std::data(str), std::size(str)), std::size(str));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto const ret = TR_N_KEYS + std::size(my_runtime);
|
||||||
|
my_runtime.emplace_back(tr_strndup(std::data(str), std::size(str)), std::size(str));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
|
#include <optional>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
#include "tr-macros.h"
|
#include "tr-macros.h"
|
||||||
@@ -425,7 +426,7 @@ enum
|
|||||||
*
|
*
|
||||||
* @return true if the specified string exists as a quark
|
* @return true if the specified string exists as a quark
|
||||||
*/
|
*/
|
||||||
bool tr_quark_lookup(void const* str, size_t len, tr_quark* setme);
|
std::optional<tr_quark> tr_quark_lookup(std::string_view key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the string that corresponds to the specified quark
|
* Get the string that corresponds to the specified quark
|
||||||
|
|||||||
@@ -2443,20 +2443,17 @@ static char const* sessionGet(tr_session* s, tr_variant* args_in, tr_variant* ar
|
|||||||
|
|
||||||
for (size_t i = 0; i < field_count; ++i)
|
for (size_t i = 0; i < field_count; ++i)
|
||||||
{
|
{
|
||||||
char const* field_name = nullptr;
|
auto field_name = std::string_view{};
|
||||||
auto field_name_len = size_t{};
|
if (tr_variantGetStrView(tr_variantListChild(fields, i), &field_name))
|
||||||
if (!tr_variantGetStr(tr_variantListChild(fields, i), &field_name, &field_name_len))
|
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto field_id = tr_quark{};
|
auto const field_id = tr_quark_lookup(field_name);
|
||||||
if (!tr_quark_lookup(field_name, field_name_len, &field_id))
|
if (field_id)
|
||||||
{
|
{
|
||||||
continue;
|
addSessionField(s, args_out, *field_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
addSessionField(s, args_out, field_id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -33,10 +33,9 @@ TEST_F(QuarkTest, allPredefinedKeysCanBeLookedUp)
|
|||||||
for (int i = 0; i < TR_N_KEYS; i++)
|
for (int i = 0; i < TR_N_KEYS; i++)
|
||||||
{
|
{
|
||||||
auto const str = quarkGetString(i);
|
auto const str = quarkGetString(i);
|
||||||
|
auto const q = tr_quark_lookup(str);
|
||||||
tr_quark q;
|
EXPECT_TRUE(q);
|
||||||
EXPECT_TRUE(tr_quark_lookup(str.data(), str.size(), &q));
|
EXPECT_EQ(i, *q);
|
||||||
EXPECT_EQ(i, q);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user