Re: gstring.c tweaks



On 12 Feb 2000, Havoc Pennington wrote:

> 
> 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.       

sounds good.

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

then do some testing first, especially tests for pos > string->len and
similar cases are important.

> 
> 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);

get rid of those newlines.

> 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);

why did you get rid of this g_return_val_if_fail() statement?
what happens if pos > string->len? how would that be usefull?

> +
> +  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 */

comments are either this style for multi lines:
/* this is a multi
 * line comment
 */
or like this:
/* this is a single line comment */

> +  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);
> +}

keep the g_return_val_if_fail() statements.

> -  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);
> +}

put a space before the opening brace.

---
ciaoTJ



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