Patch to add non-initialized space to a GString



    This patch allows g_string_[append,prepend,insert]_len
to take a NULL argument for the string and insert
uninitialized space into the string. This is useful,
e.g. in the new GIOChannel implementation (see bug 52811),
where GString is used as a buffer and read_raw (an
internal function) needs a character buffer to
read data into.
    This patch also modifies the
behavior of g_string_new_len to match the behavior
of the other functions. Previously, if passed a NULL,
g_string_new_len mimicked the behavior of
g_string_sized_new, making the (non user visible)
internal buffer of size len, but still making
the apparent length of the string zero. Now,
it makes the apparent length of the string
equal to len, but does not initialize the space.
    This change does not appear to affect the performance
of the code in the two locations that g_string_new_len
is called internally in glib, in gmarkup.c and
in gobject/gboxed.c.
    This has already been added to bugzilla as bug 54903.

					Ron Steinke

--- patch follows ---

diff -ur -x *.o -x *.lo -x .libs -x .deps -x docs -x test glib-buffered/gstring.c glib/gstring.c
--- glib-buffered/gstring.c	Thu May 17 09:27:40 2001
+++ glib/gstring.c	Thu May 17 13:12:34 2001
@@ -263,8 +263,7 @@
     {
       string = g_string_sized_new (len);
       
-      if (init)
-        g_string_append_len (string, init, len);
+      g_string_append_len (string, init, len);
       
       return string;
     }
@@ -372,7 +371,7 @@
   GRealString *string = (GRealString *) fstring;
 
   g_return_val_if_fail (string != NULL, NULL);
-  g_return_val_if_fail (val != NULL, fstring);
+  g_return_val_if_fail ((val != NULL) || (len >= 0), fstring);
   g_return_val_if_fail (pos <= string->len, fstring);
 
   if (len < 0)
@@ -390,7 +389,8 @@
     g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
   
   /* insert the new string */
-  g_memmove (string->str + pos, val, len);
+  if(val)
+    g_memmove (string->str + pos, val, len);
 
   string->len += len;
 
@@ -415,7 +415,7 @@
                      gint         len)
 {
   g_return_val_if_fail (string != NULL, NULL);
-  g_return_val_if_fail (val != NULL, string);
+  g_return_val_if_fail ((val != NULL) || (len >= 0), string);
 
   return g_string_insert_len (string, -1, val, len);
 }
@@ -445,7 +445,7 @@
                       gint         len)
 {
   g_return_val_if_fail (string != NULL, NULL);
-  g_return_val_if_fail (val != NULL, string);
+  g_return_val_if_fail ((val != NULL) || (len >= 0), string);
 
   return g_string_insert_len (string, 0, val, len);
 }




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]