use icu::ListFormatter to combine a list strings into a string

This commit is contained in:
Christopher Lam 2023-10-02 12:28:09 +08:00
parent fc0e80bbad
commit ab7ead39ca
2 changed files with 38 additions and 0 deletions

View File

@ -46,6 +46,7 @@
#include <cinttypes>
#include <unicode/calendar.h>
#include <unicode/listformatter.h>
#include "gnc-date.h"
#include "gnc-date-p.h"
@ -1652,3 +1653,29 @@ gnc_date_load_funcs (void)
Testfuncs *tf = g_slice_new (Testfuncs);
return tf;
}
gchar*
gnc_list_formatter (GList *strings)
{
g_return_val_if_fail (strings, nullptr);
UErrorCode status = U_ZERO_ERROR;
auto formatter = icu::ListFormatter::createInstance(status);
std::vector<icu::UnicodeString> strvec;
icu::UnicodeString result;
std::string retval;
for (auto n = strings; n; n = g_list_next (n))
strvec.push_back (static_cast<char*>(n->data));
formatter->format (strvec.data(), strvec.size(), result, status);
if (U_FAILURE(status))
PERR ("Unicode error");
else
result.toUTF8String(retval);
delete formatter;
return g_strdup (retval.c_str());
}

View File

@ -813,6 +813,17 @@ void gnc_gdate_set_prev_fiscal_year_start (GDate *date, const GDate *year_end);
* fiscal year. The year field of this argument is ignored. */
void gnc_gdate_set_prev_fiscal_year_end (GDate *date, const GDate *year_end);
/** This function takes a GList of char*, and uses locale-sensitive
* list formatter.
*
* @param strings The GList* of char*.
*
* @returns a newly allocated char*
*/
gchar* gnc_list_formatter (GList* strings);
//@}
//@}