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.
This commit is contained in:
Barnabás Pőcze 2021-06-12 15:11:37 +02:00
parent ab4de7733f
commit 2088f0652e

View file

@ -65,13 +65,18 @@ static void clear_item(struct spa_dict_item *item)
free((char *) item->value); 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; const struct properties *impl = SPA_CONTAINER_OF(this, struct properties, this);
item = spa_dict_lookup_item(&this->dict, key); const struct spa_dict_item *item = spa_dict_lookup_item(&this->dict, key);
size_t index;
if (item == NULL) if (item == NULL)
return -1; return NULL;
return item - this->dict.items;
index = item - this->dict.items;
return pw_array_get_unchecked(&impl->items, index, struct spa_dict_item);
} }
static struct properties *properties_new(int prealloc) 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) 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); struct properties *impl = SPA_CONTAINER_OF(properties, struct properties, this);
int index; struct spa_dict_item *item;
if (key == NULL || key[0] == 0) if (key == NULL || key[0] == 0)
goto exit_noupdate; goto exit_noupdate;
index = find_index(properties, key); item = find_item(properties, key);
if (index == -1) { if (item == NULL) {
if (value == NULL) if (value == NULL)
return 0; return 0;
add_func(properties, strdup(key), copy ? strdup(value) : value); add_func(properties, strdup(key), copy ? strdup(value) : value);
SPA_FLAG_CLEAR(properties->dict.flags, SPA_DICT_FLAG_SORTED); SPA_FLAG_CLEAR(properties->dict.flags, SPA_DICT_FLAG_SORTED);
} else { } else {
struct spa_dict_item *item =
pw_array_get_unchecked(&impl->items, index, struct spa_dict_item);
if (value && spa_streq(item->value, value)) if (value && spa_streq(item->value, value))
goto exit_noupdate; goto exit_noupdate;
@ -490,13 +492,12 @@ int pw_properties_setf(struct pw_properties *properties, const char *key, const
SPA_EXPORT SPA_EXPORT
const char *pw_properties_get(const struct pw_properties *properties, const char *key) const char *pw_properties_get(const struct pw_properties *properties, const char *key)
{ {
struct properties *impl = SPA_CONTAINER_OF(properties, struct properties, this); struct spa_dict_item *item = find_item(properties, key);
int index = find_index(properties, key);
if (index == -1) if (item == NULL)
return NULL; return NULL;
return pw_array_get_unchecked(&impl->items, index, struct spa_dict_item)->value; return item->value;
} }
/** Iterate property values /** Iterate property values