From: Ryan Lortie Date: Fri, 15 Jun 2007 12:43:54 +0000 (+0000) Subject: Improve performance by removing the use of an intermediate g_malloc'd X-Git-Tag: GLIB_2_13_5~12 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=020af9f1dc6500443c546e22dc4d9f4e41e9ae86;p=platform%2Fupstream%2Fglib.git Improve performance by removing the use of an intermediate g_malloc'd 2007-06-14 Ryan Lortie * docs/reference/glib/glib-sections.txt: * glib/glib/symbols: * glib/gstring.[ch] (g_string_printf_internal): Improve performance by removing the use of an intermediate g_malloc'd buffer. Rename to g_string_append_vprintf, document, and expose along with g_string_vprintf as new public API (#57693). svn path=/trunk/; revision=5564 --- diff --git a/ChangeLog b/ChangeLog index 6e408d3..5cf1987 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2007-06-14 Ryan Lortie + + * docs/reference/glib/glib-sections.txt: + * glib/glib/symbols: + * glib/gstring.[ch] (g_string_printf_internal): Improve + performance by removing the use of an intermediate g_malloc'd + buffer. Rename to g_string_append_vprintf, document, and expose + along with g_string_vprintf as new public API (#57693). + 2007-06-15 Mathias Hasselmann * build, tests/string-test.c, glib/glib.symbols, diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index d445062..4de9920 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -2018,6 +2018,8 @@ g_string_sized_new g_string_assign g_string_sprintf g_string_sprintfa +g_string_vprintf +g_string_append_vprintf g_string_printf g_string_append_printf g_string_append diff --git a/glib/glib.symbols b/glib/glib.symbols index f507af9..a0645e4 100644 --- a/glib/glib.symbols +++ b/glib/glib.symbols @@ -1124,6 +1124,7 @@ g_string_append g_string_append_len g_string_append_printf G_GNUC_PRINTF(2,3) g_string_append_unichar +g_string_append_vprintf g_string_ascii_down g_string_ascii_up g_string_assign @@ -1157,6 +1158,7 @@ g_string_truncate g_string_down g_string_up #endif +g_string_vprintf #ifdef INCLUDE_INTERNAL_SYMBOLS /* these are not internal, but we don't want to alias them */ g_string_append_c diff --git a/glib/gstring.c b/glib/gstring.c index 3a36e1b..bf15f58 100644 --- a/glib/gstring.c +++ b/glib/gstring.c @@ -1260,17 +1260,54 @@ g_string_up (GString *string) return string; } -static void -g_string_append_printf_internal (GString *string, - const gchar *fmt, - va_list args) +/** + * g_string_append_vprintf: + * @string: a #GString. + * @format: the string format. See the printf() documentation. + * @args: the list of arguments to insert in the output. + * + * Appends a formatted string onto the end of a #GString. + * This function is is similar to g_string_append_printf() + * except that the arguments to the format string are passed + * as a va_list. + * + * Since: 2.14 + */ +void +g_string_append_vprintf (GString *string, + const gchar *fmt, + va_list args) { - gchar *buffer; - gint length; - - length = g_vasprintf (&buffer, fmt, args); - g_string_append_len (string, buffer, length); - g_free (buffer); + gsize length; + + g_return_if_fail (string != NULL); + g_return_if_fail (fmt != NULL); + + length = g_printf_string_upper_bound (fmt, args); + g_string_maybe_expand (string, length); + length = g_vsnprintf (string->str + string->len, length, fmt, args); + string->len += length; +} + +/** + * g_string_vprintf: + * @string: a #GString. + * @format: the string format. See the printf() documentation. + * @Varargs: the parameters to insert into the format string. + * + * Writes a formatted string into a #GString. This function + * is similar to g_string_printf() except that the arguments to + * the format string are passed as a va_list. + * + * Since: 2.14 + */ +void +g_string_vprintf (GString *string, + const gchar *fmt, + va_list args) +{ + g_string_truncate (string, 0); + g_string_append_vprintf (string, fmt, args); } /** @@ -1310,7 +1347,7 @@ g_string_printf (GString *string, g_string_truncate (string, 0); va_start (args, fmt); - g_string_append_printf_internal (string, fmt, args); + g_string_append_vprintf (string, fmt, args); va_end (args); } @@ -1346,7 +1383,7 @@ g_string_append_printf (GString *string, va_list args; va_start (args, fmt); - g_string_append_printf_internal (string, fmt, args); + g_string_append_vprintf (string, fmt, args); va_end (args); } diff --git a/glib/gstring.h b/glib/gstring.h index c4d423b..4201816 100644 --- a/glib/gstring.h +++ b/glib/gstring.h @@ -117,9 +117,15 @@ GString* g_string_erase (GString *string, gssize len); GString* g_string_ascii_down (GString *string); GString* g_string_ascii_up (GString *string); +void g_string_vprintf (GString *string, + const gchar *format, + va_list args); void g_string_printf (GString *string, const gchar *format, ...) G_GNUC_PRINTF (2, 3); +void g_string_append_vprintf (GString *string, + const gchar *format, + va_list args); void g_string_append_printf (GString *string, const gchar *format, ...) G_GNUC_PRINTF (2, 3);