From 89a6782c55117a311d1d7f9d0db749dac8a2c8d4 Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Mon, 1 Apr 2019 20:36:18 +0000 Subject: [PATCH] utils: Add flatpak_strv_merge Closes: #2775 Approved by: alexlarsson --- common/flatpak-utils-private.h | 2 + common/flatpak-utils.c | 68 +++++++++++++++++++--------------- tests/testcommon.c | 4 +- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/common/flatpak-utils-private.h b/common/flatpak-utils-private.h index 5960aed8..37406041 100644 --- a/common/flatpak-utils-private.h +++ b/common/flatpak-utils-private.h @@ -120,6 +120,8 @@ const char * flatpak_get_bwrap (void); char *flatpak_get_timezone (void); +char **flatpak_strv_merge (char **strv1, + char **strv2); char **flatpak_subpaths_merge (char **subpaths1, char **subpaths2); diff --git a/common/flatpak-utils.c b/common/flatpak-utils.c index aa25820c..0ca05d92 100644 --- a/common/flatpak-utils.c +++ b/common/flatpak-utils.c @@ -5690,46 +5690,56 @@ flatpak_format_choices (const char **choices, g_print ("\n"); } +char ** +flatpak_strv_merge (char **strv1, + char **strv2) +{ + GPtrArray *array; + int i; + + /* Maybe either (or both) is unspecified */ + if (strv1 == NULL) + return g_strdupv (strv2); + if (strv2 == NULL) + return g_strdupv (strv1); + + /* Combine both */ + array = g_ptr_array_new (); + + for (i = 0; strv1[i] != NULL; i++) + { + if (!flatpak_g_ptr_array_contains_string (array, strv1[i])) + g_ptr_array_add (array, g_strdup (strv1[i])); + } + + for (i = 0; strv2[i] != NULL; i++) + { + if (!flatpak_g_ptr_array_contains_string (array, strv2[i])) + g_ptr_array_add (array, g_strdup (strv2[i])); + } + + g_ptr_array_add (array, NULL); + return (char **) g_ptr_array_free (array, FALSE); +} + /* In this NULL means don't care about these paths, while an empty array means match anything */ char ** flatpak_subpaths_merge (char **subpaths1, char **subpaths2) { - GPtrArray *array; - int i; + char **res; - /* Maybe either (or both) is unspecified */ - if (subpaths1 == NULL) - return g_strdupv (subpaths2); - if (subpaths2 == NULL) + if (subpaths1 != NULL && subpaths1[0] == NULL) return g_strdupv (subpaths1); - - /* Check for any "everything" match */ - if (subpaths1[0] == NULL) - return g_strdupv (subpaths1); - if (subpaths2[0] == NULL) + if (subpaths2 != NULL && subpaths2[0] == NULL) return g_strdupv (subpaths2); - /* Combine both */ - array = g_ptr_array_new (); + res = flatpak_strv_merge (subpaths1, subpaths2); + if (res) + qsort (res, g_strv_length (res), sizeof (const char *), flatpak_strcmp0_ptr); - for (i = 0; subpaths1[i] != NULL; i++) - { - if (!flatpak_g_ptr_array_contains_string (array, subpaths1[i])) - g_ptr_array_add (array, g_strdup (subpaths1[i])); - } - - for (i = 0; subpaths2[i] != NULL; i++) - { - if (!flatpak_g_ptr_array_contains_string (array, subpaths2[i])) - g_ptr_array_add (array, g_strdup (subpaths2[i])); - } - - g_ptr_array_sort (array, flatpak_strcmp0_ptr); - g_ptr_array_add (array, NULL); - - return (char **) g_ptr_array_free (array, FALSE); + return res; } char * diff --git a/tests/testcommon.c b/tests/testcommon.c index a36fe652..754d9765 100644 --- a/tests/testcommon.c +++ b/tests/testcommon.c @@ -395,11 +395,11 @@ test_subpaths_merge (void) g_auto(GStrv) res = NULL; res = flatpak_subpaths_merge (NULL, bla); - assert_strv_equal (res, bla); + assert_strv_equal (res, bla_sorted); g_clear_pointer (&res, g_strfreev); res = flatpak_subpaths_merge (bla, NULL); - assert_strv_equal (res, bla); + assert_strv_equal (res, bla_sorted); g_clear_pointer (&res, g_strfreev); res = flatpak_subpaths_merge (empty, bla);