Introduce g_string_overwrite(_len)? for overwriting parts of strings (#368686, Samuel...
authorMathias Hasselmann <hasselmm@src.gnome.org>
Fri, 15 Jun 2007 11:54:21 +0000 (11:54 +0000)
committerMathias Hasselmann <hasselmm@src.gnome.org>
Fri, 15 Jun 2007 11:54:21 +0000 (11:54 +0000)
svn path=/trunk/; revision=5563

ChangeLog
glib/glib.symbols
glib/gstring.c
glib/gstring.h
tests/string-test.c

index 77c3d256ddc88ee7f091f6f94a7cb051ff6b49f6..6e408d39e5e223380c0b14a87a8f460dc2fec8ab 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-06-15  Mathias Hasselmann  <mathias.hasselmann@gmx.de>
+
+       * build, tests/string-test.c, glib/glib.symbols, 
+       glib/gstring.c, glib/gstring.h: Introduce g_string_overwrite(_len)?
+       for overwriting parts of strings (#368686, Samuel Cormier-Iijima)
+
 2007-06-14  Cody Russell  <bratsche@gnome.org>
 
        * gobject/gtype.c (g_type_class_add_private): Check for 0-sized
index 4489a2347f6d5d8f27213ad9f3b48fa31010d360..f507af93da3a443299e8302d685d96579ba36bb1 100644 (file)
@@ -1143,6 +1143,8 @@ g_string_insert_len
 g_string_insert_unichar
 g_string_new
 g_string_new_len
+g_string_overwrite
+g_string_overwrite_len
 g_string_prepend
 g_string_prepend_c
 g_string_prepend_len
index b17bb1be53389fa9445e206222b96630a8e47dd8..3a36e1beff93bf1f9ce0c8f2abed6b76151b2994 100644 (file)
@@ -1022,6 +1022,76 @@ g_string_insert_unichar (GString *string,
   return string;
 }
 
+/**
+ * g_string_overwrite:
+ * @string: a #GString
+ * @pos: the position at which to start overwriting
+ * @val: the string that will overwrite the @string starting at @pos
+ * 
+ * Overwrites part of a string, lengthening it if necessary.
+ * 
+ * Return value: @string
+ *
+ * Since: 2.14
+ **/
+GString *
+g_string_overwrite (GString     *string,
+                   gsize        pos,
+                   const gchar *val)
+{
+  g_return_val_if_fail (val != NULL, string);
+  return g_string_overwrite_len (string, pos, val, strlen (val));
+}
+
+/**
+ * g_string_overwrite_len:
+ * @string: a #GString
+ * @pos: the position at which to start overwriting
+ * @val: the string that will overwrite the @string starting at @pos
+ * @len: the number of bytes to write from @val
+ * 
+ * Overwrites part of a string, lengthening it if necessary. This function
+ * will work with embedded NULLs.
+ * 
+ * Return value: @string
+ *
+ * Since: 2.14
+ **/
+GString *
+g_string_overwrite_len (GString       *string,
+                       gsize          pos,
+                       const gchar   *val,
+                       gssize         len)
+{
+  gsize end;
+
+  g_return_val_if_fail (string != NULL, NULL);
+
+  if (!len)
+    return string;
+
+  g_return_val_if_fail (val != NULL, string);
+  g_return_val_if_fail (pos <= string->len, string);
+
+  if (len < 0)
+    len = strlen (val);
+
+  end = pos + len;
+
+  if (end > string->len)
+    g_string_maybe_expand (string, end - string->len);
+
+  memcpy (string->str + pos, val, len);
+
+  if (end > string->len)
+    {
+      string->str[end] = '\0';
+      string->len = end;
+    }
+
+  return string;
+}
+
 /**
  * g_string_erase:
  * @string: a #GString
index 7c6a713b0b7328f90c4b0844d6d5805a6118338b..c4d423b3cad425c687abd6c317b9a4c046e2b8f2 100644 (file)
@@ -105,6 +105,13 @@ GString*     g_string_insert_c          (GString    *string,
 GString*     g_string_insert_unichar    (GString        *string,
                                         gssize           pos,    
                                         gunichar         wc);
+GString*     g_string_overwrite         (GString        *string,
+                                        gsize            pos,    
+                                        const gchar     *val);
+GString*     g_string_overwrite_len     (GString        *string,
+                                        gsize            pos,    
+                                        const gchar     *val,
+                                        gssize           len);
 GString*     g_string_erase            (GString         *string,
                                         gssize           pos,
                                         gssize           len);
index 4ba7fc21886fd99bce8704ffdcf0ee581a9612fb..f80c438602395c1b1a4a2bd159612585bff49e24 100644 (file)
@@ -257,7 +257,27 @@ main (int   argc,
   g_assert (g_string_equal(string1, string2));
   g_string_free (string1, TRUE);
   g_string_free (string2, TRUE);
-  
+
+  /* overwriting functions */
+  string1 = g_string_new ("testing");
+
+  g_string_overwrite (string1, 4, " and expand");
+  g_assert (15 == string1->len);
+  g_assert ('\0' == string1->str[15]);
+  g_assert (g_str_equal ("test and expand", string1->str));
+
+  g_string_overwrite (string1, 5, "NOT-");
+  g_assert (15 == string1->len);
+  g_assert ('\0' == string1->str[15]);
+  g_assert (g_str_equal ("test NOT-expand", string1->str));
+
+  g_string_overwrite_len (string1, 9, "blablabla", 6);
+  g_assert (15 == string1->len);
+  g_assert ('\0' == string1->str[15]);
+  g_assert (g_str_equal ("test NOT-blabla", string1->str));
+
+  g_string_free (string1, TRUE);
+
   /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
   string1 = g_string_new ("fiddle");
   string2 = g_string_new ("fiddle");