From e52e1b49ade25bc0f4412f25dfa27f0bf1275160 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Tue, 26 Jan 2021 09:59:15 +0000 Subject: [PATCH] trivial: Add fu_common_strjoin_array() to join arrays of strings --- libfwupdplugin/fu-common.c | 30 ++++++++++++++++++++++++++++++ libfwupdplugin/fu-common.h | 2 ++ libfwupdplugin/fwupdplugin.map | 1 + src/fu-engine.c | 10 +--------- src/fu-util-common.c | 12 +++++++----- 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/libfwupdplugin/fu-common.c b/libfwupdplugin/fu-common.c index dfea1a811..598bc7324 100644 --- a/libfwupdplugin/fu-common.c +++ b/libfwupdplugin/fu-common.c @@ -1913,6 +1913,36 @@ fu_common_strsafe (const gchar *str, gsize maxsz) return g_string_free (g_steal_pointer (&tmp), FALSE); } + +/** + * fu_common_strjoin_array: + * @separator: (nullable): string to insert between each of the strings, or %NULL + * @array: (element-type utf8): A #GPtrArray + * + * Joins an array of strings together to form one long string, with the optional + * separator inserted between each of them. + * + * If @array has no items, the return value will be an empty string. + * If @array contains a single item, separator will not appear in the resulting + * string. + * + * Returns: a string + * + * Since: 1.5.6 + **/ +gchar * +fu_common_strjoin_array (const gchar *separator, GPtrArray *array) +{ + g_autofree const gchar **strv = NULL; + + g_return_val_if_fail (array != NULL, NULL); + + strv = g_new0 (const gchar *, array->len + 1); + for (guint i = 0; i < array->len; i++) + strv[i] = g_ptr_array_index (array, i); + return g_strjoinv (separator, (gchar **) strv); +} + /** * fu_memcpy_safe: * @dst: destination buffer diff --git a/libfwupdplugin/fu-common.h b/libfwupdplugin/fu-common.h index 66c616dca..291d778e4 100644 --- a/libfwupdplugin/fu-common.h +++ b/libfwupdplugin/fu-common.h @@ -268,6 +268,8 @@ gchar **fu_common_strnsplit (const gchar *str, gint max_tokens); gchar *fu_common_strsafe (const gchar *str, gsize maxsz); +gchar *fu_common_strjoin_array (const gchar *separator, + GPtrArray *array); gboolean fu_common_kernel_locked_down (void); gboolean fu_common_cpuid (guint32 leaf, guint32 *eax, diff --git a/libfwupdplugin/fwupdplugin.map b/libfwupdplugin/fwupdplugin.map index 756481c6b..1961ef655 100644 --- a/libfwupdplugin/fwupdplugin.map +++ b/libfwupdplugin/fwupdplugin.map @@ -725,6 +725,7 @@ LIBFWUPDPLUGIN_1.5.5 { LIBFWUPDPLUGIN_1.5.6 { global: fu_common_get_memory_size; + fu_common_strjoin_array; fu_hwids_add_smbios_override; fu_hwids_get_keys; fu_plugin_get_devices; diff --git a/src/fu-engine.c b/src/fu-engine.c index eca3aba8c..74dd4459e 100644 --- a/src/fu-engine.c +++ b/src/fu-engine.c @@ -1160,7 +1160,6 @@ fu_engine_check_requirement_vendor_id (FuEngine *self, XbNode *req, GPtrArray *vendor_ids; const gchar *vendor_ids_metadata; g_autofree gchar *vendor_ids_device = NULL; - g_auto(GStrv) vendor_ids_tmp = NULL; /* devices without vendor IDs should not exist! */ vendor_ids = fu_device_get_vendor_ids (device); @@ -1183,15 +1182,8 @@ fu_engine_check_requirement_vendor_id (FuEngine *self, XbNode *req, return FALSE; } - /* just cat them together into a string */ - vendor_ids_tmp = g_new0 (gchar *, vendor_ids->len + 1); - for (guint i = 0; i < vendor_ids->len; i++) { - const gchar *vendor_id_tmp = g_ptr_array_index (vendor_ids, i); - vendor_ids_tmp[i] = g_strdup (vendor_id_tmp); - } - vendor_ids_device = g_strjoinv ("|", vendor_ids_tmp); - /* it is always safe to use a regex, even for simple strings */ + vendor_ids_device = fu_common_strjoin_array ("|", vendor_ids); if (!g_regex_match_simple (vendor_ids_metadata, vendor_ids_device, 0, 0)) { g_set_error (error, FWUPD_ERROR, diff --git a/src/fu-util-common.c b/src/fu-util-common.c index 7d9463afb..62b02ad00 100644 --- a/src/fu-util-common.c +++ b/src/fu-util-common.c @@ -1134,6 +1134,7 @@ fu_util_device_to_string (FwupdDevice *dev, guint idt) { FwupdUpdateState state; GPtrArray *guids = fwupd_device_get_guids (dev); + GPtrArray *vendor_ids = fwupd_device_get_vendor_ids (dev); GPtrArray *instance_ids = fwupd_device_get_instance_ids (dev); GString *str = g_string_new (NULL); const gchar *tmp; @@ -1203,17 +1204,18 @@ fu_util_device_to_string (FwupdDevice *dev, guint idt) /* vendor */ tmp = fwupd_device_get_vendor (dev); - tmp2 = fwupd_device_get_vendor_id (dev); - if (tmp != NULL && tmp2 != NULL) { - g_autofree gchar *both = g_strdup_printf ("%s (%s)", tmp, tmp2); + if (tmp != NULL && vendor_ids->len > 0) { + g_autofree gchar *strv = fu_common_strjoin_array ("|", vendor_ids); + g_autofree gchar *both = g_strdup_printf ("%s (%s)", tmp, strv); /* TRANSLATORS: manufacturer of hardware */ fu_common_string_append_kv (str, idt + 1, _("Vendor"), both); } else if (tmp != NULL) { /* TRANSLATORS: manufacturer of hardware */ fu_common_string_append_kv (str, idt + 1, _("Vendor"), tmp); - } else if (tmp2 != NULL) { + } else if (vendor_ids->len > 0) { + g_autofree gchar *strv = fu_common_strjoin_array ("|", vendor_ids); /* TRANSLATORS: manufacturer of hardware */ - fu_common_string_append_kv (str, idt + 1, _("Vendor"), tmp2); + fu_common_string_append_kv (str, idt + 1, _("Vendor"), strv); } /* branch */