From 439bc3acd3309099216e8c997887dae321cd09c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Fri, 26 Jun 2015 22:20:37 +0200 Subject: [PATCH] Add str_vector_steal() --- liberty.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/liberty.c b/liberty.c index 8ea860f..1ae9df4 100644 --- a/liberty.c +++ b/liberty.c @@ -401,13 +401,20 @@ str_vector_add_vector (struct str_vector *self, char **vector) str_vector_add (self, *vector++); } +static char * +str_vector_steal (struct str_vector *self, size_t i) +{ + hard_assert (i < self->len); + char *tmp = self->vector[i]; + memmove (self->vector + i, self->vector + i + 1, + (self->len-- - i) * sizeof *self->vector); + return tmp; +} + static void str_vector_remove (struct str_vector *self, size_t i) { - hard_assert (i < self->len); - free (self->vector[i]); - memmove (self->vector + i, self->vector + i + 1, - (self->len-- - i) * sizeof *self->vector); + free (str_vector_steal (self, i)); } // --- Dynamically allocated strings -------------------------------------------