Skip to content

Commit 6abd220

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4fa1d50 commit 6abd220

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

strmap.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,36 @@ void strmap_partial_clear(struct strmap *map, int free_values)
7070
hashmap_partial_clear(&map->map);
7171
}
7272

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+
7390
void *strmap_put(struct strmap *map, const char *str, void *data)
7491
{
7592
struct strmap_entry *entry = find_strmap_entry(map, str);
76-
void *old = NULL;
7793

7894
if (entry) {
79-
old = entry->value;
95+
void *old = entry->value;
8096
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;
9298
}
93-
return old;
99+
100+
entry = create_entry(map, str, data);
101+
hashmap_add(&map->map, &entry->ent);
102+
return NULL;
94103
}
95104

96105
struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str)

0 commit comments

Comments
 (0)