gio: Use g_ptr_array_sort_values()

Cleanup some code using GPtrArray sorting with functions that were taking
pointer to pointers arguments.
This commit is contained in:
Marco Trevisan (Treviño) 2022-12-20 00:22:05 +01:00 committed by Philip Withnall
parent 0ffd23cac4
commit 861e82efbc
6 changed files with 23 additions and 44 deletions

View File

@ -317,12 +317,6 @@ print_paths (GDBusConnection *c,
;
}
static gint
ptr_strcmp0 (const gchar **a, const gchar **b)
{
return g_strcmp0 (*a, *b);
}
static void
print_names (GDBusConnection *c,
gboolean include_unique_names)
@ -385,7 +379,7 @@ print_names (GDBusConnection *c,
g_variant_unref (result);
keys = g_hash_table_steal_all_keys (name_set);
g_ptr_array_sort (keys, (GCompareFunc) ptr_strcmp0);
g_ptr_array_sort_values (keys, (GCompareFunc) g_strcmp0);
for (guint i = 0; i < keys->len; ++i)
{
const gchar *name = g_ptr_array_index (keys, i);

View File

@ -631,13 +631,6 @@ g_dbus_proxy_init (GDBusProxy *proxy)
/* ---------------------------------------------------------------------------------------------------- */
static gint
property_name_sort_func (const gchar **a,
const gchar **b)
{
return g_strcmp0 (*a, *b);
}
/**
* g_dbus_proxy_get_cached_property_names:
* @proxy: A #GDBusProxy.
@ -672,7 +665,7 @@ g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
g_hash_table_iter_init (&iter, proxy->priv->properties);
while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
g_ptr_array_add (p, g_strdup (key));
g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
g_ptr_array_sort_values (p, (GCompareFunc) g_strcmp0);
g_ptr_array_add (p, NULL);
names = (gchar **) g_ptr_array_free (p, FALSE);

View File

@ -1869,8 +1869,8 @@ static gint
compare_strings (gconstpointer a,
gconstpointer b)
{
gchar *one = *(gchar **) a;
gchar *two = *(gchar **) b;
const gchar *one = a;
const gchar *two = a;
gint cmp;
cmp = g_str_has_suffix (two, ".enums.xml") -
@ -2278,10 +2278,10 @@ main (int argc, char **argv)
retval = 0;
goto done;
}
g_ptr_array_sort (files, compare_strings);
g_ptr_array_sort_values (files, compare_strings);
g_ptr_array_add (files, NULL);
g_ptr_array_sort (overrides, compare_strings);
g_ptr_array_sort_values (overrides, compare_strings);
g_ptr_array_add (overrides, NULL);
schema_files = (char **) g_ptr_array_free (files, FALSE);

View File

@ -347,8 +347,8 @@ static gint
compare_strings (gconstpointer a,
gconstpointer b)
{
const gchar *sa = *(const gchar **) a;
const gchar *sb = *(const gchar **) b;
const gchar *sa = a;
const gchar *sb = b;
/* Array terminator must sort last */
if (sa == NULL)
@ -413,7 +413,7 @@ get_nodes_at (GDBusConnection *c,
g_dbus_node_info_unref (node_info);
/* Nodes are semantically unordered; sort array so tests can rely on order */
g_ptr_array_sort (p, compare_strings);
g_ptr_array_sort_values (p, compare_strings);
return (gchar **) g_ptr_array_free (p, FALSE);
}

View File

@ -1551,12 +1551,6 @@ typedef struct
guint num_interface_removed_signals;
} OMData;
static gint
my_pstrcmp (const gchar **a, const gchar **b)
{
return g_strcmp0 (*a, *b);
}
static void
om_check_interfaces_added (const gchar *signal_name,
GVariant *parameters,
@ -1597,8 +1591,10 @@ om_check_interfaces_added (const gchar *signal_name,
g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
}
g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
g_ptr_array_sort_values (interfaces, (GCompareFunc) g_strcmp0);
g_ptr_array_sort_values (interfaces_in_message, (GCompareFunc) g_strcmp0);
G_GNUC_END_IGNORE_DEPRECATIONS
for (n = 0; n < interfaces->len; n++)
g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
g_ptr_array_unref (interfaces_in_message);
@ -1646,8 +1642,10 @@ om_check_interfaces_removed (const gchar *signal_name,
g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
}
g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
g_ptr_array_sort_values (interfaces, (GCompareFunc) g_strcmp0);
g_ptr_array_sort_values (interfaces_in_message, (GCompareFunc) g_strcmp0);
G_GNUC_END_IGNORE_DEPRECATIONS
for (n = 0; n < interfaces->len; n++)
g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
g_ptr_array_unref (interfaces_in_message);

View File

@ -572,13 +572,7 @@ test_store_splice_wrong_type (void)
}
static gint
ptr_array_cmp_action_by_name (GAction **a, GAction **b)
{
return g_strcmp0 (g_action_get_name (*a), g_action_get_name (*b));
}
static gint
list_model_cmp_action_by_name (GAction *a, GAction *b, gpointer user_data)
cmp_action_by_name (GAction *a, GAction *b, gpointer user_data)
{
return g_strcmp0 (g_action_get_name (a), g_action_get_name (b));
}
@ -606,16 +600,16 @@ test_store_sort (void)
g_ptr_array_add (array, g_simple_action_new ("1", NULL));
/* Sort an empty list */
g_list_store_sort (store, (GCompareDataFunc)list_model_cmp_action_by_name, NULL);
g_list_store_sort (store, (GCompareDataFunc) cmp_action_by_name, NULL);
/* Add all */
g_list_store_splice (store, 0, 0, array->pdata, array->len);
g_assert_true (model_array_equal (model, array));
/* Sort both and check if the result is the same */
g_ptr_array_sort (array, (GCompareFunc)ptr_array_cmp_action_by_name);
g_ptr_array_sort_values (array, (GCompareFunc)cmp_action_by_name);
g_assert_false (model_array_equal (model, array));
g_list_store_sort (store, (GCompareDataFunc)list_model_cmp_action_by_name, NULL);
g_list_store_sort (store, (GCompareDataFunc) cmp_action_by_name, NULL);
g_assert_true (model_array_equal (model, array));
g_ptr_array_unref (array);
@ -763,7 +757,7 @@ test_store_signal_items_changed (void)
/* Sort the list */
expect_items_changed (&expected, 0, 2, 2);
g_list_store_sort (store,
(GCompareDataFunc)list_model_cmp_action_by_name,
(GCompareDataFunc) cmp_action_by_name,
NULL);
g_assert_true (expected.called);
g_assert_false (expected.notified);
@ -773,7 +767,7 @@ test_store_signal_items_changed (void)
item = g_simple_action_new ("3", NULL);
g_list_store_insert_sorted (store,
item,
(GCompareDataFunc)list_model_cmp_action_by_name,
(GCompareDataFunc) cmp_action_by_name,
NULL);
g_object_unref (item);
g_assert_true (expected.called);