strmap: split create_entry() out of strmap_put()
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>
Elijah Newren committed
Nov 6, 2020 at 00:24 UTC
6abd22065ccbec054db5af85296dd1167c670eda
1 file changed
+23
-14
strmap.c
+23
-14
@@ -70,27 +70,36 @@ void strmap_partial_clear(struct strmap *map, int free_values)
70
hashmap_partial_clear(&map->map);
71
}
72
73
+static struct strmap_entry *create_entry(struct strmap *map,
74
+ const char *str,
75
+ void *data)
76
+{
77
+ struct strmap_entry *entry;
78
+ const char *key = str;
79
+
80
+ entry = xmalloc(sizeof(*entry));
81
+ hashmap_entry_init(&entry->ent, strhash(str));
82
+
83
+ if (map->strdup_strings)
84
+ key = xstrdup(str);
85
+ entry->key = key;
86
+ entry->value = data;
87
+ return entry;
88
+}
89
+
90
void *strmap_put(struct strmap *map, const char *str, void *data)
91
{
92
struct strmap_entry *entry = find_strmap_entry(map, str);
76
- void *old = NULL;
93
94
if (entry) {
79
- old = entry->value;
95
+ void *old = entry->value;
96
entry->value = data;
81
- } else {
82
- const char *key = str;
83
-
84
- entry = xmalloc(sizeof(*entry));
85
- hashmap_entry_init(&entry->ent, strhash(str));
86
-
87
- if (map->strdup_strings)
88
- key = xstrdup(str);
89
- entry->key = key;
90
- entry->value = data;
91
- hashmap_add(&map->map, &entry->ent);
97
+ return old;
98
}
93
- return old;
99
+
100
+ entry = create_entry(map, str, data);
101
+ hashmap_add(&map->map, &entry->ent);
102
+ return NULL;
103
}
104
105
struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str)