gstring.c tweaks




Hi,

Some changes to GString Owen suggested:

 - reimplement append/prepend in terms of insert, special-case 
   insert to make it handle the append more efficiently. 
   Reduces cut-and-paste and cleans things up
 - same for the append_c, prepend_c etc.
 - add insert_len(), append_len(), prepend_len() to append
   non-NULL-terminated string hunks. Especially useful if you 
   want to extract a substring and append it to a GString.
   insert_len takes -1 for the "pos" arg (means end of the string, 
   i.e append) and -1 for the "len" arg (means do a strlen())

So basically g_string_insert_len() is the only function that still
does anything and everything else is a convenience wrapper.       

I haven't tested it yet so it may have bugs. I can add some testglib
checks.

Havoc

Index: glib.h
===================================================================
RCS file: /cvs/gnome/glib/glib.h,v
retrieving revision 1.153
diff -u -u -r1.153 glib.h
--- glib.h	2000/02/03 20:43:00	1.153
+++ glib.h	2000/02/13 03:34:13
@@ -1830,14 +1830,26 @@
 					 const gchar	 *rval);
 GString*     g_string_truncate          (GString	 *string,
 					 gint		  len);
+
+GString*     g_string_insert_len        (GString         *string,
+                                         gint             pos,
+                                         const gchar     *val,
+                                         gint             len);
+
 GString*     g_string_append            (GString	 *string,
 			                 const gchar	 *val);
+GString*     g_string_append_len        (GString	 *string,
+			                 const gchar	 *val,
+                                         gint             len);
 GString*     g_string_append_c          (GString	 *string,
 					 gchar		  c);
 GString*     g_string_prepend           (GString	 *string,
 					 const gchar	 *val);
 GString*     g_string_prepend_c         (GString	 *string,
 					 gchar		  c);
+GString*     g_string_prepend_len       (GString	 *string,
+			                 const gchar	 *val,
+                                         gint             len);
 GString*     g_string_insert            (GString	 *string,
 					 gint		  pos,
 					 const gchar	 *val);
Index: gstring.c
===================================================================
RCS file: /cvs/gnome/glib/gstring.c,v
retrieving revision 1.13
diff -u -u -r1.13 gstring.c
--- gstring.c	1999/07/24 18:50:55	1.13
+++ gstring.c	2000/02/13 03:34:14
@@ -292,82 +292,83 @@
 }
 
 GString*
-g_string_append (GString *fstring,
-		 const gchar *val)
+g_string_insert_len        (GString         *string,
+                            gint             pos,
+                            const gchar     *val,
+                            gint             len)
 {
   GRealString *string = (GRealString*)fstring;
-  int len;
+  gint len;
 
   g_return_val_if_fail (string != NULL, NULL);
   g_return_val_if_fail (val != NULL, fstring);
+  g_return_val_if_fail (pos <= string->len, fstring);
+
+  if (len < 0)
+    len = strlen (val);
+
+  if (pos < 0)
+    pos = string->len;
   
-  len = strlen (val);
   g_string_maybe_expand (string, len);
+
+  /* If we aren't appending at the end, move a hunk
+     of the old string to the end, opening up space */
+  if (pos < string->len)
+    g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
 
-  strcpy (string->str + string->len, val);
+  /* Fill in the blank space */
+  strncpy (string->str + pos, val, len);
 
   string->len += len;
 
+  string->str[string->len] = 0;
+
   return fstring;
 }
 
 GString*
-g_string_append_c (GString *fstring,
-		   gchar c)
+g_string_append (GString *fstring,
+		 const gchar *val)
 {
-  GRealString *string = (GRealString*)fstring;
-
-  g_return_val_if_fail (string != NULL, NULL);
-  g_string_maybe_expand (string, 1);
+  return g_string_insert_len(fstring, -1, val, -1);
+}
 
-  string->str[string->len++] = c;
-  string->str[string->len] = 0;
+GString*
+g_string_append_len        (GString	 *string,
+                            const gchar	 *val,
+                            gint          len)
+{
+  return g_string_insert_len(string, -1, val, len);
+}
 
-  return fstring;
+GString*
+g_string_append_c (GString *fstring,
+		   gchar c)
+{
+  return g_string_insert_c(fstring, -1, c);
 }
 
 GString*
 g_string_prepend (GString *fstring,
 		  const gchar *val)
 {
-  GRealString *string = (GRealString*)fstring;
-  gint len;
-
-  g_return_val_if_fail (string != NULL, NULL);
-  g_return_val_if_fail (val != NULL, fstring);
-
-  len = strlen (val);
-  g_string_maybe_expand (string, len);
-
-  g_memmove (string->str + len, string->str, string->len);
-
-  strncpy (string->str, val, len);
-
-  string->len += len;
-
-  string->str[string->len] = 0;
+  return g_string_insert_len(fstring, 0, val, -1);
+}
 
-  return fstring;
+GString*
+g_string_prepend_len        (GString	 *string,
+                             const gchar	 *val,
+                             gint          len)
+{
+  return g_string_insert_len(string, 0, val, len);
 }
 
 GString*
 g_string_prepend_c (GString *fstring,
 		    gchar    c)
 {
-  GRealString *string = (GRealString*)fstring;
-
-  g_return_val_if_fail (string != NULL, NULL);
-  g_string_maybe_expand (string, 1);
-
-  g_memmove (string->str + 1, string->str, string->len);
-
-  string->str[0] = c;
-
-  string->len += 1;
-
-  string->str[string->len] = 0;
-
-  return fstring;
+  return g_string_insert_c(fstring, 0, c);
 }
 
 GString*
@@ -375,26 +376,7 @@
 		 gint         pos,
 		 const gchar *val)
 {
-  GRealString *string = (GRealString*)fstring;
-  gint len;
-
-  g_return_val_if_fail (string != NULL, NULL);
-  g_return_val_if_fail (val != NULL, fstring);
-  g_return_val_if_fail (pos >= 0, fstring);
-  g_return_val_if_fail (pos <= string->len, fstring);
-
-  len = strlen (val);
-  g_string_maybe_expand (string, len);
-
-  g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
-
-  strncpy (string->str + pos, val, len);
-
-  string->len += len;
-
-  string->str[string->len] = 0;
-
-  return fstring;
+  return g_string_insert_len(fstring, pos, val, -1);
 }
 
 GString *
@@ -409,7 +391,12 @@
 
   g_string_maybe_expand (string, 1);
 
-  g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
+  if (pos < 0)
+    pos = string->len;
+  
+  /* If not just an append, move the old stuff */
+  if (pos < string->len)
+    g_memmove (string->str + pos + 1, string->str + pos, string->len - pos);
 
   string->str[pos] = c;
 



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