From ef507bc7bdd736d2379a0d0614b3db1341d77187 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Wed, 23 Jan 2019 10:44:02 +0100 Subject: strmap: introduce `git_strmap_get` and use it throughout the tree The current way of looking up an entry from a map is tightly coupled with the map implementation, as one first has to look up the index of the key and then retrieve the associated value by using the index. As a caller, you usually do not care about any indices at all, though, so this is more complicated than really necessary. Furthermore, it invites for errors to happen if the correct error checking sequence is not being followed. Introduce a new high-level function `git_strmap_get` that takes a map and a key and returns a pointer to the associated value if such a key exists. Otherwise, a `NULL` pointer is returned. Adjust all callers that can trivially be converted. --- src/sortedcache.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'src/sortedcache.c') diff --git a/src/sortedcache.c b/src/sortedcache.c index 3967bce15..f58764479 100644 --- a/src/sortedcache.c +++ b/src/sortedcache.c @@ -276,11 +276,8 @@ int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key) size_t keylen, itemlen; char *item_key; - pos = git_strmap_lookup_index(sc->map, key); - if (git_strmap_valid_index(sc->map, pos)) { - item = git_strmap_value_at(sc->map, pos); + if ((item = git_strmap_get(sc->map, key)) != NULL) goto done; - } keylen = strlen(key); itemlen = sc->item_path_offset + keylen + 1; @@ -320,10 +317,7 @@ done: /* lookup item by key */ void *git_sortedcache_lookup(const git_sortedcache *sc, const char *key) { - size_t pos = git_strmap_lookup_index(sc->map, key); - if (git_strmap_valid_index(sc->map, pos)) - return git_strmap_value_at(sc->map, pos); - return NULL; + return git_strmap_get(sc->map, key); } /* find out how many items are in the cache */ -- cgit v1.2.1