refactor: Avoid copy of bilingual_str when formatting, Fix ADL violation

The return type of TranslateArg is std::string, which creates a copy.
Fix this by moving everything into a lambda that takes a reference and
returns a reference.

Also, the format function is called without specifying the namespace it
lives in. Fix this by specifying the namespace. See also:
7a59865793/doc/developer-notes.md (L117-L137).
This commit is contained in:
MarcoFalke 2023-06-15 09:38:46 +02:00
parent 681ecac5c2
commit fa8ef7d138
No known key found for this signature in database

View file

@ -49,22 +49,18 @@ inline bilingual_str Untranslated(std::string original) { return {original, orig
// Provide an overload of tinyformat::format which can take bilingual_str arguments.
namespace tinyformat {
inline std::string TranslateArg(const bilingual_str& arg, bool translated)
{
return translated ? arg.translated : arg.original;
}
template <typename T>
inline T const& TranslateArg(const T& arg, bool translated)
{
return arg;
}
template <typename... Args>
bilingual_str format(const bilingual_str& fmt, const Args&... args)
{
return bilingual_str{format(fmt.original, TranslateArg(args, false)...),
format(fmt.translated, TranslateArg(args, true)...)};
const auto translate_arg{[](const auto& arg, bool translated) -> const auto& {
if constexpr (std::is_same_v<decltype(arg), const bilingual_str&>) {
return translated ? arg.translated : arg.original;
} else {
return arg;
}
}};
return bilingual_str{tfm::format(fmt.original, translate_arg(args, false)...),
tfm::format(fmt.translated, translate_arg(args, true)...)};
}
} // namespace tinyformat