gconf_quote_percents(const gchar* src)

Philip Van Hoof <[email protected]>
Newsgroups gmane.comp.gnome.lib.gconf
Message-ID <[email protected]>
Hi there,

While studying the code of GConf, I came across an oddity.

gconf_quote_percents(const gchar* src) in gconf-internals.c sais in a
comment it's
wasting memory. I don't see why it would need to do that ... given the
fact that it's
just quoting a character.

Note, after differencing with the latest in HEAD the function is now
called gchar*
gconf_quote_string (const gchar* src) but it's ways of doing things are
exactly
the same as before (it just adds one other quotation-type).

As far as I can see it can, however, very easily be programmed in such a
way
that it doesn't need to waste any memory. For example (simplified
version):

We have glib so why use pointer tricks :-)? Just use an auto-growing
GString...

gchar*
gconf_quote_string (const gchar* src)
{
   const gchar *text = src;

  gint i=0, len = strlen(text);
  GString *str = g_string_new ("");
  gchar *retval = NULL;


  /* You can also loop using the pointer of course (simplified) */
  while (i < len) {


	/* Add one extra '%'-character for quotation */
	if (text[i] == '%')
		str = g_string_append_c (str, '%');


	/* Add one extra '\'-character for quotation */
	if (text[i] == '\\')
		str = g_string_append_c (str, '\\');

	/* Add the character to the result (also the original '%') */
	str = g_string_append_c (str, text[i]);
	i++;
  }

  /* Steal the gchar-pointer of the GString before freeing it */
  retval = str->str;

  /* Free all what we don't need from the GString */
  g_string_free (str, FALSE);

  /* Return the stolen gchar-pointer */
  return retval; 

}

Shall I provide a patch for just gconf-internals.c or immediately commit
this (my user is pvanhoof).



-- 
Philip Van Hoof, Software Developer @ Cronos
home: me at freax dot org
gnome: pvanhoof at gnome dot org
work: philip dot vanhoof at cronos dot be
junk: philip dot vanhoof at gmail dot com
http://www.freax.be, http://www.freax.eu.org

_______________________________________________
gconf-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/gconf-list
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.