gstring patch




Here's the gstring patch with hopefully all the little details fixed
(assertions, indentation, etc)

Havoc

Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/glib/ChangeLog,v
retrieving revision 1.355
diff -u -u -r1.355 ChangeLog
--- ChangeLog	2000/02/13 07:18:22	1.355
+++ ChangeLog	2000/02/17 05:39:22
@@ -1,3 +1,24 @@
+2000-02-13  Havoc Pennington  <hp@pobox.com>
+
+	* tests/string-test.c (main): Add tests for the new GString 
+	features
+
+	* testglib.c (main): Add tests for the new GString features
+
+	* gstring.c (g_string_insert_len): New function; insert 
+	a given length of string at a given position. 
+	(g_string_append): reimplement in terms of g_string_insert_len
+	(g_string_append_len): new function
+	(g_string_insert_c): accept -1 for "pos" arg to mean "append"
+	(g_string_append_c): reimplement in terms of g_string_insert_c
+	(g_string_prepend): reimplement in terms of g_string_insert_len
+	(g_string_prepend_len): new function
+	(g_string_prepend_c): reimplement in terms of g_string_insert_c
+	(g_string_insert): reimplement in terms of g_string_insert_len
+
+	* glib.h: Declare g_string_insert_len, g_string_append_len,
+	g_string_prepend_len
+	
 Sun Feb 13 08:16:47 2000  Tim Janik  <timj@gtk.org>
 
 	* configure.in: wtf??? someone destroyed the configure.in, reverting to
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/17 05:39:32
@@ -1830,14 +1830,24 @@
 					 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/17 05:39:33
@@ -292,82 +292,98 @@
 }
 
 GString*
-g_string_append (GString *fstring,
-		 const gchar *val)
+g_string_insert_len        (GString         *fstring,
+                            gint             pos,
+                            const gchar     *val,
+                            gint             len)
 {
   GRealString *string = (GRealString*)fstring;
-  int 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);
 
-  strcpy (string->str + string->len, val);
+  /* 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);
+  
+  /* 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)
-{
-  GRealString *string = (GRealString*)fstring;
+g_string_append (GString *fstring,
+		 const gchar *val)
+{  
+  g_return_val_if_fail (fstring != NULL, NULL);
+  g_return_val_if_fail (val != NULL, fstring);
+
+  return g_string_insert_len (fstring, -1, val, -1);
+}
 
+GString*
+g_string_append_len (GString	 *string,
+                     const gchar *val,
+                     gint         len)
+{
   g_return_val_if_fail (string != NULL, NULL);
-  g_string_maybe_expand (string, 1);
+  g_return_val_if_fail (val != NULL, string);
 
-  string->str[string->len++] = c;
-  string->str[string->len] = 0;
+  return g_string_insert_len (string, -1, val, len);
+}
 
-  return fstring;
+GString*
+g_string_append_c (GString *fstring,
+		   gchar c)
+{
+  g_return_val_if_fail (fstring != NULL, NULL);
+  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 (fstring != 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 fstring;
+  
+  return g_string_insert_len (fstring, 0, val, -1);
 }
 
 GString*
-g_string_prepend_c (GString *fstring,
-		    gchar    c)
+g_string_prepend_len (GString	 *string,
+                      const gchar  *val,
+                      gint          len)
 {
-  GRealString *string = (GRealString*)fstring;
-
   g_return_val_if_fail (string != NULL, NULL);
-  g_string_maybe_expand (string, 1);
+  g_return_val_if_fail (val != NULL, string);
 
-  g_memmove (string->str + 1, string->str, string->len);
-
-  string->str[0] = c;
-
-  string->len += 1;
-
-  string->str[string->len] = 0;
+  return g_string_insert_len (string, 0, val, len);
+}
 
-  return fstring;
+GString*
+g_string_prepend_c (GString *fstring,
+		    gchar    c)
+{  
+  g_return_val_if_fail (fstring != NULL, NULL);
+  
+  return g_string_insert_c (fstring, 0, c);
 }
 
 GString*
@@ -375,26 +391,11 @@
 		 gint         pos,
 		 const gchar *val)
 {
-  GRealString *string = (GRealString*)fstring;
-  gint len;
-
-  g_return_val_if_fail (string != NULL, NULL);
+  g_return_val_if_fail (fstring != 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;
+  g_return_val_if_fail (pos <= fstring->len, fstring);
+  
+  return g_string_insert_len (fstring, pos, val, -1);
 }
 
 GString *
@@ -409,7 +410,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;
 
Index: testglib.c
===================================================================
RCS file: /cvs/gnome/glib/testglib.c,v
retrieving revision 1.31
diff -u -u -r1.31 testglib.c
--- testglib.c	1999/11/01 20:46:44	1.31
+++ testglib.c	2000/02/17 05:39:35
@@ -726,6 +726,65 @@
   g_print ("and next 70:\n%s\n", string2->str+142);
   g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
 
+  g_string_free (string1, TRUE);
+  g_string_free (string2, TRUE);
+
+  /* append */
+  string1 = g_string_new ("firsthalf");
+  g_string_append (string1, "lasthalf");
+  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+  g_string_free (string1, TRUE);
+
+  /* append_len */
+
+  string1 = g_string_new ("firsthalf");
+  g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
+  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+  g_string_free (string1, TRUE);  
+  
+  /* prepend */
+  string1 = g_string_new ("lasthalf");
+  g_string_prepend (string1, "firsthalf");
+  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+  g_string_free (string1, TRUE);
+
+  /* prepend_len */
+  string1 = g_string_new ("lasthalf");
+  g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
+  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+  g_string_free (string1, TRUE);
+  
+  /* insert */
+  string1 = g_string_new ("firstlast");
+  g_string_insert (string1, 5, "middle");
+  g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
+  g_string_free (string1, TRUE);
+
+  /* insert with pos == end of the string */
+  string1 = g_string_new ("firstmiddle");
+  g_string_insert (string1, strlen ("firstmiddle"), "last");
+  g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
+  g_string_free (string1, TRUE);
+  
+  /* insert_len */
+
+  string1 = g_string_new ("firstlast");
+  g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
+  g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
+  g_string_free (string1, TRUE);
+
+  /* insert_len with magic -1 pos for append */
+  string1 = g_string_new ("first");
+  g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
+  g_assert (strcmp (string1->str, "firstlast") == 0);
+  g_string_free (string1, TRUE);
+  
+  /* insert_len with magic -1 len for strlen-the-string */
+  string1 = g_string_new ("first");
+  g_string_insert_len (string1, 5, "last", -1);
+  g_assert (strcmp (string1->str, "firstlast") == 0);
+  g_string_free (string1, TRUE);
+  
   g_print ("ok\n");
 
   g_print ("checking timers...\n");
Index: tests/string-test.c
===================================================================
RCS file: /cvs/gnome/glib/tests/string-test.c,v
retrieving revision 1.7
diff -u -u -r1.7 string-test.c
--- tests/string-test.c	1999/10/05 16:21:07	1.7
+++ tests/string-test.c	2000/02/17 05:39:35
@@ -120,6 +120,65 @@
 		    string1->str,
 		    10, 666, 15, 15, 666.666666666, 666.666666666);
 #endif
+  
+  g_string_free (string1, TRUE);
+  g_string_free (string2, TRUE);
+
+  /* append */
+  string1 = g_string_new ("firsthalf");
+  g_string_append (string1, "lasthalf");
+  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+  g_string_free (string1, TRUE);
+
+  /* append_len */
+
+  string1 = g_string_new ("firsthalf");
+  g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
+  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+  g_string_free (string1, TRUE);  
+  
+  /* prepend */
+  string1 = g_string_new ("lasthalf");
+  g_string_prepend (string1, "firsthalf");
+  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+  g_string_free (string1, TRUE);
+
+  /* prepend_len */
+  string1 = g_string_new ("lasthalf");
+  g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
+  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
+  g_string_free (string1, TRUE);
+  
+  /* insert */
+  string1 = g_string_new ("firstlast");
+  g_string_insert (string1, 5, "middle");
+  g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
+  g_string_free (string1, TRUE);
+
+  /* insert with pos == end of the string */
+  string1 = g_string_new ("firstmiddle");
+  g_string_insert (string1, strlen ("firstmiddle"), "last");
+  g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
+  g_string_free (string1, TRUE);
+  
+  /* insert_len */
+
+  string1 = g_string_new ("firstlast");
+  g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
+  g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
+  g_string_free (string1, TRUE);
+
+  /* insert_len with magic -1 pos for append */
+  string1 = g_string_new ("first");
+  g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
+  g_assert (strcmp (string1->str, "firstlast") == 0);
+  g_string_free (string1, TRUE);
+  
+  /* insert_len with magic -1 len for strlen-the-string */
+  string1 = g_string_new ("first");
+  g_string_insert_len (string1, 5, "last", -1);
+  g_assert (strcmp (string1->str, "firstlast") == 0);
+  g_string_free (string1, TRUE);
 
   return 0;
 }



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