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 77c3d25..6e408d3 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 4489a23..f507af9 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 b17bb1b..3a36e1b 100644 (file)
@@ -1023,6 +1023,76 @@ g_string_insert_unichar (GString *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
  * @pos: the position of the content to remove
index 7c6a713..c4d423b 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 4ba7fc2..f80c438 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");