]> git.ipfire.org Git - thirdparty/git.git/commitdiff
strmap: split create_entry() out of strmap_put()
authorElijah Newren <newren@gmail.com>
Fri, 6 Nov 2020 00:24:53 +0000 (00:24 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 6 Nov 2020 17:33:35 +0000 (09:33 -0800)
This will facilitate adding entries to a strmap subtype in ways that
differ slightly from that of strmap_put().

Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
strmap.c

index 0d10a884b53a21307982f6384d269d7249c6ef69..dc84c57c077fa50177991f9520f1a464592ee0d9 100644 (file)
--- a/strmap.c
+++ b/strmap.c
@@ -70,27 +70,36 @@ void strmap_partial_clear(struct strmap *map, int free_values)
        hashmap_partial_clear(&map->map);
 }
 
+static struct strmap_entry *create_entry(struct strmap *map,
+                                        const char *str,
+                                        void *data)
+{
+       struct strmap_entry *entry;
+       const char *key = str;
+
+       entry = xmalloc(sizeof(*entry));
+       hashmap_entry_init(&entry->ent, strhash(str));
+
+       if (map->strdup_strings)
+               key = xstrdup(str);
+       entry->key = key;
+       entry->value = data;
+       return entry;
+}
+
 void *strmap_put(struct strmap *map, const char *str, void *data)
 {
        struct strmap_entry *entry = find_strmap_entry(map, str);
-       void *old = NULL;
 
        if (entry) {
-               old = entry->value;
+               void *old = entry->value;
                entry->value = data;
-       } else {
-               const char *key = str;
-
-               entry = xmalloc(sizeof(*entry));
-               hashmap_entry_init(&entry->ent, strhash(str));
-
-               if (map->strdup_strings)
-                       key = xstrdup(str);
-               entry->key = key;
-               entry->value = data;
-               hashmap_add(&map->map, &entry->ent);
+               return old;
        }
-       return old;
+
+       entry = create_entry(map, str, data);
+       hashmap_add(&map->map, &entry->ent);
+       return NULL;
 }
 
 struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str)