[PATCH] Some new GString functions (second attempt)



As a result of the discussions above, here is a second attempt at a
patch with some new GString functions.

They are, in brief:

  GString* g_string_clone(GString*);

  GString* g_string_slice(GString*, guint, guint);

  GString* g_string_new_printf(const gchar*, ...);
  GString* g_string_new_vprintf(const gchar*, va_list);

  void     g_string_vprintf(GString*, const gchar*, va_list);
  void     g_string_append_vprintf(GString*, const gchar*, va_list);

I'd submitted the former two before, but the latter four are new,
following my question on the lack of va_list versions of the printf
functions.

I've also submitted a brief little test program, to demonstrate the use
of the _clone(), _slice() and _new_printf() functions. The use of the
va_list ones should largely be obvious from their "..." counterparts.

-- 
Paul "LeoNerd" Evans

leonerd leonerd org uk
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/
Index: docs/reference/glib/glib-sections.txt
===================================================================
RCS file: /cvs/gnome/glib/docs/reference/glib/glib-sections.txt,v
retrieving revision 1.135
diff -u -r1.135 glib-sections.txt
--- docs/reference/glib/glib-sections.txt	21 Dec 2005 04:45:56 -0000	1.135
+++ docs/reference/glib/glib-sections.txt	22 Dec 2005 02:26:13 -0000
@@ -1795,13 +1795,19 @@
 <FILE>strings</FILE>
 GString
 g_string_new
+g_string_clone
 g_string_new_len
 g_string_sized_new
+g_string_new_printf
+g_string_new_vprintf
+g_string_slice
 g_string_assign
 g_string_sprintf
 g_string_sprintfa
 g_string_printf
+g_string_vprintf
 g_string_append_printf
+g_string_append_vprintf
 g_string_append
 g_string_append_c
 g_string_append_unichar
Index: glib/gstring.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gstring.c,v
retrieving revision 1.49
diff -u -r1.49 gstring.c
--- glib/gstring.c	5 Dec 2005 15:38:52 -0000	1.49
+++ glib/gstring.c	22 Dec 2005 02:26:13 -0000
@@ -297,6 +297,26 @@
   return string;
 }
 
+/**
+ * g_string_clone:
+ * @orig: a #GString object to copy
+ *
+ * Creates a new #GString, initialised to contain the same data as @orig. If 
+ * @orig is %NULL then no new string is allocated and %NULL is returned 
+ * instead.
+ *
+ * Return value: a new #GString or %NULL
+ */
+
+GString*
+g_string_clone (const GString *orig)
+{
+  if (orig)
+    return g_string_new(orig->str);
+  else
+    return NULL;
+}
+
 GString*
 g_string_new_len (const gchar *init,
                   gssize       len)    
@@ -316,6 +336,49 @@
     }
 }
 
+/**
+ * g_string_new_printf:
+ * @format: the string format. See the %printf() documentation.
+ * @...: the parameters to insert into the format string.
+ *
+ * Allocates a new #GString object and fills the buffer with the printed
+ * string.
+ *
+ * Return value: a new #GString
+ */
+GString*
+g_string_new_printf(const gchar *format,
+                    ...)
+{
+  va_list args;
+  GString *string;
+
+  va_start (args, format);
+  string = g_string_new_vprintf (format, args);
+  va_end (args);
+
+  return string;
+}
+
+/**
+ * g_string_new_vprintf:
+ * @format: the string format. See the %printf() documentation.
+ * @args: the list of parameters to insert into the format string.
+ *
+ * Allocates a new #GString object and fills the buffer with the printed
+ * string.
+ *
+ * Return value: a new #GString
+ */
+GString*
+g_string_new_vprintf(const gchar *format,
+                     va_list args)
+{
+  GString *string = g_string_new (NULL);
+  g_string_append_vprintf (string, format, args);
+  return string;
+}
+
 gchar*
 g_string_free (GString *string,
 	       gboolean free_segment)
@@ -900,6 +963,44 @@
   g_free (buffer);
 }
 
+/**
+ * g_string_append_vprintf:
+ * @string: a #GString.
+ * @format: the string format. See the %printf() documentation.
+ * @args: the list of parameters to insert into the format string.
+ *
+ * Appends a formatted string onto the end of a #GString. This function is
+ * similar to #g_string_vprintf() except that the text is appended to the
+ * #GString
+ */
+void
+g_string_append_vprintf (GString *string,
+                         const gchar *format,
+                         va_list args)
+{
+  g_string_append_printf_internal (string, format, args);
+}
+
+/**
+ * g_string_vprintf:
+ * @string: a #GString.
+ * @format: the string format. See the %printf() documentation.
+ * @args: the list of parameters to insert into the format string.
+ * 
+ * Writes a formatted string into a #GString. This is similar to the standard
+ * %vsprintf() function, except that the #GString buffer automatically expands
+ * to contain the results. The previous contents of the #GString are
+ * destroyed.
+ */
+void
+g_string_vprintf (GString *string,
+                  const gchar *format,
+                  va_list args)
+{
+  g_string_truncate (string, 0);
+  g_string_append_printf_internal (string, format, args);
+}
+
 void
 g_string_printf (GString *string,
 		 const gchar *fmt,
@@ -924,6 +1025,38 @@
   va_start (args, fmt);
   g_string_append_printf_internal (string, fmt, args);
   va_end (args);
+}
+
+/**
+ * g_string_slice:
+ * @string: a #GString.
+ * @pos: The zero-based index of the start of the section to copy
+ * @len: The number of bytes to copy
+ *
+ * Creates a new #GString by taking a slice of the given string. The slice
+ * will begin at the @pos character, and contain at most @len bytes. The
+ * returned string will be nul-terminated.
+ *
+ * Return value: a new #GString
+ */
+
+GString*
+g_string_slice (GString* string,
+                guint pos,
+                guint len)
+{
+  GString *ret = g_string_new("");
+
+  if (pos > string->len || len < 1)
+    return ret;
+
+  if (pos + len > string->len)
+    len = string->len - pos;
+
+  g_string_append_len (ret, string->str + pos, len);
+  g_string_append_c (ret, '\0');
+
+  return ret;
 }
 
 #define __G_STRING_C__
Index: glib/gstring.h
===================================================================
RCS file: /cvs/gnome/glib/glib/gstring.h,v
retrieving revision 1.15
diff -u -r1.15 gstring.h
--- glib/gstring.h	2 Dec 2004 01:06:17 -0000	1.15
+++ glib/gstring.h	22 Dec 2005 02:26:14 -0000
@@ -59,9 +59,14 @@
 /* Strings
  */
 GString*     g_string_new	        (const gchar	 *init);
+GString*     g_string_clone             (const GString   *orig);
 GString*     g_string_new_len           (const gchar     *init,
                                          gssize           len);   
 GString*     g_string_sized_new         (gsize            dfl_size);  
+GString*     g_string_new_printf        (const gchar     *format,
+                                         ...) G_GNUC_PRINTF (1, 2);
+GString*     g_string_new_vprintf       (const gchar     *format,
+                                         va_list          args);
 gchar*	     g_string_free	        (GString	 *string,
 					 gboolean	  free_segment);
 gboolean     g_string_equal             (const GString	 *v,
@@ -112,9 +117,18 @@
 void         g_string_printf            (GString	 *string,
 					 const gchar	 *format,
 					 ...) G_GNUC_PRINTF (2, 3);
+void         g_string_vprintf           (GString         *string,
+                                         const gchar     *format,
+                                         va_list          args);
 void         g_string_append_printf     (GString	 *string,
 					 const gchar	 *format,
 					 ...) G_GNUC_PRINTF (2, 3);
+void         g_string_append_vprintf    (GString         *string,
+                                         const gchar     *format,
+                                         va_list          args);
+GString*     g_string_slice             (GString         *string,
+                                         guint            pos,
+                                         guint            len);
 
 /* -- optimize g_strig_append_c --- */
 #ifdef G_CAN_INLINE
#include <glib.h>
#include <stdio.h>

void printf_gstr_fields(GString *s)
{
  printf("[str=%p len=%d alloc=%d]\n", s->str, s->len, s->allocated_len);
}

int main(int argc, gchar *argv[])
{
  GString *s1 = g_string_new_printf("My string is %s", "here");
  printf("s1: \"%s\"\n", s1->str);
  printf_gstr_fields(s1);

  GString *s2 = g_string_clone(s1);
  printf("s2: \"%s\"\n", s2->str);
  printf_gstr_fields(s2);

  GString *s3 = g_string_slice(s2, 3, 9);
  printf("s3: \"%s\"\n", s3->str);
  printf_gstr_fields(s3);

  char XS[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  int l;
  for (l = 1; l < sizeof(XS); l++) {
    GString *s4 = g_string_new_printf("%.*s", l, &XS);
    printf("l=%d s4: \"%s\"\n", l, s4->str);
    printf_gstr_fields(s4);
    g_string_free(s4, TRUE);
  }

  return 0;
}

Attachment: signature.asc
Description: Digital signature



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