summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-07-19 10:33:54 +0200
committerThomas Haller <thaller@redhat.com>2019-07-25 10:42:06 +0200
commita78ba1c33a40358ca52c91fc0163b7b6f898da41 (patch)
treeb41fa7845c7251d38ead0f96dc0aa933c51bfdd6 /shared
parent31d74e8b457380e6a9f46fca47f8fef8a56f7cb0 (diff)
downloadNetworkManager-a78ba1c33a40358ca52c91fc0163b7b6f898da41.tar.gz
shared: add nm_strcmp_with_data()
It is like strcmp(), but has a signature suitable for GCompareDataFunc. This is necessary with nm_utils_ptrarray_find_binary_search() to search a sorted strv array. The fault is here really C, which doesn't allow inline static functions. So, you need all kinds of slightly different flavors for the same callbacks (with or without user-data). Note that glib2 internally just casts strcmp() to GCompareDataFunc ([1]), relying on the fact how arguments are passed to the function and ignoring the additional user-data argument. But I find that really ugly and probably not permissible in general C. Dunno whether POSIX would guarantee for this to work. I'd rather not do such function pointer casts. [1] https://gitlab.gnome.org/GNOME/glib/blob/0c0cf59858abb3f8464bc55f596f9fbf599ac251/glib/garray.c#L1792
Diffstat (limited to 'shared')
-rw-r--r--shared/nm-glib-aux/nm-shared-utils.c9
-rw-r--r--shared/nm-glib-aux/nm-shared-utils.h1
2 files changed, 10 insertions, 0 deletions
diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c
index 7362dfc930..74f8a9b3f5 100644
--- a/shared/nm-glib-aux/nm-shared-utils.c
+++ b/shared/nm-glib-aux/nm-shared-utils.c
@@ -806,6 +806,15 @@ _nm_utils_ascii_str_to_uint64 (const char *str, guint base, guint64 min, guint64
/*****************************************************************************/
+int
+nm_strcmp_with_data (gconstpointer a, gconstpointer b, gpointer user_data)
+{
+ const char *s1 = a;
+ const char *s2 = b;
+
+ return strcmp (s1, s2);
+}
+
/* like nm_strcmp_p(), suitable for g_ptr_array_sort_with_data().
* g_ptr_array_sort() just casts nm_strcmp_p() to a function of different
* signature. I guess, in glib there are knowledgeable people that ensure
diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h
index 3868a4938d..d1090af397 100644
--- a/shared/nm-glib-aux/nm-shared-utils.h
+++ b/shared/nm-glib-aux/nm-shared-utils.h
@@ -918,6 +918,7 @@ nm_utf8_collate0 (const char *a, const char *b)
return g_utf8_collate (a, b);
}
+int nm_strcmp_with_data (gconstpointer a, gconstpointer b, gpointer user_data);
int nm_strcmp_p_with_data (gconstpointer a, gconstpointer b, gpointer user_data);
int nm_cmp_uint32_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_data);
int nm_cmp_int2ptr_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_data);