This commit is contained in:
Yat Ho 2024-05-06 09:16:13 +08:00 committed by GitHub
commit 69af527f9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 1688 additions and 1638 deletions

View File

@ -18,7 +18,6 @@
#include <locale>
#include <memory>
#include <optional>
#include <set>
#include <stdexcept> // std::runtime_error
#include <string>
#include <string_view>
@ -540,7 +539,7 @@ std::string tr_strpercent(double x)
return fmt::format("{:.0Lf}", x);
}
std::string tr_strratio(double ratio, char const* infinity)
std::string tr_strratio(double ratio, std::string_view infinity)
{
if ((int)ratio == TR_RATIO_NA)
{
@ -549,7 +548,7 @@ std::string tr_strratio(double ratio, char const* infinity)
if ((int)ratio == TR_RATIO_INF)
{
return infinity != nullptr ? infinity : "";
return std::string{ infinity };
}
return tr_strpercent(ratio);

View File

@ -251,7 +251,7 @@ template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
/** @param ratio the ratio to convert to a string
@param infinity the string representation of "infinity" */
[[nodiscard]] std::string tr_strratio(double ratio, char const* infinity);
[[nodiscard]] std::string tr_strratio(double ratio, std::string_view infinity);
// ---

View File

@ -76,8 +76,7 @@ template<typename T>
if (auto* const map = var != nullptr ? var->get_if<tr_variant::MapIndex>() : nullptr; map != nullptr)
{
map->erase(key);
return &map->try_emplace(key, std::forward<T>(val)).first;
return &map->insert_or_assign(key, std::forward<T>(val)).first;
}
return {};

View File

@ -148,8 +148,26 @@ public:
return { vec_.emplace_back(key, tr_variant{ std::forward<Val>(val) }).second, true };
}
template<typename Val>
std::pair<tr_variant&, bool> insert_or_assign(tr_quark const key, Val&& val)
{
auto res = try_emplace(key, std::forward<Val>(val));
if (!res.second)
{
res.first = std::forward<Val>(val);
}
return res;
}
// --- custom functions
template<typename Type>
[[nodiscard]] TR_CONSTEXPR20 auto find_if(tr_quark const key) noexcept
{
auto const iter = find(key);
return iter != end() ? iter->second.get_if<Type>() : nullptr;
}
template<typename Type>
[[nodiscard]] TR_CONSTEXPR20 auto find_if(tr_quark const key) const noexcept
{

File diff suppressed because it is too large Load Diff