gvalue: add g_value_steal_string()

This call is needed to avoid an extra copy after
serialization of the data.
This commit is contained in:
Alexander Slobodeniuk
2023-11-03 17:00:11 +01:00
committed by Philip Withnall
parent 7e7a6271fe
commit 7880d87091
4 changed files with 60 additions and 1 deletions

View File

@@ -420,7 +420,7 @@ test_value_string (void)
const gchar *static2 = "static2";
const gchar *storedstr;
const gchar *copystr;
gchar *str1, *str2;
gchar *str1, *str2, *stolen_str;
GValue value = G_VALUE_INIT;
GValue copy = G_VALUE_INIT;
@@ -513,7 +513,12 @@ test_value_string (void)
g_assert_true (storedstr != static2);
g_assert_cmpstr (storedstr, ==, static2);
/* Now check stealing the ownership of the contents */
stolen_str = g_value_steal_string (&value);
g_assert_null (g_value_get_string (&value));
g_value_unset (&value);
g_assert_cmpstr (stolen_str, ==, static2);
g_free (stolen_str);
/*
* Static strings
@@ -544,6 +549,14 @@ test_value_string (void)
g_assert_true (storedstr != static1);
g_assert_cmpstr (storedstr, ==, static2);
/* Check if g_value_steal_string() can handle GValue
* with a static string */
stolen_str = g_value_steal_string (&value);
g_assert_true (stolen_str != static2);
g_assert_cmpstr (stolen_str, ==, static2);
g_assert_null (g_value_get_string (&value));
g_free (stolen_str);
g_value_unset (&value);
/*
@@ -588,6 +601,14 @@ test_value_string (void)
g_assert_true (storedstr != static2);
g_assert_cmpstr (storedstr, ==, static2);
/* Check if g_value_steal_string() can handle GValue
* with an interned string */
stolen_str = g_value_steal_string (&value);
g_assert_true (stolen_str != static2);
g_assert_cmpstr (stolen_str, ==, static2);
g_assert_null (g_value_get_string (&value));
g_free (stolen_str);
g_value_unset (&value);
}