From 3b2b47e32a1c143d7d6d2bd9b6d7752a3e71cdd3 Mon Sep 17 00:00:00 2001 From: rcombs Date: Thu, 15 Dec 2022 19:18:45 -0600 Subject: [PATCH] misc/node: add bstr-based variants for map_get() and map_add() --- misc/node.c | 15 +++++++++++++-- misc/node.h | 3 +++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/misc/node.c b/misc/node.c index 3b982ed24a..5bf3211a6e 100644 --- a/misc/node.c +++ b/misc/node.c @@ -45,12 +45,18 @@ struct mpv_node *node_array_add(struct mpv_node *dst, int format) struct mpv_node *node_map_add(struct mpv_node *dst, const char *key, int format) { assert(key); + return node_map_badd(dst, bstr0(key), format); +} + +struct mpv_node *node_map_badd(struct mpv_node *dst, struct bstr key, int format) +{ + assert(key.start); struct mpv_node_list *list = dst->u.list; assert(dst->format == MPV_FORMAT_NODE_MAP && dst->u.list); MP_TARRAY_GROW(list, list->values, list->num); MP_TARRAY_GROW(list, list->keys, list->num); - list->keys[list->num] = talloc_strdup(list, key); + list->keys[list->num] = bstrdup0(list, key); node_init(&list->values[list->num], format, dst); return &list->values[list->num++]; } @@ -83,12 +89,17 @@ void node_map_add_flag(struct mpv_node *dst, const char *key, bool v) } mpv_node *node_map_get(mpv_node *src, const char *key) +{ + return node_map_bget(src, bstr0(key)); +} + +mpv_node *node_map_bget(mpv_node *src, struct bstr key) { if (src->format != MPV_FORMAT_NODE_MAP) return NULL; for (int i = 0; i < src->u.list->num; i++) { - if (strcmp(key, src->u.list->keys[i]) == 0) + if (bstr_equals0(key, src->u.list->keys[i])) return &src->u.list->values[i]; } diff --git a/misc/node.h b/misc/node.h index 419f3fc505..688b0a8ce9 100644 --- a/misc/node.h +++ b/misc/node.h @@ -2,15 +2,18 @@ #define MP_MISC_NODE_H_ #include "libmpv/client.h" +#include "misc/bstr.h" void node_init(struct mpv_node *dst, int format, struct mpv_node *parent); struct mpv_node *node_array_add(struct mpv_node *dst, int format); struct mpv_node *node_map_add(struct mpv_node *dst, const char *key, int format); +struct mpv_node *node_map_badd(struct mpv_node *dst, struct bstr key, int format); void node_map_add_string(struct mpv_node *dst, const char *key, const char *val); void node_map_add_int64(struct mpv_node *dst, const char *key, int64_t v); void node_map_add_double(struct mpv_node *dst, const char *key, double v); void node_map_add_flag(struct mpv_node *dst, const char *key, bool v); mpv_node *node_map_get(mpv_node *src, const char *key); +mpv_node *node_map_bget(mpv_node *src, struct bstr key); bool equal_mpv_value(const void *a, const void *b, mpv_format format); bool equal_mpv_node(const struct mpv_node *a, const struct mpv_node *b);