Skip to content

Commit ab7ead3

Browse files
use icu::ListFormatter to combine a list strings into a string
1 parent fc0e80b commit ab7ead3

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

libgnucash/engine/gnc-date.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
#include <cinttypes>
4848
#include <unicode/calendar.h>
49+
#include <unicode/listformatter.h>
4950

5051
#include "gnc-date.h"
5152
#include "gnc-date-p.h"
@@ -1652,3 +1653,29 @@ gnc_date_load_funcs (void)
16521653
Testfuncs *tf = g_slice_new (Testfuncs);
16531654
return tf;
16541655
}
1656+
1657+
1658+
gchar*
1659+
gnc_list_formatter (GList *strings)
1660+
{
1661+
g_return_val_if_fail (strings, nullptr);
1662+
1663+
UErrorCode status = U_ZERO_ERROR;
1664+
auto formatter = icu::ListFormatter::createInstance(status);
1665+
std::vector<icu::UnicodeString> strvec;
1666+
icu::UnicodeString result;
1667+
std::string retval;
1668+
1669+
for (auto n = strings; n; n = g_list_next (n))
1670+
strvec.push_back (static_cast<char*>(n->data));
1671+
1672+
formatter->format (strvec.data(), strvec.size(), result, status);
1673+
1674+
if (U_FAILURE(status))
1675+
PERR ("Unicode error");
1676+
else
1677+
result.toUTF8String(retval);
1678+
1679+
delete formatter;
1680+
return g_strdup (retval.c_str());
1681+
}

libgnucash/engine/gnc-date.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,17 @@ void gnc_gdate_set_prev_fiscal_year_start (GDate *date, const GDate *year_end);
813813
* fiscal year. The year field of this argument is ignored. */
814814
void gnc_gdate_set_prev_fiscal_year_end (GDate *date, const GDate *year_end);
815815

816+
817+
818+
/** This function takes a GList of char*, and uses locale-sensitive
819+
* list formatter.
820+
*
821+
* @param strings The GList* of char*.
822+
*
823+
* @returns a newly allocated char*
824+
*/
825+
gchar* gnc_list_formatter (GList* strings);
826+
816827
//@}
817828

818829
//@}

0 commit comments

Comments
 (0)