Merge bitcoin/bitcoin#27892: refactor: Avoid copy of bilingual_str when formatting, Fix ADL violation

fa8ef7d138 refactor: Avoid copy of bilingual_str when formatting, Fix ADL violation (MarcoFalke)

Pull request description:

  This refactor shouldn't change behavior, but may fix compile errors such as https://github.com/bitcoin/bitcoin/pull/27862#issuecomment-1592516184

ACKs for top commit:
  achow101:
    ACK fa8ef7d138
  ryanofsky:
    Code review ACK fa8ef7d138. Looks great! Thanks for updating
  hebasto:
    ACK fa8ef7d138, I have reviewed the code and it looks OK.

Tree-SHA512: 903019962f27b5432b8e3af052b472238ef68d3ee165148c9d2232bf290309075f9f17d8d06c9b5c7fddb89c1a9c3a4c09c6310af01e8561adc0244a30db0857
This commit is contained in:
Andrew Chow 2023-06-15 14:22:20 -04:00
commit 5b8e07725d
No known key found for this signature in database
GPG Key ID: 17565732E08E5E41

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. // Provide an overload of tinyformat::format which can take bilingual_str arguments.
namespace tinyformat { 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> template <typename... Args>
bilingual_str format(const bilingual_str& fmt, const Args&... args) bilingual_str format(const bilingual_str& fmt, const Args&... args)
{ {
return bilingual_str{format(fmt.original, TranslateArg(args, false)...), const auto translate_arg{[](const auto& arg, bool translated) -> const auto& {
format(fmt.translated, TranslateArg(args, true)...)}; 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 } // namespace tinyformat