From f47c22dfb2fa428cf0756ad6f5ecd8cd90b85ff1 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sat, 13 Apr 2024 17:54:52 +0800 Subject: [PATCH] fixup... use an unordered_map to substitute in numeric format string --- libgnucash/app-utils/gnc-ui-util.cpp | 33 +++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/libgnucash/app-utils/gnc-ui-util.cpp b/libgnucash/app-utils/gnc-ui-util.cpp index 22498a95602..4a5d90b63f1 100644 --- a/libgnucash/app-utils/gnc-ui-util.cpp +++ b/libgnucash/app-utils/gnc-ui-util.cpp @@ -1500,18 +1500,41 @@ gnc_wrap_text_with_bidi_ltr_isolate (const char* text) /********************************************************************\ ********************************************************************/ +using StrStrVec = std::vector>; + +static std::string +string_replace_substring (const std::string& input, const StrStrVec& replacements) +{ + std::string result = input; + for (const auto& [key, val] : replacements) + for (auto pos = result.find(key); pos != std::string::npos; pos = result.find(key)) + result.replace(pos, key.length(), val); + return result; +} + static std::string number_to_words(double val, int64_t denom) { double int_part; const int frac_part = std::round(std::modf (std::fabs(val), &int_part) * denom); - const std::vector tail = - { " ", _("and"), " ", std::to_string (frac_part), "/", std::to_string (denom) }; std::ostringstream ss; - ss.imbue(gnc_get_boost_locale()); - ss << boost::locale::as::spellout << int_part; - return std::accumulate (tail.begin(), tail.end(), ss.str()); + auto num_to_words = [&ss](auto num) + { + ss.str(""); + ss << boost::locale::as::spellout << num; + return ss.str(); + }; + StrStrVec replacements = { + {"{int_part}", num_to_words(int_part)}, + {"{frac_part}", num_to_words(frac_part)}, + {"{value}", num_to_words(val)}, + {"{int_part_num}", std::to_string (int_part)}, + {"{frac_part_num}", std::to_string (frac_part)}, + {"{denom_num}", std::to_string (denom)}, + }; + + return string_replace_substring("{int_part} and {frac_part_num}/{denom_num}", replacements); } #ifdef _MSC_VER