added overflow-safe grow functions
Minclude/cutils/byte_array.h
@@ -46,6 +46,7 @@ static void bytearray_remove(Bytearray* bytearray, size_t index, void (*rmv)(voi
}
void bytearray_remove_range(Bytearray* bytearray, size_t index, size_t length, void (*rmv)(void*));
bool bytearray_grow(Bytearray* bytearray, size_t add);
bool bytearray_adjust_size(Bytearray* bytearray, size_t size);
bool bytearray_shrink(Bytearray* bytearray);
HEDLEY_NON_NULL(3)
Minclude/cutils/dyn_string.h
@@ -43,6 +43,7 @@ static void string_remove(String* string, size_t index)
void string_remove_range(String* string, size_t index, size_t length);
size_t string_strip(String* string, char character);
bool string_grow(String* string, size_t add);
bool string_adjust_size(String* string, size_t size);
bool string_find_char(const String* haystack, const char needle, size_t* pos);
HEDLEY_INLINE
Minclude/cutils/vector.h
@@ -42,6 +42,7 @@ static void vector_remove(Vector* vector, size_t index, void (*rmv)(void*))
}
void vector_remove_range(Vector* vector, size_t index, size_t length, void (*rmv)(void*));
bool vector_grow(Vector* vector, size_t add);
bool vector_adjust_size(Vector* vector, size_t size);
bool vector_shrink(Vector* vector);
HEDLEY_NON_NULL(3)
Msrc/byte_array.c
@@ -75,7 +75,7 @@ bool bytearray_insert(Bytearray* bytearray, size_t index, const void* item)
size_t length = bytearray->length;
size_t size = bytearray->element_size;
if(index > length || length == SIZE_MAX || !bytearray_adjust_size(bytearray, length+1))
if(index > length || !bytearray_grow(bytearray, 1))
return false;
memmove(bytearray->items+index*size+1*size, bytearray->items+index*size, (length*size-index*size)*sizeof(*bytearray->items));
@@ -84,6 +84,13 @@ bool bytearray_insert(Bytearray* bytearray, size_t index, const void* item)
return true;
}
bool bytearray_grow(Bytearray* bytearray, size_t add)
{
if(bytearray->length+add < bytearray->length)
return false;
return bytearray_adjust_size(bytearray, bytearray->length+add);
}
bool bytearray_adjust_size(Bytearray* bytearray, size_t size)
{
while(bytearray->capacity < size)
Msrc/dyn_string.c
@@ -55,7 +55,7 @@ size_t string_strip(String* string, char character)
bool string_insert(String* string, size_t index, char character)
{
if(index > string->length || string->length == SIZE_MAX || !string_adjust_size(string, string->length+1))
if(index > string->length || !string_grow(string, 1))
return false;
memmove(string->chars+index+1, string->chars+index, (string->length-index+(string->null_terminated?1:0))*sizeof(*string->chars));
@@ -73,7 +73,9 @@ char string_pop_at(String* string, size_t index)
bool string_concat(String* string, const String* other)
{
if(!string_adjust_size(string,string->length+other->length-1))
if(other->length == 0)
return true;
if(!string_grow(string, other->length-1))
return false;
memcpy(string->chars+string->length,other->chars,other->length);
string->length += other->length;
@@ -107,6 +109,13 @@ size_t string_count(const String* string, char character)
return ret;
}
bool string_grow(String* string, size_t add)
{
if(string->length+add < string->length)
return false;
return string_adjust_size(string, string->length+add);
}
bool string_adjust_size(String* string, size_t size)
{
if(size == SIZE_MAX && string->null_terminated)
Msrc/vector.c
@@ -61,7 +61,7 @@ size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const
bool vector_insert(Vector* vector, size_t index, void* item)
{
if(index > vector->length || vector->length == SIZE_MAX || !vector_adjust_size(vector, vector->length+1))
if(index > vector->length || !vector_grow(vector, 1))
return false;
memmove(vector->items+index+1, vector->items+index, (vector->length-index)*sizeof(*vector->items));
@@ -70,6 +70,13 @@ bool vector_insert(Vector* vector, size_t index, void* item)
return true;
}
bool vector_grow(Vector* vector, size_t add)
{
if(vector->length+add < vector->length)
return false;
return vector_adjust_size(vector, vector->length+add);
}
bool vector_adjust_size(Vector* vector, size_t size)
{
while(vector->capacity < size)