Skip to content

Commit

Permalink
Bug 799439 - Duplicate information in description when importing...
Browse files Browse the repository at this point in the history
CAMT.053.001.02 via aqbanking

Deduplicate the description inputs when joining them by creating a
variant of gnc_g_list_stringjoin named gnc_g_list_stringjoin_nodups
that tests each string against the agregate string and adds it only if
it's not already included. Note that while it uses g_utf8_normalize to
ensure that non-ascii strings are consistently composed it has to do a
bytewise comparison with strstr as glib doesn't provide a UTF-8
equivalent. strnocasestr would work only with ASCII strings so it's
not suitable.
  • Loading branch information
jralls committed Dec 10, 2024
1 parent f0f875d commit 6c21687
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gnucash/import-export/aqb/gnc-ab-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ gnc_ab_description_to_gnc (const AB_TRANSACTION *ab_trans, gboolean is_ofx)
acc = g_list_prepend (acc, gnc_ab_get_remote_name (ab_trans));
acc = g_list_prepend (acc, gnc_ab_get_purpose (ab_trans, is_ofx));
acc = g_list_prepend (acc, ab_ultimate_creditor_debtor_to_gnc (ab_trans, is_ofx));
retval = gnc_g_list_stringjoin (acc, "; ");
retval = gnc_g_list_stringjoin_nodups (acc, "; ");

g_list_free_full (acc, g_free);
return retval ? retval : g_strdup (_("Unspecified"));
Expand Down
45 changes: 40 additions & 5 deletions libgnucash/core-utils/gnc-glib-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@

#include <config.h>
#include <errno.h>
#include <mach/arm/boolean.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdbool.h>

#include "gnc-glib-utils.h"

Expand Down Expand Up @@ -287,9 +289,25 @@ gnc_g_list_cut(GList **list, GList *cut_point)
cut_point->prev = NULL;
}

static bool
utf8_strstr(char **needle, char *haystack)
{
char *tmp = g_utf8_normalize (*needle, -1, G_NORMALIZE_NFC);
if (haystack && *haystack)
{
char *place = strstr(haystack, tmp);
if (place)
{
g_free (tmp);
return false;
}
}
*needle = tmp; //so that haystack is already normalized
return true;
}

gchar *
gnc_g_list_stringjoin (GList *list_of_strings, const gchar *sep)
static gchar *
gnc_g_list_stringjoin_internal (GList *list_of_strings, const gchar *sep, bool testdups)
{
gint seplen = sep ? strlen(sep) : 0;
gint length = -seplen;
Expand All @@ -311,14 +329,31 @@ gnc_g_list_stringjoin (GList *list_of_strings, const gchar *sep)
gchar *str = n->data;
if (!str || !str[0])
continue;
if (sep && (p != retval))
p = g_stpcpy (p, sep);
p = g_stpcpy (p, str);
if (!testdups || utf8_strstr (&str, retval))
{
if (sep && (p != retval))
p = g_stpcpy (p, sep);
p = g_stpcpy (p, str);
if (testdups)
g_free (str);
}
}

return retval;
}

gchar *
gnc_g_list_stringjoin (GList *list_of_strings, const gchar *sep)
{
return gnc_g_list_stringjoin_internal (list_of_strings, sep, false);
}

gchar *
gnc_g_list_stringjoin_nodups (GList *list_of_strings, const gchar *sep)
{
return gnc_g_list_stringjoin_internal (list_of_strings, sep, true);
}

gint
gnc_list_length_cmp (const GList *list, size_t len)
{
Expand Down
13 changes: 13 additions & 0 deletions libgnucash/core-utils/gnc-glib-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ void gnc_g_list_cut(GList **list, GList *cut_point);
* caller.
**/
gchar * gnc_g_list_stringjoin (GList *list_of_strings, const gchar *sep);
/**
* @brief Like stringjoin but ensures that the string to be added isn't
* already part of the return string.
*
* @param list_of_strings A GList of chars*
*
* @param sep a separator or NULL
*
* @return A newly allocated string that has to be g_free'd by the
* caller.
**/
gchar * gnc_g_list_stringjoin_nodups (GList *list_of_strings, const gchar *sep);

/**
* @brief Scans the GList elements the minimum number of iterations
* required to test it against a specified size. Returns -1, 0 or 1
Expand Down
19 changes: 19 additions & 0 deletions libgnucash/core-utils/test/test-gnc-glib-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ test_g_list_stringjoin (gconstpointer data)
g_list_free (test);
}

static void
test_g_list_stringjoin_nodups (gconstpointer data)
{
GList *test = NULL;
gchar *ret;

test = g_list_prepend (test, "one");
test = g_list_prepend (test, "two");
test = g_list_prepend (test, "two");
test = g_list_prepend (test, "three");
test = g_list_prepend (test, "one:two");
test = g_list_prepend (test, "four");
test = g_list_reverse (test);
ret = gnc_g_list_stringjoin_nodups (test, ":");
g_assert_cmpstr (ret, ==, "one:two:three:four");
g_free (ret);
}

static void
test_gnc_list_length (gconstpointer data)
{
Expand Down Expand Up @@ -146,6 +164,7 @@ main (int argc, char *argv[])
g_test_add_data_func ("/core-utils/gnc_utf8_strip_invalid_and_controls invalid utf8", (gconstpointer)invalid_utf8, test_gnc_utf8_strip_invalid_and_controls);
g_test_add_data_func ("/core-utils/gnc_utf8_strip_invalid_and_controls control chars", (gconstpointer)controls, test_gnc_utf8_strip_invalid_and_controls);
g_test_add_data_func ("/core-utils/gnc_g_list_stringjoin", NULL, test_g_list_stringjoin);
g_test_add_data_func ("/core-utils/gnc_g_list_stringjoin_nodups", NULL, test_g_list_stringjoin_nodups);
g_test_add_data_func ("/core-utils/gnc_list_length", NULL, test_gnc_list_length);

return g_test_run();
Expand Down

0 comments on commit 6c21687

Please sign in to comment.