aboutsummaryrefslogtreecommitdiffstats
path: root/strvec.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2024-05-27 13:47:04 +0200
committerJunio C Hamano <gitster@pobox.com>2024-05-27 11:20:02 -0700
commit11ce77b5cc04e2a42b98f0f9f42d367f50f3b1fc (patch)
treede3d223621a5afca4f2d9a24b9d8b280adee2d7c /strvec.c
parentsubmodule: fix leaking memory for submodule entries (diff)
downloadgit-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.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/strvec.c b/strvec.c
index 178f4f3748..d4073ec9fa 100644
--- a/strvec.c
+++ b/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)