Skip to content

Commit b118339

Browse files
fixup... use an unordered_map to substitute in numeric format string
1 parent e86d4a9 commit b118339

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

libgnucash/app-utils/gnc-ui-util.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,18 +1500,40 @@ gnc_wrap_text_with_bidi_ltr_isolate (const char* text)
15001500
/********************************************************************\
15011501
********************************************************************/
15021502

1503+
using StrStrVec = std::vector<std::pair<std::string, std::string>>;
1504+
1505+
static std::string
1506+
string_replace_substring (const std::string& input, const StrStrVec& replacements)
1507+
{
1508+
std::string result = input;
1509+
for (const auto& [key, val] : replacements)
1510+
for (auto pos = result.find(key); pos != std::string::npos; pos = result.find(key))
1511+
result.replace(pos, key.length(), val);
1512+
return result;
1513+
}
1514+
15031515
static std::string
15041516
number_to_words(double val, int64_t denom)
15051517
{
15061518
double int_part;
15071519
const int frac_part = std::round(std::modf (std::fabs(val), &int_part) * denom);
1508-
const std::vector<std::string> tail =
1509-
{ " ", _("and"), " ", std::to_string (frac_part), "/", std::to_string (denom) };
1510-
std::ostringstream ss;
1520+
auto num_to_words = [](auto num)
1521+
{
1522+
std::ostringstream ss;
1523+
ss.imbue(gnc_get_boost_locale());
1524+
ss << boost::locale::as::spellout << num;
1525+
return ss.str();
1526+
};
1527+
StrStrVec replacements = {
1528+
{"{int_part}", num_to_words(int_part)},
1529+
{"{frac_part}", num_to_words(frac_part)},
1530+
{"{value}", num_to_words(val)},
1531+
{"{int_part_num}", std::to_string (int_part)},
1532+
{"{frac_part_num}", std::to_string (frac_part)},
1533+
{"{denom_num}", std::to_string (denom)},
1534+
};
15111535

1512-
ss.imbue(gnc_get_boost_locale());
1513-
ss << boost::locale::as::spellout << int_part;
1514-
return std::accumulate (tail.begin(), tail.end(), ss.str());
1536+
return string_replace_substring("{int_part} and {frac_part_num}/{denom_num}", replacements);
15151537
}
15161538

15171539
#ifdef _MSC_VER

0 commit comments

Comments
 (0)