From 0ed0be4ef233368ff21bcf8976a25bcd59b607a5 Mon Sep 17 00:00:00 2001 From: Geert Janssens Date: Fri, 30 Dec 2022 19:20:43 +0100 Subject: [PATCH] C++ - use more expressive std::accumulate algo rather than std::for_each It encapsulates the custom string concatenation bits into a lambda called "add_bullet_item" and streamlines the logic of when to add what to the summary. --- gnucash/gnome/assistant-stock-transaction.cpp | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/gnucash/gnome/assistant-stock-transaction.cpp b/gnucash/gnome/assistant-stock-transaction.cpp index 27fc521c723..f2418e1d0b3 100644 --- a/gnucash/gnome/assistant-stock-transaction.cpp +++ b/gnucash/gnome/assistant-stock-transaction.cpp @@ -899,20 +899,24 @@ to ensure proper recording."), new_date_str, last_split_date_str); // generate final summary message. Collates a header, the errors // and warnings. Then allow completion if errors is empty. - auto header = errors.empty() ? - N_("No errors found. Click Apply to create transaction.") : - N_("The following errors must be fixed:"); - auto summary = std::string { _(header) }; - auto summary_add = [&summary](auto a) { summary += "\n• "; summary += a; }; - std::for_each (errors.begin(), errors.end(), summary_add); + auto add_bullet_item = [](std::string& a, std::string& b)->std::string { return std::move(a) + "\n• " + b; }; + auto summary = std::string{}; if (errors.empty()) - std::for_each (infos.begin(), infos.end(), summary_add); + { + summary = _("No errors found. Click Apply to create transaction."); + summary = std::accumulate (infos.begin(), infos.end(), std::move (summary), add_bullet_item); + } + else + { + summary = _("The following errors must be fixed:"); + summary = std::accumulate (errors.begin(), errors.end(), std::move (summary), add_bullet_item); + } + if (!warnings.empty()) { - auto warnings_header = N_ ("The following warnings exist:"); summary += "\n\n"; - summary += _(warnings_header); - std::for_each (warnings.begin(), warnings.end(), summary_add); + summary += _("The following warnings exist:"); + summary = std::accumulate (warnings.begin(), warnings.end(), std::move (summary), add_bullet_item); } gtk_label_set_text (GTK_LABEL (info->finish_summary), summary.c_str()); gtk_assistant_set_page_complete (GTK_ASSISTANT (info->window),