From 1c586e44befe07812ac1e4473ef9e3a76330b78e Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Thu, 11 Jul 2013 12:16:44 -0400 Subject: [PATCH] Add g_variant_new_printf Add a quick way to get a floating GVariant from printf formatting. --- docs/reference/glib/glib-sections.txt | 1 + glib/gvariant.c | 38 +++++++++++++++++++++++++++ glib/gvariant.h | 3 +++ glib/tests/gvariant.c | 21 +++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index b7dc06b3d..54c7b6d16 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -3117,6 +3117,7 @@ g_variant_new_handle g_variant_new_double g_variant_new_string g_variant_new_take_string +g_variant_new_printf g_variant_new_object_path g_variant_is_object_path g_variant_new_signature diff --git a/glib/gvariant.c b/glib/gvariant.c index 62cb4f874..2197cb07d 100644 --- a/glib/gvariant.c +++ b/glib/gvariant.c @@ -1309,6 +1309,44 @@ g_variant_new_take_string (gchar *string) return value; } +/** + * g_variant_new_printf: (skip) + * @format_string: a printf-style format string + * @...: arguments for @format_string + * + * Creates a string-type GVariant using printf formatting. + * + * This is similar to calling g_strdup_printf() and then + * g_variant_new_string() but it saves a temporary variable and an + * unnecessary copy. + * + * Returns: (transfer none): a floating reference to a new string + * #GVariant instance + * + * Since: 2.38 + **/ +GVariant * +g_variant_new_printf (const gchar *format_string, + ...) +{ + GVariant *value; + GBytes *bytes; + gchar *string; + va_list ap; + + g_return_val_if_fail (format_string != NULL, NULL); + + va_start (ap, format_string); + string = g_strdup_vprintf (format_string, ap); + va_end (ap); + + bytes = g_bytes_new_take (string, strlen (string) + 1); + value = g_variant_new_from_bytes (G_VARIANT_TYPE_STRING, bytes, TRUE); + g_bytes_unref (bytes); + + return value; +} + /** * g_variant_new_object_path: * @object_path: a normal C nul-terminated string diff --git a/glib/gvariant.h b/glib/gvariant.h index 7e9a82343..dbace869e 100644 --- a/glib/gvariant.h +++ b/glib/gvariant.h @@ -103,6 +103,9 @@ GLIB_AVAILABLE_IN_ALL GVariant * g_variant_new_string (const gchar *string); GLIB_AVAILABLE_IN_2_38 GVariant * g_variant_new_take_string (gchar *string); +GLIB_AVAILABLE_IN_2_38 +GVariant * g_variant_new_printf (const gchar *format_string, + ...) G_GNUC_PRINTF (1, 2); GLIB_AVAILABLE_IN_ALL GVariant * g_variant_new_object_path (const gchar *object_path); GLIB_AVAILABLE_IN_ALL diff --git a/glib/tests/gvariant.c b/glib/tests/gvariant.c index 693e9280f..debd69893 100644 --- a/glib/tests/gvariant.c +++ b/glib/tests/gvariant.c @@ -2716,6 +2716,26 @@ test_container (void) g_free (s1); } +static void +test_string (void) +{ + /* Test some different methods of creating strings */ + GVariant *v; + + v = g_variant_new_string ("foo"); + g_assert_cmpstr (g_variant_get_string (v, NULL), ==, "foo"); + g_variant_unref (v); + + + v = g_variant_new_take_string (g_strdup ("foo")); + g_assert_cmpstr (g_variant_get_string (v, NULL), ==, "foo"); + g_variant_unref (v); + + v = g_variant_new_printf ("%s %d", "foo", 123); + g_assert_cmpstr (g_variant_get_string (v, NULL), ==, "foo 123"); + g_variant_unref (v); +} + static void test_utf8 (void) { @@ -4332,6 +4352,7 @@ main (int argc, char **argv) g_free (testname); } + g_test_add_func ("/gvariant/string", test_string); g_test_add_func ("/gvariant/utf8", test_utf8); g_test_add_func ("/gvariant/containers", test_containers); g_test_add_func ("/gvariant/format-strings", test_format_strings);