mirror of
https://github.com/git/git.git
synced 2025-12-12 20:36:24 +01:00
strvec: add functions to replace and remove strings
Add two functions that allow to replace and remove strings contained in the strvec. This will be used by a subsequent commit that refactors git-mv(1). While at it, add a bunch of unit tests that cover both old and new functionality. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
3ef52dd112
commit
11ce77b5cc
20
strvec.c
20
strvec.c
@@ -56,6 +56,26 @@ void strvec_pushv(struct strvec *array, const char **items)
|
||||
strvec_push(array, *items);
|
||||
}
|
||||
|
||||
const char *strvec_replace(struct strvec *array, size_t idx, const char *replacement)
|
||||
{
|
||||
char *to_free;
|
||||
if (idx >= array->nr)
|
||||
BUG("index outside of array boundary");
|
||||
to_free = (char *) array->v[idx];
|
||||
array->v[idx] = xstrdup(replacement);
|
||||
free(to_free);
|
||||
return array->v[idx];
|
||||
}
|
||||
|
||||
void strvec_remove(struct strvec *array, size_t idx)
|
||||
{
|
||||
if (idx >= array->nr)
|
||||
BUG("index outside of array boundary");
|
||||
free((char *)array->v[idx]);
|
||||
memmove(array->v + idx, array->v + idx + 1, (array->nr - idx) * sizeof(char *));
|
||||
array->nr--;
|
||||
}
|
||||
|
||||
void strvec_pop(struct strvec *array)
|
||||
{
|
||||
if (!array->nr)
|
||||
|
||||
Reference in New Issue
Block a user