diff options
| author | Patrick Steinhardt <ps@pks.im> | 2024-05-27 13:47:04 +0200 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2024-05-27 11:20:02 -0700 |
| commit | 11ce77b5cc04e2a42b98f0f9f42d367f50f3b1fc (patch) | |
| tree | de3d223621a5afca4f2d9a24b9d8b280adee2d7c /strvec.c | |
| parent | submodule: fix leaking memory for submodule entries (diff) | |
| download | git-11ce77b5cc04e2a42b98f0f9f42d367f50f3b1fc.tar.gz git-11ce77b5cc04e2a42b98f0f9f42d367f50f3b1fc.zip | |
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>
Diffstat (limited to 'strvec.c')
| -rw-r--r-- | strvec.c | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -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) |
