g_variant_get_data_as_bytes: return a sub-bytes if necessary

https://bugzilla.gnome.org/show_bug.cgi?id=698457
This commit is contained in:
Lars Uebernickel 2013-04-20 11:44:21 -04:00 committed by Ryan Lortie
parent dbb65b5465
commit ac1379e22c
2 changed files with 25 additions and 1 deletions

View File

@ -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);
}

View File

@ -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