Move gnc_list_formatter from gnc-date to gnc-ui-util.

It has nothing at all to do with dates.
This commit is contained in:
John Ralls 2024-01-06 18:16:31 -08:00
parent d52d226e5b
commit c87d480b0d
4 changed files with 40 additions and 41 deletions

View File

@ -42,6 +42,7 @@
#include <stdio.h>
#include <string.h>
#include <cinttypes>
#include <unicode/listformatter.h>
#include "qof.h"
#include "gnc-prefs.h"
@ -62,6 +63,8 @@
#define GNC_PREF_REVERSED_ACCTS_INC_EXP "reversed-accounts-incomeexpense"
#define GNC_PREF_PRICES_FORCE_DECIMAL "force-price-decimal"
using UniStr = icu::UnicodeString;
static QofLogModule log_module = GNC_MOD_GUI;
static bool auto_decimal_enabled = false;
@ -2281,3 +2284,31 @@ gnc_filter_text_for_currency_commodity (const gnc_commodity *comm,
return gnc_filter_text_for_currency_symbol (incoming_text, *symbol);
}
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<UniStr> strvec;
UniStr result;
std::string retval;
for (auto n = strings; n; n = g_list_next (n))
{
auto utf8_str{static_cast<const char*>(n->data)};
strvec.push_back (UniStr::fromUTF8(utf8_str));
}
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

@ -458,6 +458,15 @@ char* gnc_filter_text_for_currency_commodity (const gnc_commodity *comm,
const char* incoming_text,
const char** symbol);
/** 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);
#ifdef __cplusplus
}
#endif

View File

@ -46,7 +46,6 @@
#include <cinttypes>
#include <unicode/calendar.h>
#include <unicode/listformatter.h>
#include "gnc-date.h"
#include "gnc-date-p.h"
@ -1651,32 +1650,3 @@ 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))
{
auto utf8_str{static_cast<const char*>(n->data)};
strvec.push_back (icu::UnicodeString::fromUTF8(utf8_str));
}
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,17 +813,6 @@ 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);
//@}
//@}