trivial: Add fu_common_strjoin_array() to join arrays of strings

This commit is contained in:
Richard Hughes 2021-01-26 09:59:15 +00:00
parent fcc9dcc0e5
commit e52e1b49ad
5 changed files with 41 additions and 14 deletions

View File

@ -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

View File

@ -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,

View File

@ -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;

View File

@ -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,

View File

@ -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 */