From 2088f0652e03402d7f33520c08bd3b4fcc0228e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Sat, 12 Jun 2021 15:11:37 +0200 Subject: [PATCH] pipewire: properties: add helper to find item directly Add find_item() which retrieves a spa_dict_item directly, and delete find_index() as it is no longer needed. --- src/pipewire/properties.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/pipewire/properties.c b/src/pipewire/properties.c index 4975d755d..a84244858 100644 --- a/src/pipewire/properties.c +++ b/src/pipewire/properties.c @@ -65,13 +65,18 @@ static void clear_item(struct spa_dict_item *item) free((char *) item->value); } -static int find_index(const struct pw_properties *this, const char *key) +static struct spa_dict_item *find_item(const struct pw_properties *this, const char *key) { - const struct spa_dict_item *item; - item = spa_dict_lookup_item(&this->dict, key); + const struct properties *impl = SPA_CONTAINER_OF(this, struct properties, this); + const struct spa_dict_item *item = spa_dict_lookup_item(&this->dict, key); + size_t index; + if (item == NULL) - return -1; - return item - this->dict.items; + return NULL; + + index = item - this->dict.items; + + return pw_array_get_unchecked(&impl->items, index, struct spa_dict_item); } static struct properties *properties_new(int prealloc) @@ -381,22 +386,19 @@ void pw_properties_free(struct pw_properties *properties) static int do_replace(struct pw_properties *properties, const char *key, char *value, bool copy) { struct properties *impl = SPA_CONTAINER_OF(properties, struct properties, this); - int index; + struct spa_dict_item *item; if (key == NULL || key[0] == 0) goto exit_noupdate; - index = find_index(properties, key); + item = find_item(properties, key); - if (index == -1) { + if (item == NULL) { if (value == NULL) return 0; add_func(properties, strdup(key), copy ? strdup(value) : value); SPA_FLAG_CLEAR(properties->dict.flags, SPA_DICT_FLAG_SORTED); } else { - struct spa_dict_item *item = - pw_array_get_unchecked(&impl->items, index, struct spa_dict_item); - if (value && spa_streq(item->value, value)) goto exit_noupdate; @@ -490,13 +492,12 @@ int pw_properties_setf(struct pw_properties *properties, const char *key, const SPA_EXPORT const char *pw_properties_get(const struct pw_properties *properties, const char *key) { - struct properties *impl = SPA_CONTAINER_OF(properties, struct properties, this); - int index = find_index(properties, key); + struct spa_dict_item *item = find_item(properties, key); - if (index == -1) + if (item == NULL) return NULL; - return pw_array_get_unchecked(&impl->items, index, struct spa_dict_item)->value; + return item->value; } /** Iterate property values