summaryrefslogtreecommitdiff
path: root/strvec.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2024-11-20 14:39:38 +0100
committerJunio C Hamano <gitster@pobox.com>2024-11-21 08:23:42 +0900
commit3f5fadef3718f0ada963aeb25be70c8ba71b5c7c (patch)
tree07f9f131ae73d94429cd0ddf6024ce0a39f1e645 /strvec.c
parent141766d1bb801ae2a9c7358920ce8dc9697f53c4 (diff)
strvec: introduce new `strvec_splice()` function
Introduce a new `strvec_splice()` function that can replace a range of strings in the vector with another array of strings. This function will be used in subsequent commits. 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.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/strvec.c b/strvec.c
index f712070f57..d1cf4e2496 100644
--- a/strvec.c
+++ b/strvec.c
@@ -56,6 +56,25 @@ void strvec_pushv(struct strvec *array, const char **items)
strvec_push(array, *items);
}
+void strvec_splice(struct strvec *array, size_t idx, size_t len,
+ const char **replacement, size_t replacement_len)
+{
+ if (idx + len > array->nr)
+ BUG("range outside of array boundary");
+ if (replacement_len > len)
+ ALLOC_GROW(array->v, array->nr + (replacement_len - len) + 1,
+ array->alloc);
+ for (size_t i = 0; i < len; i++)
+ free((char *)array->v[idx + i]);
+ if (replacement_len != len) {
+ memmove(array->v + idx + replacement_len, array->v + idx + len,
+ (array->nr - idx - len + 1) * sizeof(char *));
+ array->nr += (replacement_len - len);
+ }
+ for (size_t i = 0; i < replacement_len; i++)
+ array->v[idx + i] = xstrdup(replacement[i]);
+}
+
const char *strvec_replace(struct strvec *array, size_t idx, const char *replacement)
{
char *to_free;