]> saetta.ns0.it Git - libzakutils/commitdiff
Added functions ::string_replace_full and ::gstring_replace_full.
authorAndrea Zagli <azagli@libero.it>
Sat, 2 Jun 2018 09:34:12 +0000 (11:34 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 2 Jun 2018 09:34:12 +0000 (11:34 +0200)
src/generic.c
src/generic.h

index f3e0fd505d9581cff555007e7d82aa71a48a7ad6..ea37b4ea3310a8b5306230a08eaf06837459b3bf 100644 (file)
@@ -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);
 
index 243346b95aacd646e31c2f8c97ca5e453f789d31..7b9c5f5ba302a0d6ab6adf760885fbe25b4442e9 100644 (file)
@@ -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);