From: Andrea Zagli Date: Sat, 2 Jun 2018 09:34:12 +0000 (+0200) Subject: Added functions ::string_replace_full and ::gstring_replace_full. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=cca3140902cc4492f36e3bc579dab5fa89561bf4;p=libzakutils Added functions ::string_replace_full and ::gstring_replace_full. --- diff --git a/src/generic.c b/src/generic.c index f3e0fd5..ea37b4e 100644 --- a/src/generic.c +++ b/src/generic.c @@ -81,20 +81,57 @@ gchar } /** - * zak_utils_gstring_replace: + * zak_utils_string_replace_full: + * @string: + * @origin: + * @replace: + * + */ +gchar +*zak_utils_string_replace_full (const gchar *string, + const gchar *origin, + const gchar *replace) +{ + gchar *ret; + gchar *p; + + if (string == NULL) return NULL; + if (origin == NULL || replace == NULL) return g_strdup (string); + + ret = g_strdup (string); + + do + { + p = g_strstr_len (ret, -1, origin); + + if (p == NULL) + { + return ret; + } + + ret = g_strndup (ret, p - ret); + + ret = g_strdup_printf ("%s%s%s", ret, replace, p + strlen (origin)); + } while (TRUE); + + return ret; +} + +/** + * zak_utils_gstring_replace_full: * @string: * @origin: * @replace: * */ void -zak_utils_gstring_replace (GString *string, - const gchar *origin, - const gchar *replace) +zak_utils_gstring_replace_full (GString *string, + const gchar *origin, + const gchar *replace) { gchar *str; - str = zak_utils_string_replace ((const gchar *)string->str, origin, replace); + str = zak_utils_string_replace_full ((const gchar *)string->str, origin, replace); g_string_assign (string, str); diff --git a/src/generic.h b/src/generic.h index 243346b..7b9c5f5 100644 --- a/src/generic.h +++ b/src/generic.h @@ -33,9 +33,15 @@ gboolean zak_utils_file_exists (const gchar *filename); gchar *zak_utils_string_replace (const gchar *string, const gchar *origin, const gchar *replace); +gchar *zak_utils_string_replace_full (const gchar *string, + const gchar *origin, + const gchar *replace); void zak_utils_gstring_replace (GString *string, const gchar *origin, const gchar *replace); +void zak_utils_gstring_replace_full (GString *string, + const gchar *origin, + const gchar *replace); gdouble zak_utils_round (gdouble value, guint n_decimals);