From fff812e25523997bfa6997a15eecba4aeda52e2e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 7 Jun 2020 01:04:05 +0200 Subject: [PATCH] shared: refactor nm_utils_strdict_to_variant_ass() to use nm_utils_named_values_from_strdict() It is pretty much the same code this way, but shorter. --- shared/nm-glib-aux/nm-shared-utils.c | 55 ++++++++-------------------- 1 file changed, 15 insertions(+), 40 deletions(-) diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c index 918964e785..68e6ee5ba1 100644 --- a/shared/nm-glib-aux/nm-shared-utils.c +++ b/shared/nm-glib-aux/nm-shared-utils.c @@ -478,50 +478,25 @@ nm_utils_gbytes_to_variant_ay (GBytes *bytes) GVariant * nm_utils_strdict_to_variant_ass (GHashTable *strdict) { - GHashTableIter iter; - const char *key, *value; + gs_free NMUtilsNamedValue *values_free = NULL; + NMUtilsNamedValue values_prepared[20]; + const NMUtilsNamedValue *values; GVariantBuilder builder; - guint i, len; + guint i; + guint n; + + values = nm_utils_named_values_from_strdict (strdict, + &n, + values_prepared, + &values_free); g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}")); - - if (!strdict) - goto out; - len = g_hash_table_size (strdict); - if (!len) - goto out; - - g_hash_table_iter_init (&iter, strdict); - if (!g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) - nm_assert_not_reached (); - - if (len == 1) - g_variant_builder_add (&builder, "{ss}", key, value); - else { - gs_free NMUtilsNamedValue *idx_free = NULL; - NMUtilsNamedValue *idx; - - if (len > 300 / sizeof (NMUtilsNamedValue)) { - idx_free = g_new (NMUtilsNamedValue, len); - idx = idx_free; - } else - idx = g_alloca (sizeof (NMUtilsNamedValue) * len); - - i = 0; - do { - idx[i].name = key; - idx[i].value_str = value; - i++; - } while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)); - nm_assert (i == len); - - nm_utils_named_value_list_sort (idx, len, NULL, NULL); - - for (i = 0; i < len; i++) - g_variant_builder_add (&builder, "{ss}", idx[i].name, idx[i].value_str); + for (i = 0; i < n; i++) { + g_variant_builder_add (&builder, + "{ss}", + values[i].name, + values[i].value_str); } - -out: return g_variant_builder_end (&builder); }