[glib] g_variant_get_data_as_bytes: return a sub-bytes if necessary
- From: Ryan Lortie <ryanl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] g_variant_get_data_as_bytes: return a sub-bytes if necessary
- Date: Sat, 20 Apr 2013 23:25:02 +0000 (UTC)
commit ac1379e22cc6d97f1336eee0f846900950e978b5
Author: Lars Uebernickel <lars uebernickel canonical com>
Date: Sat Apr 20 11:44:21 2013 -0400
g_variant_get_data_as_bytes: return a sub-bytes if necessary
https://bugzilla.gnome.org/show_bug.cgi?id=698457
glib/gvariant-core.c | 15 ++++++++++++++-
glib/tests/gvariant.c | 11 +++++++++++
2 files changed, 25 insertions(+), 1 deletion(-)
---
diff --git a/glib/gvariant-core.c b/glib/gvariant-core.c
index f823e01..dc20c32 100644
--- a/glib/gvariant-core.c
+++ b/glib/gvariant-core.c
@@ -882,11 +882,24 @@ g_variant_get_data (GVariant *value)
GBytes *
g_variant_get_data_as_bytes (GVariant *value)
{
+ const gchar *bytes_data;
+ const gchar *data;
+ gsize bytes_size;
+ gsize size;
+
g_variant_lock (value);
g_variant_ensure_serialised (value);
g_variant_unlock (value);
- return g_bytes_ref (value->contents.serialised.bytes);
+ bytes_data = g_bytes_get_data (value->contents.serialised.bytes, &bytes_size);
+ data = value->contents.serialised.data;
+ size = value->size;
+
+ if (data == bytes_data && size == bytes_size)
+ return g_bytes_ref (value->contents.serialised.bytes);
+ else
+ return g_bytes_new_from_bytes (value->contents.serialised.bytes,
+ data - bytes_data, size);
}
diff --git a/glib/tests/gvariant.c b/glib/tests/gvariant.c
index d2d09d9..4e531c8 100644
--- a/glib/tests/gvariant.c
+++ b/glib/tests/gvariant.c
@@ -4258,6 +4258,7 @@ static void
test_gbytes (void)
{
GVariant *a;
+ GVariant *tuple;
GBytes *bytes;
GBytes *bytes2;
const guint8 values[5] = { 1, 2, 3, 4, 5 };
@@ -4279,9 +4280,19 @@ test_gbytes (void)
bytes = g_bytes_new (&values, 5);
g_assert (g_bytes_equal (bytes, bytes2));
+ g_bytes_unref (bytes);
+ g_bytes_unref (bytes2);
+
+ tuple = g_variant_new_parsed ("['foo', 'bar']");
+ bytes = g_variant_get_data_as_bytes (tuple); /* force serialisation */
+ a = g_variant_get_child_value (tuple, 1);
+ bytes2 = g_variant_get_data_as_bytes (a);
+ g_assert (!g_bytes_equal (bytes, bytes2));
g_bytes_unref (bytes);
g_bytes_unref (bytes2);
+ g_variant_unref (a);
+ g_variant_unref (tuple);
}
int
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]