[glib] Add g_variant_new_printf



commit 1c586e44befe07812ac1e4473ef9e3a76330b78e
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Jul 11 12:16:44 2013 -0400

    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(+), 0 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index b7dc06b..54c7b6d 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 62cb4f8..2197cb0 100644
--- a/glib/gvariant.c
+++ b/glib/gvariant.c
@@ -1310,6 +1310,44 @@ g_variant_new_take_string (gchar *string)
 }
 
 /**
+ * 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 7e9a823..dbace86 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 693e928..debd698 100644
--- a/glib/tests/gvariant.c
+++ b/glib/tests/gvariant.c
@@ -2717,6 +2717,26 @@ test_container (void)
 }
 
 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)
 {
   const gchar invalid[] = "hello\xffworld";
@@ -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);


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