Commit 53571f2f by Edward Thomson Committed by Edward Thomson

vector: more sensible names for `grow_at`/`shrink_at`

parent 440e3bae
......@@ -137,10 +137,10 @@ static int update_hunk(
int error = 0;
if (postlen > prelen)
error = git_vector_grow_at(
error = git_vector_insert_null(
&image->lines, linenum, (postlen - prelen));
else if (prelen > postlen)
error = git_vector_shrink_at(
error = git_vector_remove_range(
&image->lines, linenum, (prelen - postlen));
if (error) {
......
......@@ -331,33 +331,33 @@ int git_vector_resize_to(git_vector *v, size_t new_length)
return 0;
}
int git_vector_grow_at(git_vector *v, size_t idx, size_t grow_len)
int git_vector_insert_null(git_vector *v, size_t idx, size_t insert_len)
{
size_t new_length;
assert(grow_len > 0 && idx <= v->length);
assert(insert_len > 0 && idx <= v->length);
GITERR_CHECK_ALLOC_ADD(&new_length, v->length, grow_len);
GITERR_CHECK_ALLOC_ADD(&new_length, v->length, insert_len);
if (new_length > v->_alloc_size && resize_vector(v, new_length) < 0)
return -1;
memmove(&v->contents[idx + grow_len], &v->contents[idx],
memmove(&v->contents[idx + insert_len], &v->contents[idx],
sizeof(void *) * (v->length - idx));
memset(&v->contents[idx], 0, sizeof(void *) * grow_len);
memset(&v->contents[idx], 0, sizeof(void *) * insert_len);
v->length = new_length;
return 0;
}
int git_vector_shrink_at(git_vector *v, size_t idx, size_t shrink_len)
int git_vector_remove_range(git_vector *v, size_t idx, size_t remove_len)
{
size_t new_length = v->length - shrink_len;
size_t new_length = v->length - remove_len;
size_t end_idx = 0;
assert(shrink_len > 0);
assert(remove_len > 0);
if (git__add_sizet_overflow(&end_idx, idx, shrink_len))
if (git__add_sizet_overflow(&end_idx, idx, remove_len))
assert(0);
assert(end_idx <= v->length);
......@@ -366,7 +366,7 @@ int git_vector_shrink_at(git_vector *v, size_t idx, size_t shrink_len)
memmove(&v->contents[idx], &v->contents[end_idx],
sizeof(void *) * (v->length - end_idx));
memset(&v->contents[new_length], 0, sizeof(void *) * shrink_len);
memset(&v->contents[new_length], 0, sizeof(void *) * remove_len);
v->length = new_length;
return 0;
......
......@@ -93,8 +93,8 @@ void git_vector_remove_matching(
void *payload);
int git_vector_resize_to(git_vector *v, size_t new_length);
int git_vector_grow_at(git_vector *v, size_t idx, size_t grow_len);
int git_vector_shrink_at(git_vector *v, size_t idx, size_t shrink_len);
int git_vector_insert_null(git_vector *v, size_t idx, size_t insert_len);
int git_vector_remove_range(git_vector *v, size_t idx, size_t remove_len);
int git_vector_set(void **old, git_vector *v, size_t position, void *value);
......
......@@ -338,40 +338,40 @@ void test_core_vector__grow_and_shrink(void)
git_vector_insert(&x, (void *)0x09);
git_vector_insert(&x, (void *)0x0a);
git_vector_shrink_at(&x, 0, 1);
git_vector_remove_range(&x, 0, 1);
assert_vector(&x, expected1, ARRAY_SIZE(expected1));
git_vector_shrink_at(&x, 1, 1);
git_vector_remove_range(&x, 1, 1);
assert_vector(&x, expected2, ARRAY_SIZE(expected2));
git_vector_shrink_at(&x, 4, 3);
git_vector_remove_range(&x, 4, 3);
assert_vector(&x, expected3, ARRAY_SIZE(expected3));
git_vector_shrink_at(&x, 3, 2);
git_vector_remove_range(&x, 3, 2);
assert_vector(&x, expected4, ARRAY_SIZE(expected4));
git_vector_grow_at(&x, 0, 2);
git_vector_insert_null(&x, 0, 2);
assert_vector(&x, expected5, ARRAY_SIZE(expected5));
git_vector_grow_at(&x, 5, 1);
git_vector_insert_null(&x, 5, 1);
assert_vector(&x, expected6, ARRAY_SIZE(expected6));
git_vector_grow_at(&x, 4, 3);
git_vector_insert_null(&x, 4, 3);
assert_vector(&x, expected7, ARRAY_SIZE(expected7));
git_vector_shrink_at(&x, 0, 3);
git_vector_remove_range(&x, 0, 3);
assert_vector(&x, expected8, ARRAY_SIZE(expected8));
git_vector_shrink_at(&x, 1, 2);
git_vector_remove_range(&x, 1, 2);
assert_vector(&x, expected9, ARRAY_SIZE(expected9));
git_vector_shrink_at(&x, 2, 2);
git_vector_remove_range(&x, 2, 2);
assert_vector(&x, expectedA, ARRAY_SIZE(expectedA));
git_vector_shrink_at(&x, 1, 1);
git_vector_remove_range(&x, 1, 1);
assert_vector(&x, expectedB, ARRAY_SIZE(expectedB));
git_vector_shrink_at(&x, 0, 1);
git_vector_remove_range(&x, 0, 1);
assert_vector(&x, NULL, 0);
git_vector_free(&x);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment