summaryrefslogtreecommitdiff
path: root/src/sortedcache.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-01-23 10:44:02 +0100
committerPatrick Steinhardt <ps@pks.im>2019-02-15 13:16:48 +0100
commitef507bc7bdd736d2379a0d0614b3db1341d77187 (patch)
treea441206d2eef9ddfa3784b361910a9f03d1a5bf6 /src/sortedcache.c
parent7e926ef306166993131628b41a13f2b26aaf43de (diff)
downloadlibgit2-ef507bc7bdd736d2379a0d0614b3db1341d77187.tar.gz
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.
Diffstat (limited to 'src/sortedcache.c')
-rw-r--r--src/sortedcache.c10
1 files changed, 2 insertions, 8 deletions
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 */