mirror of
https://github.com/RPCSX/rpcsx.git
synced 2026-04-05 14:37:08 +00:00
[Config] Use std::less<> for std::map<...>
Reduces amount of string copies [Utilities] fmt::replace_all: avoid creation of temporary strings
This commit is contained in:
parent
2981867375
commit
cccfb89aa0
14 changed files with 80 additions and 62 deletions
|
|
@ -19,8 +19,8 @@ namespace cfg
|
|||
}
|
||||
}
|
||||
|
||||
_base::_base(type _type, node* owner, const std::string& name, bool dynamic)
|
||||
: m_type(_type), m_dynamic(dynamic), m_name(name)
|
||||
_base::_base(type _type, node* owner, std::string name, bool dynamic)
|
||||
: m_type(_type), m_dynamic(dynamic), m_name(std::move(name))
|
||||
{
|
||||
for (const auto& node : owner->m_nodes)
|
||||
{
|
||||
|
|
@ -33,7 +33,7 @@ namespace cfg
|
|||
owner->m_nodes.emplace_back(this);
|
||||
}
|
||||
|
||||
bool _base::from_string(const std::string&, bool)
|
||||
bool _base::from_string(std::string_view, bool)
|
||||
{
|
||||
cfg_log.fatal("cfg::_base::from_string() purecall");
|
||||
return false;
|
||||
|
|
@ -58,7 +58,7 @@ std::vector<std::string> cfg::make_int_range(s64 min, s64 max)
|
|||
return {std::to_string(min), std::to_string(max)};
|
||||
}
|
||||
|
||||
bool try_to_int64(s64* out, const std::string& value, s64 min, s64 max)
|
||||
bool try_to_int64(s64* out, std::string_view value, s64 min, s64 max)
|
||||
{
|
||||
s64 result;
|
||||
const char* start = &value.front();
|
||||
|
|
@ -104,7 +104,7 @@ std::vector<std::string> cfg::make_uint_range(u64 min, u64 max)
|
|||
return {std::to_string(min), std::to_string(max)};
|
||||
}
|
||||
|
||||
bool try_to_uint64(u64* out, const std::string& value, u64 min, u64 max)
|
||||
bool try_to_uint64(u64* out, std::string_view value, u64 min, u64 max)
|
||||
{
|
||||
u64 result;
|
||||
const char* start = &value.front();
|
||||
|
|
@ -136,7 +136,7 @@ bool try_to_uint64(u64* out, const std::string& value, u64 min, u64 max)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool cfg::try_to_enum_value(u64* out, decltype(&fmt_class_string<int>::format) func, const std::string& value)
|
||||
bool cfg::try_to_enum_value(u64* out, decltype(&fmt_class_string<int>::format) func, std::string_view value)
|
||||
{
|
||||
u64 max = umax;
|
||||
|
||||
|
|
@ -320,7 +320,7 @@ void cfg::decode(const YAML::Node& data, cfg::_base& rhs, bool dynamic)
|
|||
return;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> values;
|
||||
map_of_type<std::string> values;
|
||||
|
||||
for (const auto& pair : data)
|
||||
{
|
||||
|
|
@ -339,7 +339,7 @@ void cfg::decode(const YAML::Node& data, cfg::_base& rhs, bool dynamic)
|
|||
return; // ???
|
||||
}
|
||||
|
||||
std::map<std::string, logs::level> values;
|
||||
map_of_type<logs::level> values;
|
||||
|
||||
for (const auto& pair : data)
|
||||
{
|
||||
|
|
@ -377,9 +377,9 @@ std::string cfg::node::to_string() const
|
|||
return {out.c_str(), out.size()};
|
||||
}
|
||||
|
||||
bool cfg::node::from_string(const std::string& value, bool dynamic)
|
||||
bool cfg::node::from_string(std::string_view value, bool dynamic)
|
||||
{
|
||||
auto [result, error] = yaml_load(value);
|
||||
auto [result, error] = yaml_load(std::string(value));
|
||||
|
||||
if (error.empty())
|
||||
{
|
||||
|
|
@ -414,26 +414,31 @@ void cfg::set_entry::from_default()
|
|||
m_set = {};
|
||||
}
|
||||
|
||||
std::string cfg::map_entry::get_value(const std::string& key)
|
||||
std::string cfg::map_entry::get_value(std::string_view key)
|
||||
{
|
||||
return m_map.contains(key) ? m_map.at(key) : "";
|
||||
if (auto it = m_map.find(key); it != m_map.end())
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void cfg::map_entry::set_value(const std::string& key, const std::string& value)
|
||||
void cfg::map_entry::set_value(std::string key, std::string value)
|
||||
{
|
||||
m_map[key] = value;
|
||||
m_map[std::move(key)] = std::move(value);
|
||||
}
|
||||
|
||||
void cfg::map_entry::set_map(std::map<std::string, std::string>&& map)
|
||||
void cfg::map_entry::set_map(map_of_type<std::string>&& map)
|
||||
{
|
||||
m_map = std::move(map);
|
||||
}
|
||||
|
||||
void cfg::map_entry::erase(const std::string& key)
|
||||
void cfg::map_entry::erase(std::string_view key)
|
||||
{
|
||||
if (m_map.contains(key))
|
||||
if (auto it = m_map.find(key); it != m_map.end())
|
||||
{
|
||||
m_map.erase(key);
|
||||
m_map.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -442,7 +447,7 @@ void cfg::map_entry::from_default()
|
|||
set_map({});
|
||||
}
|
||||
|
||||
void cfg::log_entry::set_map(std::map<std::string, logs::level>&& map)
|
||||
void cfg::log_entry::set_map(map_of_type<logs::level>&& map)
|
||||
{
|
||||
m_map = std::move(map);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ namespace cfg
|
|||
std::vector<std::string> make_uint_range(u64 min, u64 max);
|
||||
|
||||
// Internal hack
|
||||
bool try_to_enum_value(u64* out, decltype(&fmt_class_string<int>::format) func, const std::string&);
|
||||
bool try_to_enum_value(u64* out, decltype(&fmt_class_string<int>::format) func, std::string_view);
|
||||
|
||||
// Internal hack
|
||||
std::vector<std::string> try_to_enum_list(decltype(&fmt_class_string<int>::format) func);
|
||||
|
|
@ -53,7 +53,7 @@ namespace cfg
|
|||
_base(type _type);
|
||||
|
||||
// Owned entry constructor
|
||||
_base(type _type, class node* owner, const std::string& name, bool dynamic);
|
||||
_base(type _type, class node* owner, std::string name, bool dynamic);
|
||||
|
||||
public:
|
||||
_base(const _base&) = delete;
|
||||
|
|
@ -80,7 +80,7 @@ namespace cfg
|
|||
}
|
||||
|
||||
// Try to convert from string (optional)
|
||||
virtual bool from_string(const std::string&, bool /*dynamic*/ = false);
|
||||
virtual bool from_string(std::string_view, bool /*dynamic*/ = false);
|
||||
|
||||
// Get string list (optional)
|
||||
virtual std::vector<std::string> to_list() const
|
||||
|
|
@ -107,8 +107,8 @@ namespace cfg
|
|||
}
|
||||
|
||||
// Registered node constructor
|
||||
node(node* owner, const std::string& name, bool dynamic = true)
|
||||
: _base(type::node, owner, name, dynamic)
|
||||
node(node* owner, std::string name, bool dynamic = true)
|
||||
: _base(type::node, owner, std::move(name), dynamic)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -122,7 +122,7 @@ namespace cfg
|
|||
std::string to_string() const override;
|
||||
|
||||
// Deserialize node
|
||||
bool from_string(const std::string& value, bool dynamic = false) override;
|
||||
bool from_string(std::string_view value, bool dynamic = false) override;
|
||||
|
||||
// Set default values
|
||||
void from_default() override;
|
||||
|
|
@ -135,8 +135,8 @@ namespace cfg
|
|||
public:
|
||||
bool def;
|
||||
|
||||
_bool(node* owner, const std::string& name, bool def = false, bool dynamic = false)
|
||||
: _base(type::_bool, owner, name, dynamic)
|
||||
_bool(node* owner, std::string name, bool def = false, bool dynamic = false)
|
||||
: _base(type::_bool, owner, std::move(name), dynamic)
|
||||
, m_value(def)
|
||||
, def(def)
|
||||
{
|
||||
|
|
@ -159,7 +159,7 @@ namespace cfg
|
|||
return m_value ? "true" : "false";
|
||||
}
|
||||
|
||||
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
|
||||
bool from_string(std::string_view value, bool /*dynamic*/ = false) override
|
||||
{
|
||||
if (value == "false")
|
||||
m_value = false;
|
||||
|
|
@ -220,7 +220,7 @@ namespace cfg
|
|||
return result; // TODO: ???
|
||||
}
|
||||
|
||||
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
|
||||
bool from_string(std::string_view value, bool /*dynamic*/ = false) override
|
||||
{
|
||||
u64 result;
|
||||
|
||||
|
|
@ -285,7 +285,7 @@ namespace cfg
|
|||
return std::to_string(m_value);
|
||||
}
|
||||
|
||||
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
|
||||
bool from_string(std::string_view value, bool /*dynamic*/ = false) override
|
||||
{
|
||||
s64 result;
|
||||
if (try_to_int64(&result, value, Min, Max))
|
||||
|
|
@ -359,7 +359,7 @@ namespace cfg
|
|||
return std::to_string(m_value);
|
||||
}
|
||||
|
||||
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
|
||||
bool from_string(std::string_view value, bool /*dynamic*/ = false) override
|
||||
{
|
||||
u64 result;
|
||||
if (try_to_uint64(&result, value, Min, Max))
|
||||
|
|
@ -430,9 +430,9 @@ namespace cfg
|
|||
return *m_value.load().get();
|
||||
}
|
||||
|
||||
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
|
||||
bool from_string(std::string_view value, bool /*dynamic*/ = false) override
|
||||
{
|
||||
m_value = value;
|
||||
m_value = std::string(value);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
|
@ -474,9 +474,12 @@ namespace cfg
|
|||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
using map_of_type = std::map<std::string, T, std::less<>>;
|
||||
|
||||
class map_entry final : public _base
|
||||
{
|
||||
std::map<std::string, std::string> m_map{};
|
||||
map_of_type<std::string> m_map{};
|
||||
|
||||
public:
|
||||
map_entry(node* owner, const std::string& name)
|
||||
|
|
@ -484,24 +487,24 @@ namespace cfg
|
|||
{
|
||||
}
|
||||
|
||||
const std::map<std::string, std::string>& get_map() const
|
||||
const map_of_type<std::string>& get_map() const
|
||||
{
|
||||
return m_map;
|
||||
}
|
||||
|
||||
std::string get_value(const std::string& key);
|
||||
std::string get_value(std::string_view key);
|
||||
|
||||
void set_value(const std::string& key, const std::string& value);
|
||||
void set_map(std::map<std::string, std::string>&& map);
|
||||
void set_value(std::string key, std::string value);
|
||||
void set_map(map_of_type<std::string>&& map);
|
||||
|
||||
void erase(const std::string& key);
|
||||
void erase(std::string_view key);
|
||||
|
||||
void from_default() override;
|
||||
};
|
||||
|
||||
class log_entry final : public _base
|
||||
{
|
||||
std::map<std::string, logs::level> m_map{};
|
||||
map_of_type<logs::level> m_map{};
|
||||
|
||||
public:
|
||||
log_entry(node* owner, const std::string& name)
|
||||
|
|
@ -509,12 +512,12 @@ namespace cfg
|
|||
{
|
||||
}
|
||||
|
||||
const std::map<std::string, logs::level>& get_map() const
|
||||
const map_of_type<logs::level>& get_map() const
|
||||
{
|
||||
return m_map;
|
||||
}
|
||||
|
||||
void set_map(std::map<std::string, logs::level>&& map);
|
||||
void set_map(map_of_type<logs::level>&& map);
|
||||
|
||||
void from_default() override;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@
|
|||
#include <functional>
|
||||
#include <string_view>
|
||||
|
||||
#include "util/types.hpp"
|
||||
|
||||
#ifdef _WIN32
|
||||
std::string wchar_to_utf8(const wchar_t *src);
|
||||
std::string wchar_path_to_ansi_path(const std::wstring& src);
|
||||
|
|
@ -22,17 +24,17 @@ inline void strcpy_trunc(D& dst, const T& src)
|
|||
}
|
||||
|
||||
// Convert string to signed integer
|
||||
bool try_to_int64(s64* out, const std::string& value, s64 min, s64 max);
|
||||
bool try_to_int64(s64* out, std::string_view value, s64 min, s64 max);
|
||||
|
||||
// Convert string to unsigned integer
|
||||
bool try_to_uint64(u64* out, const std::string& value, u64 min, u64 max);
|
||||
bool try_to_uint64(u64* out, std::string_view value, u64 min, u64 max);
|
||||
|
||||
namespace fmt
|
||||
{
|
||||
std::string replace_all(std::string_view src, std::string_view from, std::string_view to, usz count = -1);
|
||||
|
||||
template <usz list_size>
|
||||
std::string replace_all(std::string src, const std::pair<std::string, std::string> (&list)[list_size])
|
||||
std::string replace_all(std::string src, const std::pair<std::string_view, std::string> (&list)[list_size])
|
||||
{
|
||||
for (usz pos = 0; pos < src.length(); ++pos)
|
||||
{
|
||||
|
|
@ -41,11 +43,14 @@ namespace fmt
|
|||
const usz comp_length = list[i].first.length();
|
||||
|
||||
if (src.length() - pos < comp_length)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (src.substr(pos, comp_length) == list[i].first)
|
||||
{
|
||||
src = (pos ? src.substr(0, pos) + list[i].second : list[i].second) + src.substr(pos + comp_length);
|
||||
src.erase(pos, comp_length);
|
||||
src.insert(pos, list[i].second.data(), list[i].second.length());
|
||||
pos += list[i].second.length() - 1;
|
||||
break;
|
||||
}
|
||||
|
|
@ -56,7 +61,7 @@ namespace fmt
|
|||
}
|
||||
|
||||
template <usz list_size>
|
||||
std::string replace_all(std::string src, const std::pair<std::string, std::function<std::string()>> (&list)[list_size])
|
||||
std::string replace_all(std::string src, const std::pair<std::string_view, std::function<std::string()>> (&list)[list_size])
|
||||
{
|
||||
for (usz pos = 0; pos < src.length(); ++pos)
|
||||
{
|
||||
|
|
@ -65,12 +70,16 @@ namespace fmt
|
|||
const usz comp_length = list[i].first.length();
|
||||
|
||||
if (src.length() - pos < comp_length)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (src.substr(pos, comp_length) == list[i].first)
|
||||
{
|
||||
src = (pos ? src.substr(0, pos) + list[i].second() : list[i].second()) + src.substr(pos + comp_length);
|
||||
pos += list[i].second().length() - 1;
|
||||
src.erase(pos, comp_length);
|
||||
auto replacement = list[i].second();
|
||||
src.insert(pos, replacement);
|
||||
pos += replacement.length() - 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue