From dbb65b54651a88b9691b156bf19b53fbe4a4bd5a Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Sat, 20 Apr 2013 11:44:53 -0400 Subject: [PATCH] GVariant: add new g_variant_new_take_string() API Lots of people have variously asked for APIs like g_variant_new_string_printf() in order to avoid having to use g_strdup_printf(), create a GVariant using g_variant_new_string(), then free the temporary string. Instead of supporting that, plus a million other potential cases, introduce g_variant_new_take_string() as a compromise. It's not possible to write: v = g_variant_new_take_string (g_strdup_printf (....)); to get the desired result and avoid the extra copies. In addition, it works with many other functions. https://bugzilla.gnome.org/show_bug.cgi?id=698455 --- docs/reference/glib/glib-sections.txt | 1 + glib/gvariant.c | 36 +++++++++++++++++++++++++++++++++++ glib/gvariant.h | 2 ++ 3 files changed, 39 insertions(+) diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 4153a29..788af8d 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -3108,6 +3108,7 @@ g_variant_new_uint64 g_variant_new_handle g_variant_new_double g_variant_new_string +g_variant_new_take_string 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 163782d..8c8f66c 100644 --- a/glib/gvariant.c +++ b/glib/gvariant.c @@ -1274,6 +1274,42 @@ g_variant_new_string (const gchar *string) } /** + * g_variant_new_take_string: (skip) + * @string: a normal utf8 nul-terminated string + * + * Creates a string #GVariant with the contents of @string. + * + * @string must be valid utf8. + * + * This function consumes @string. g_free() will be called on @string + * when it is no longer required. + * + * You must not modify or access @string in any other way after passing + * it to this function. It is even possible that @string is immediately + * freed. + * + * Returns: (transfer none): a floating reference to a new string + * #GVariant instance + * + * Since: 2.38 + **/ +GVariant * +g_variant_new_take_string (gchar *string) +{ + GVariant *value; + GBytes *bytes; + + g_return_val_if_fail (string != NULL, NULL); + g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL); + + 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 01b7b5e..7e9a823 100644 --- a/glib/gvariant.h +++ b/glib/gvariant.h @@ -101,6 +101,8 @@ GLIB_AVAILABLE_IN_ALL GVariant * g_variant_new_double (gdouble value); 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_ALL GVariant * g_variant_new_object_path (const gchar *object_path); GLIB_AVAILABLE_IN_ALL -- 2.7.4