This commit is contained in:
2026-09-28 12:26:29 -04:00
parent 74873807dd
commit b218af2952
2 changed files with 358 additions and 280 deletions
+330 -249
View File
@@ -33,7 +33,7 @@ _vec_char_create(const char* src, const size_t size)
._internal_capacity = CAPACITY, ._internal_capacity = CAPACITY,
._internal_max_size = VEC_MAX, ._internal_max_size = VEC_MAX,
}; };
if (size >= self._internal_max_size) if (size > self._internal_max_size)
{ {
LOGERROR(ERR_MAX_PASSED); LOGERROR(ERR_MAX_PASSED);
LOG("If you need more memory to start, create a blank and " LOG("If you need more memory to start, create a blank and "
@@ -72,137 +72,153 @@ _vec_char_create(const char* src, const size_t size)
return self; return self;
} }
_VecCharInternal size_t
_vec_char_push_back(_VecCharInternal* ptr, const char val) _vec_char_push_back(_VecCharInternal* ptr, const char val)
{ {
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) char* res;
size_t status = _vec_char_is_valid(ptr);
if (status == ERR_VECTOR_NULL)
{ {
return _vec_char_create(nullptr, 0); return ERR_VECTOR_NULL;
} }
_VecCharInternal self = { NULL, ptr->_internal_size,
ptr->_internal_capacity,
ptr->_internal_max_size };
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
{
LOG("If you think this is wrong, reserve memory first");
return self;
}
if (ptr->_internal_size + 1 > ptr->_internal_max_size) if (ptr->_internal_size + 1 > ptr->_internal_max_size)
{ {
LOGERROR(ERR_MAX_PASSED); LOGERROR(ERR_MAX_PASSED);
self = *ptr; return ERR_MAX_PASSED;
return self;
} }
char* dst_buf; if (status == ERR_BUF_NULL && ptr->_internal_size == 0)
if (ptr->_internal_size + 1 > ptr->_internal_capacity)
{ {
if (ptr->_internal_capacity * 2 > ptr->_internal_max_size) res = calloc(1, sizeof(char));
{ if (!res)
LOGERROR(ERR_MAX_PASSED);
self = *ptr;
return self;
}
self._internal_capacity = ptr->_internal_capacity * 2;
dst_buf = realloc(ptr->_internal_arr,
self._internal_capacity * sizeof(char));
if (!dst_buf)
{ {
LOGERROR(ERR_ALLOC_FAIL); LOGERROR(ERR_ALLOC_FAIL);
self = *ptr; return ERR_ALLOC_FAIL;
return self; }
ptr->_internal_arr = res;
ptr->_internal_arr[0] = val;
ptr->_internal_size++;
return SUCCESS;
}
else if (ptr->_internal_size + 1 > ptr->_internal_capacity)
{
size_t dst_cap = ptr->_internal_capacity * 2;
if (dst_cap > ptr->_internal_max_size)
{
ptr->_internal_capacity = ptr->_internal_max_size;
res = realloc(ptr->_internal_arr,
ptr->_internal_capacity * sizeof(char));
LOG("DEBUG: Capacity cannot be grown any further due "
"to hitting max size");
}
else
{
res =
realloc(ptr->_internal_arr, dst_cap * sizeof(char));
ptr->_internal_capacity = dst_cap;
}
if (!res)
{
LOGERROR(ERR_ALLOC_FAIL);
return ERR_ALLOC_FAIL;
} }
} }
else else
{ {
dst_buf = ptr->_internal_arr; res = ptr->_internal_arr;
} }
self._internal_arr = dst_buf; ptr->_internal_arr = res;
self._internal_arr[self._internal_size] = val; ptr->_internal_arr[ptr->_internal_size] = val;
self._internal_size++; ptr->_internal_size++;
return self; return SUCCESS;
} }
_VecCharInternal size_t
_vec_char_pop_back(_VecCharInternal* ptr) _vec_char_pop_back(_VecCharInternal* ptr)
{ {
_VecCharInternal err = _vec_char_create(nullptr, 0); if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
if (_vec_char_is_valid(ptr) != SUCCESS)
{ {
return err; return ERR_VECTOR_NULL;
}
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
{
return ERR_BUF_NULL;
} }
if (ptr->_internal_size < 1) if (ptr->_internal_size < 1)
{ {
LOGERROR(ERR_SIZE_ZERO); LOGERROR(ERR_SIZE_ZERO);
return err; return ERR_SIZE_ZERO;
} }
_VecCharInternal self = { NULL, ptr->_internal_size - 1,
ptr->_internal_capacity, ptr->_internal_arr[ptr->_internal_size - 1] = 0;
ptr->_internal_max_size }; ptr->_internal_size--;
self._internal_arr = ptr->_internal_arr; return SUCCESS;
self._internal_arr[self._internal_size] = 0;
return self;
} }
_VecCharInternal size_t
_vec_char_reserve(const _VecCharInternal* ptr, const size_t capacity) _vec_char_reserve(_VecCharInternal* ptr, const size_t capacity)
{ {
_VecCharInternal self = _vec_char_create(nullptr, 0); char* res;
size_t status = _vec_char_is_valid(ptr);
if (capacity == 0) if (capacity == 0)
{ {
LOGERROR(ERR_SIZE_ZERO); LOGERROR(ERR_SIZE_ZERO);
return self; return ERR_SIZE_ZERO;
} }
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) if (status == ERR_VECTOR_NULL)
{ {
return self; return ERR_VECTOR_NULL;
} }
self._internal_arr = ptr->_internal_arr; if (status == ERR_BUF_NULL)
if (_vec_char_is_valid(&self) == ERR_BUF_NULL)
{ {
self._internal_arr = calloc(capacity, sizeof(char)); res = calloc(ptr->_internal_capacity, sizeof(char));
if (!self._internal_arr) if (!res)
{ {
LOGERROR(ERR_ALLOC_FAIL); LOGERROR(ERR_ALLOC_FAIL);
self = *ptr; return ERR_ALLOC_FAIL;
return self;
} }
} }
else if (_vec_char_is_valid(ptr) == SUCCESS) else if (status == SUCCESS)
{ {
self._internal_arr = res = realloc(ptr->_internal_arr, capacity * sizeof(char));
realloc(self._internal_arr, capacity * sizeof(char)); if (!res)
if (!self._internal_arr)
{ {
LOGERROR(ERR_ALLOC_FAIL); LOGERROR(ERR_ALLOC_FAIL);
self = *ptr; return ERR_ALLOC_FAIL;
return self;
} }
self._internal_size = ptr->_internal_size; if (ptr->_internal_size > capacity)
if (self._internal_size > capacity)
{ {
for (size_t i = capacity; i < self._internal_size; i++) for (size_t i = capacity; i < ptr->_internal_size; i++)
{ {
self._internal_arr[i] = 0; ptr->_internal_arr[i] = '\0';
} }
self._internal_size = capacity; ptr->_internal_size = capacity;
} }
} }
self._internal_capacity = capacity; ptr->_internal_arr = res;
self._internal_max_size = capacity; ptr->_internal_capacity = capacity;
return self; ptr->_internal_max_size = capacity;
return SUCCESS;
} }
_VecCharInternal size_t
_vec_char_shrink_to_fit(_VecCharInternal* ptr) _vec_char_shrink_to_fit(_VecCharInternal* ptr)
{ {
if (_vec_char_is_valid(ptr) != SUCCESS) if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
return _vec_char_create(nullptr, 0); {
return ERR_VECTOR_NULL;
}
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
{
return ERR_BUF_NULL;
}
if (ptr->_internal_size == 0) if (ptr->_internal_size == 0)
{ {
LOGERROR(ERR_SIZE_ZERO); LOGERROR(ERR_SIZE_ZERO);
return _vec_char_create(nullptr, 0); return ERR_SIZE_ZERO;
} }
return _vec_char_reserve(ptr, ptr->_internal_size); return _vec_char_reserve(ptr, ptr->_internal_size);
} }
@@ -284,25 +300,26 @@ _vec_char_delete(const _VecCharInternal* ptr)
} }
} }
_VecCharInternal size_t
_vec_char_clear(const _VecCharInternal* ptr) _vec_char_clear(_VecCharInternal* ptr)
{ {
_VecCharInternal self = _vec_char_create(nullptr, 0); if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL ||
_vec_char_is_valid(ptr) == ERR_BUF_NULL)
{ {
return self; return ERR_VECTOR_NULL;
} }
self = *ptr; if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
if (self._internal_size > 0)
{ {
for (size_t i = 0; i < self._internal_size; i++) return ERR_BUF_NULL;
}
if (ptr->_internal_size > 0)
{ {
self._internal_arr[i] = 0; for (size_t i = 0; i < ptr->_internal_size; i++)
{
ptr->_internal_arr[i] = 0;
} }
self._internal_size = 0; ptr->_internal_size = 0;
} }
return self; return SUCCESS;
} }
char char
@@ -319,8 +336,8 @@ _vec_char_at(const _VecCharInternal* ptr, const size_t idx)
if (idx >= ptr->_internal_size) if (idx >= ptr->_internal_size)
{ {
fprintf(stderr, fprintf(stderr,
"At Index: [%zu] is out of bounds for size: [%zu]", idx, "At Index: [%zu] is out of bounds for size: [%zu]\n",
ptr->_internal_size); idx, ptr->_internal_size);
return ERR_OUT_OF_BOUNDS; return ERR_OUT_OF_BOUNDS;
} }
return ptr->_internal_arr[idx]; return ptr->_internal_arr[idx];
@@ -382,82 +399,84 @@ _vec_char_empty(const _VecCharInternal* ptr)
return FAILURE; return FAILURE;
} }
__attribute__((overloadable)) _VecCharInternal // TODO: Fix return value
_vec_char_erase(const _VecCharInternal* ptr, const size_t iter) __attribute__((overloadable)) size_t
_vec_char_erase(_VecCharInternal* ptr, const size_t iter)
{ {
_VecCharInternal self = _vec_char_create(nullptr, 0); if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) return ERR_VECTOR_NULL;
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) return self; if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) return ERR_BUF_NULL;
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) return self; if (iter >= ptr->_internal_size) return ERR_OUT_OF_BOUNDS;
if (iter >= ptr->_internal_size) return self; memmove(&ptr->_internal_arr[iter], &ptr->_internal_arr[iter + 1],
self = *ptr; (ptr->_internal_size - iter - 1) * sizeof(char));
memmove(&self._internal_arr[iter], &self._internal_arr[iter + 1], ptr->_internal_arr[ptr->_internal_size - 1] = 0;
(self._internal_size - iter - 1) * sizeof(char)); ptr->_internal_size--;
self._internal_arr[self._internal_size - 1] = 0; return SUCCESS;
self._internal_size--;
return self;
} }
__attribute__((overloadable)) _VecCharInternal // TODO: Fix return value
_vec_char_erase(const _VecCharInternal* ptr, const size_t iter_start, __attribute__((overloadable)) size_t
_vec_char_erase(_VecCharInternal* ptr, const size_t iter_start,
const size_t iter_end) const size_t iter_end)
{ {
_VecCharInternal self = _vec_char_create(nullptr, 0); if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) return ERR_VECTOR_NULL;
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) return self; if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) return ERR_BUF_NULL;
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) return self; if (iter_end > ptr->_internal_size) return ERR_OUT_OF_BOUNDS;
if (iter_end > ptr->_internal_size) return self; if (iter_start >= iter_end) return ERR_OUT_OF_BOUNDS;
if (iter_start >= iter_end) return self;
self = *ptr;
size_t diff = iter_end - iter_start; size_t diff = iter_end - iter_start;
memmove(&self._internal_arr[iter_start], &self._internal_arr[iter_end], memmove(&ptr->_internal_arr[iter_start], &ptr->_internal_arr[iter_end],
(self._internal_size - iter_end) * sizeof(char)); (ptr->_internal_size - iter_end) * sizeof(char));
for (size_t i = self._internal_size - diff; i < self._internal_size; for (size_t i = ptr->_internal_size - diff; i < ptr->_internal_size;
i++) i++)
{ {
self._internal_arr[i] = 0; ptr->_internal_arr[i] = 0;
} }
self._internal_size -= diff; ptr->_internal_size -= diff;
return self; return SUCCESS;
} }
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_resize(const _VecCharInternal* ptr, const size_t size) _vec_char_resize(_VecCharInternal* ptr, const size_t size)
{ {
_VecCharInternal self = _vec_char_create(nullptr, 0); if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL || {
_vec_char_is_valid(ptr) == ERR_BUF_NULL) return ERR_VECTOR_NULL;
return self; }
self = *ptr; if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
{
return ERR_BUF_NULL;
}
if (ptr->_internal_size == size) if (ptr->_internal_size == size)
{ {
LOG("ERR :- Ptr size and size are the same"); LOG("ERR :- Ptr size and size are the same");
self = *ptr; return ERR_OUT_OF_BOUNDS;
return self;
} }
else if (size > ptr->_internal_max_size) else if (size > ptr->_internal_max_size)
{ {
LOGERROR(ERR_MAX_PASSED); LOGERROR(ERR_MAX_PASSED);
self = *ptr; return ERR_MAX_PASSED;
return self;
} }
else if (ptr->_internal_size == 0) else if (ptr->_internal_size == 0)
{ {
size_t max = self._internal_max_size; size_t max = ptr->_internal_max_size;
self = _vec_char_reserve(&self, size); size_t status = _vec_char_reserve(ptr, size);
if (self._internal_capacity != size) if (status != SUCCESS)
{
return status;
}
if (ptr->_internal_capacity != size)
{ {
LOGERROR(ERR_ALLOC_FAIL); LOGERROR(ERR_ALLOC_FAIL);
self = *ptr; return ERR_ALLOC_FAIL;
return self;
} }
for (size_t i = 0; i < size; i++) for (size_t i = 0; i < size; i++)
{ {
self._internal_arr[i] = '\0'; ptr->_internal_arr[i] = '\0';
} }
self._internal_max_size = max; ptr->_internal_max_size = max;
self._internal_size = size; ptr->_internal_size = size;
return self; return SUCCESS;
} }
else if (size > ptr->_internal_capacity) else if (size > ptr->_internal_capacity)
{ {
@@ -471,72 +490,79 @@ _vec_char_resize(const _VecCharInternal* ptr, const size_t size)
{ {
tmpcp = size; tmpcp = size;
} }
self = _vec_char_reserve(ptr, tmpcp); size_t status = _vec_char_reserve(ptr, tmpcp);
if (self._internal_capacity != tmpcp) if (status != SUCCESS)
{
return status;
}
if (ptr->_internal_capacity != tmpcp)
{ {
LOGERROR(ERR_ALLOC_FAIL); LOGERROR(ERR_ALLOC_FAIL);
self = *ptr; return ERR_ALLOC_FAIL;
return self;
} }
self._internal_max_size = tmpmx; ptr->_internal_max_size = tmpmx;
} }
if (size < ptr->_internal_size) if (size < ptr->_internal_size)
{ {
for (size_t i = size; i < self._internal_size; i++) for (size_t i = size; i < ptr->_internal_size; i++)
{ {
self._internal_arr[i] = '\0'; ptr->_internal_arr[i] = '\0';
} }
} }
if (self._internal_size < size && size <= self._internal_capacity) if (ptr->_internal_size < size && size <= ptr->_internal_capacity)
{ {
for (size_t i = self._internal_size; i < size; i++) for (size_t i = ptr->_internal_size; i < size; i++)
{ {
self._internal_arr[i] = '\0'; ptr->_internal_arr[i] = '\0';
} }
} }
self._internal_size = size; ptr->_internal_size = size;
return self; return SUCCESS;
} }
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_resize(const _VecCharInternal* ptr, const size_t size, const char val) _vec_char_resize(_VecCharInternal* ptr, const size_t size, const char val)
{ {
_VecCharInternal self = _vec_char_create(nullptr, 0); if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL || {
_vec_char_is_valid(ptr) == ERR_BUF_NULL) return ERR_VECTOR_NULL;
return self; }
self = *ptr; if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
{
return ERR_BUF_NULL;
}
if (ptr->_internal_size == size) if (ptr->_internal_size == size)
{ {
LOG("ERR :- Ptr size and size are the same"); LOG("ERR :- Ptr size and size are the same");
self = *ptr; return ERR_OUT_OF_BOUNDS;
return self;
} }
else if (size > ptr->_internal_max_size) else if (size > ptr->_internal_max_size)
{ {
LOGERROR(ERR_MAX_PASSED); LOGERROR(ERR_MAX_PASSED);
self = *ptr; return ERR_MAX_PASSED;
return self;
} }
else if (ptr->_internal_size == 0) else if (ptr->_internal_size == 0)
{ {
size_t max = self._internal_max_size; size_t max = ptr->_internal_max_size;
self = _vec_char_reserve(&self, size); size_t status = _vec_char_reserve(ptr, size);
if (self._internal_capacity != size) if (status != SUCCESS)
{
return status;
}
if (ptr->_internal_capacity != size)
{ {
LOGERROR(ERR_ALLOC_FAIL); LOGERROR(ERR_ALLOC_FAIL);
self = *ptr; return ERR_ALLOC_FAIL;
return self;
} }
for (size_t i = 0; i < size; i++) for (size_t i = 0; i < size; i++)
{ {
self._internal_arr[i] = val; ptr->_internal_arr[i] = val;
} }
self._internal_max_size = max; ptr->_internal_max_size = max;
self._internal_size = size; ptr->_internal_size = size;
return self; return SUCCESS;
} }
else if (size > ptr->_internal_capacity) else if (size > ptr->_internal_capacity)
{ {
@@ -550,176 +576,231 @@ _vec_char_resize(const _VecCharInternal* ptr, const size_t size, const char val)
{ {
tmpcp = size; tmpcp = size;
} }
self = _vec_char_reserve(ptr, tmpcp); size_t status = _vec_char_reserve(ptr, tmpcp);
if (self._internal_capacity != tmpcp) if (status != SUCCESS)
{ {
LOGERROR(ERR_ALLOC_FAIL); LOGERROR(ERR_ALLOC_FAIL);
self = *ptr; return ERR_ALLOC_FAIL;
return self;
} }
self._internal_max_size = tmpmx; ptr->_internal_max_size = tmpmx;
} }
if (size < ptr->_internal_size) if (size < ptr->_internal_size)
{ {
for (size_t i = size; i < self._internal_size; i++) for (size_t i = size; i < ptr->_internal_size; i++)
{ {
self._internal_arr[i] = val; ptr->_internal_arr[i] = val;
} }
} }
if (self._internal_size < size && size <= self._internal_capacity) if (ptr->_internal_size < size && size <= ptr->_internal_capacity)
{ {
for (size_t i = self._internal_size; i < size; i++) for (size_t i = ptr->_internal_size; i < size; i++)
{ {
self._internal_arr[i] = val; ptr->_internal_arr[i] = val;
} }
} }
self._internal_size = size; ptr->_internal_size = size;
return self; return SUCCESS;
} }
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_assign(const _VecCharInternal* ptr, const _VecCharInternal* iterator, _vec_char_assign(_VecCharInternal* ptr, const _VecCharInternal* iterator,
const size_t iter_start, const size_t iter_end) const size_t iter_start, const size_t iter_end)
{ {
_VecCharInternal self = _vec_char_create(nullptr, 0); if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL || {
_vec_char_is_valid(ptr) == ERR_BUF_NULL) return ERR_VECTOR_NULL;
return self; }
if (_vec_char_is_valid(iterator) == ERR_VECTOR_NULL || if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
_vec_char_is_valid(iterator) == ERR_BUF_NULL) {
return self; return ERR_BUF_NULL;
}
if (_vec_char_is_valid(iterator) == ERR_VECTOR_NULL)
{
return ERR_VECTOR_NULL;
}
if (_vec_char_is_valid(iterator) == ERR_BUF_NULL)
{
return ERR_BUF_NULL;
}
if (ptr->_internal_arr == iterator->_internal_arr) if (ptr->_internal_arr == iterator->_internal_arr)
{ {
LOGERROR(ERR_DOUBLE_BUF); LOGERROR(ERR_DOUBLE_BUF);
self = *ptr; return ERR_DOUBLE_BUF;
return self;
} }
if (iterator->_internal_size < iter_end || iter_end <= iter_start) if (iterator->_internal_size < iter_end || iter_end <= iter_start)
{ {
LOGERROR(ERR_OUT_OF_BOUNDS); LOGERROR(ERR_OUT_OF_BOUNDS);
return self; return ERR_OUT_OF_BOUNDS;
} }
self = *ptr; size_t status =
self =
_vec_char_erase(ptr, _vec_char_begin(ptr), _vec_char_end(ptr) + 1); _vec_char_erase(ptr, _vec_char_begin(ptr), _vec_char_end(ptr) + 1);
self = _vec_char_reserve(&self, (iter_end - iter_start)); if (status != SUCCESS)
{
return status;
}
status = _vec_char_reserve(ptr, (iter_end - iter_start));
if (status != SUCCESS)
{
return status;
}
for (size_t i = iter_start; i < iter_end; i++) for (size_t i = iter_start; i < iter_end; i++)
{ {
self = _vec_char_push_back(&self, _vec_char_at(iterator, i)); status = _vec_char_push_back(ptr, _vec_char_at(iterator, i));
if (status != SUCCESS)
{
return status;
} }
return self; }
return SUCCESS;
} }
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_assign(const _VecCharInternal* ptr, const size_t amount, _vec_char_assign(_VecCharInternal* ptr, const size_t amount, const char val)
const char val)
{ {
_VecCharInternal self = _vec_char_create(nullptr, 0); if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL || {
_vec_char_is_valid(ptr) == ERR_BUF_NULL) return ERR_VECTOR_NULL;
return _vec_char_create(nullptr, 0); }
self = *ptr; if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
{
return ERR_BUF_NULL;
}
if (amount == 0) if (amount == 0)
{ {
LOGERROR(ERR_SIZE_ZERO); LOGERROR(ERR_SIZE_ZERO);
return self; return ERR_SIZE_ZERO;
} }
self = _vec_char_erase(&self, _vec_char_begin(&self), size_t status =
_vec_char_end(&self) + 1); _vec_char_erase(ptr, _vec_char_begin(ptr), _vec_char_end(ptr) + 1);
self = _vec_char_reserve(&self, amount); if (status != SUCCESS)
{
return status;
}
status = _vec_char_reserve(ptr, amount);
if (status != SUCCESS)
{
return status;
}
for (size_t i = 0; i < amount; i++) for (size_t i = 0; i < amount; i++)
{ {
self = _vec_char_push_back(&self, val); status = _vec_char_push_back(ptr, val);
if (status != SUCCESS)
{
return status;
} }
return self; }
return SUCCESS;
} }
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_insert(const _VecCharInternal* ptr, const size_t iter, const char val) _vec_char_insert(_VecCharInternal* ptr, const size_t iter, const char val)
{ {
_VecCharInternal self = _vec_char_create(nullptr, 0); if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL ||
_vec_char_is_valid(ptr) == ERR_BUF_NULL)
{ {
return self; return ERR_VECTOR_NULL;
}
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
{
return ERR_BUF_NULL;
} }
if (ptr->_internal_max_size < ptr->_internal_size + 1) if (ptr->_internal_max_size < ptr->_internal_size + 1)
{ {
LOGERROR(ERR_MAX_PASSED); LOGERROR(ERR_MAX_PASSED);
return self; return ERR_MAX_PASSED;
} }
if (iter > ptr->_internal_size) if (iter > ptr->_internal_size)
{ {
LOGERROR(ERR_OUT_OF_BOUNDS); LOGERROR(ERR_OUT_OF_BOUNDS);
return self; return ERR_OUT_OF_BOUNDS;
} }
self = *ptr; size_t status = _vec_char_push_back(ptr, 0);
self = _vec_char_push_back(&self, 0); if (status != SUCCESS)
for (size_t i = self._internal_size - 1; i > iter; i--)
{ {
self._internal_arr[i] = self._internal_arr[i - 1]; return status;
} }
self._internal_arr[iter] = val; for (size_t i = ptr->_internal_size - 1; i > iter; i--)
{
ptr->_internal_arr[i] = ptr->_internal_arr[i - 1];
}
ptr->_internal_arr[iter] = val;
return self; return SUCCESS;
} }
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_insert(const _VecCharInternal* ptr, const size_t iter, _vec_char_insert(_VecCharInternal* ptr, const size_t iter, const size_t amount,
const size_t amount, const char val) const char val)
{ {
_VecCharInternal self = _vec_char_create(nullptr, 0); if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
if (_vec_char_is_valid(ptr) != SUCCESS)
{ {
LOGERROR(ERR_NULL); return ERR_VECTOR_NULL;
return self; }
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
{
return ERR_BUF_NULL;
} }
if (iter > ptr->_internal_size) if (iter > ptr->_internal_size)
{ {
LOGERROR(ERR_OUT_OF_BOUNDS); LOGERROR(ERR_OUT_OF_BOUNDS);
return self; return ERR_OUT_OF_BOUNDS;
} }
if (amount <= 0) if (amount <= 0)
{ {
LOGERROR(ERR_SIZE_ZERO); LOGERROR(ERR_SIZE_ZERO);
return self; return ERR_SIZE_ZERO;
} }
self = *ptr;
for (size_t i = iter; i < iter + amount; i++) for (size_t i = iter; i < iter + amount; i++)
{ {
self = _vec_char_insert(&self, i, val); size_t status = _vec_char_insert(ptr, i, val);
if (status != SUCCESS)
{
return status;
} }
return self; }
return SUCCESS;
} }
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_insert(const _VecCharInternal* ptr, const _VecCharInternal* iterator, _vec_char_insert(_VecCharInternal* ptr, const _VecCharInternal* iterator,
const size_t iter, const size_t iter_start, const size_t iter, const size_t iter_start,
const size_t iter_end) const size_t iter_end)
{ {
_VecCharInternal self = _vec_char_create(nullptr, 0); if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
if (_vec_char_is_valid(ptr) != SUCCESS ||
_vec_char_is_valid(iterator) != SUCCESS)
{ {
LOGERROR(ERR_NULL); return ERR_VECTOR_NULL;
return self;
} }
if (iter_end > iterator->_internal_size || iter > ptr->_internal_size) if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
{
return ERR_BUF_NULL;
}
if (_vec_char_is_valid(iterator) == ERR_VECTOR_NULL)
{
return ERR_VECTOR_NULL;
}
if (_vec_char_is_valid(iterator) == ERR_BUF_NULL)
{
return ERR_BUF_NULL;
}
if (iter_end > iter_start || iter_end > iterator->_internal_size ||
iter > ptr->_internal_size)
{ {
LOGERROR(ERR_OUT_OF_BOUNDS); LOGERROR(ERR_OUT_OF_BOUNDS);
return self; return ERR_OUT_OF_BOUNDS;
} }
if (ptr->_internal_arr == iterator->_internal_arr) if (ptr->_internal_arr == iterator->_internal_arr)
{ {
LOGERROR(ERR_DOUBLE_BUF); LOGERROR(ERR_DOUBLE_BUF);
return self; return ERR_DOUBLE_BUF;
} }
self = *ptr;
for (size_t i = iter_start; i < iter_end; i++) for (size_t i = iter_start; i < iter_end; i++)
{ {
self = _vec_char_insert(&self, iter + (i - iter_start), size_t status = _vec_char_insert(ptr, iter + (i - iter_start),
iterator->_internal_arr[i]); iterator->_internal_arr[i]);
if (status != SUCCESS)
{
return status;
} }
return self; }
return SUCCESS;
} }
+27 -30
View File
@@ -22,16 +22,16 @@ _vec_char_is_valid(const _VecCharInternal* ptr);
_VecCharInternal _VecCharInternal
_vec_char_create(const char* src, const size_t size); _vec_char_create(const char* src, const size_t size);
_VecCharInternal size_t
_vec_char_push_back(_VecCharInternal* vec, const char val); _vec_char_push_back(_VecCharInternal* vec, const char val);
_VecCharInternal size_t
_vec_char_pop_back(_VecCharInternal* ptr); _vec_char_pop_back(_VecCharInternal* ptr);
_VecCharInternal size_t
_vec_char_reserve(const _VecCharInternal* ptr, const size_t max_size); _vec_char_reserve(_VecCharInternal* ptr, const size_t max_size);
_VecCharInternal size_t
_vec_char_shrink_to_fit(_VecCharInternal* ptr); _vec_char_shrink_to_fit(_VecCharInternal* ptr);
size_t size_t
@@ -49,8 +49,8 @@ _vec_char_max_size(const _VecCharInternal* ptr);
void void
_vec_char_delete(const _VecCharInternal* ptr); _vec_char_delete(const _VecCharInternal* ptr);
_VecCharInternal size_t
_vec_char_clear(const _VecCharInternal* ptr); _vec_char_clear(_VecCharInternal* ptr);
char char
_vec_char_at(const _VecCharInternal* ptr, const size_t idx); _vec_char_at(const _VecCharInternal* ptr, const size_t idx);
@@ -73,38 +73,35 @@ _vec_char_back(const _VecCharInternal* ptr);
size_t size_t
_vec_char_empty(const _VecCharInternal* ptr); _vec_char_empty(const _VecCharInternal* ptr);
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_erase(const _VecCharInternal* ptr, const size_t iter); _vec_char_erase(_VecCharInternal* ptr, const size_t iter);
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_erase(const _VecCharInternal* ptr, const size_t iter_start, _vec_char_erase(_VecCharInternal* ptr, const size_t iter_start,
const size_t iter_end); const size_t iter_end);
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_resize(const _VecCharInternal* ptr, const size_t size); _vec_char_resize(_VecCharInternal* ptr, const size_t size);
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_resize(const _VecCharInternal* ptr, const size_t size, _vec_char_resize(_VecCharInternal* ptr, const size_t size, const char val);
const char val);
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_assign(const _VecCharInternal* ptr, const _VecCharInternal* iterator, _vec_char_assign(_VecCharInternal* ptr, const _VecCharInternal* iterator,
const size_t iter_start, const size_t iter_end); const size_t iter_start, const size_t iter_end);
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_assign(const _VecCharInternal* ptr, const size_t amount, _vec_char_assign(_VecCharInternal* ptr, const size_t amount, const char val);
__attribute__((overloadable)) size_t
_vec_char_insert(_VecCharInternal* ptr, const size_t iter, const char val);
__attribute__((overloadable)) size_t
_vec_char_insert(_VecCharInternal* ptr, const size_t iter, const size_t amount,
const char val); const char val);
__attribute__((overloadable)) _VecCharInternal __attribute__((overloadable)) size_t
_vec_char_insert(const _VecCharInternal* ptr, const size_t iter, _vec_char_insert(_VecCharInternal* ptr, const _VecCharInternal* iterator,
const char val);
__attribute__((overloadable)) _VecCharInternal
_vec_char_insert(const _VecCharInternal* ptr, const size_t iter,
const size_t amount, const char val);
__attribute__((overloadable)) _VecCharInternal
_vec_char_insert(const _VecCharInternal* ptr, const _VecCharInternal* iterator,
const size_t iter, const size_t iter_start, const size_t iter, const size_t iter_start,
const size_t iter_end); const size_t iter_end);