gconf_quote_percents(const gchar* src)
- From: Philip Van Hoof <spamfrommailing freax org>
- To: gconf-list gnome org
- Cc: Havoc Pennington <hp redhat com>
- Subject: gconf_quote_percents(const gchar* src)
- Date: Tue, 08 Mar 2005 09:50:27 +0100
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
|
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]