From b218af295290d2713a3f8620a67607e9f27b718c Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Mon, 28 Sep 2026 12:26:29 -0400 Subject: [PATCH] BACKUP --- src/lib/char/char.c | 581 +++++++++++++++++++++++++------------------- src/lib/char/char.h | 57 ++--- 2 files changed, 358 insertions(+), 280 deletions(-) diff --git a/src/lib/char/char.c b/src/lib/char/char.c index 69d859a..cb4ed05 100644 --- a/src/lib/char/char.c +++ b/src/lib/char/char.c @@ -33,7 +33,7 @@ _vec_char_create(const char* src, const size_t size) ._internal_capacity = CAPACITY, ._internal_max_size = VEC_MAX, }; - if (size >= self._internal_max_size) + if (size > self._internal_max_size) { LOGERROR(ERR_MAX_PASSED); 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; } -_VecCharInternal +size_t _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) { LOGERROR(ERR_MAX_PASSED); - self = *ptr; - return self; + return ERR_MAX_PASSED; } - char* dst_buf; - if (ptr->_internal_size + 1 > ptr->_internal_capacity) + if (status == ERR_BUF_NULL && ptr->_internal_size == 0) { - if (ptr->_internal_capacity * 2 > ptr->_internal_max_size) - { - 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) + res = calloc(1, sizeof(char)); + if (!res) { LOGERROR(ERR_ALLOC_FAIL); - self = *ptr; - return self; + return ERR_ALLOC_FAIL; + } + 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 { - dst_buf = ptr->_internal_arr; + res = ptr->_internal_arr; } - self._internal_arr = dst_buf; - self._internal_arr[self._internal_size] = val; - self._internal_size++; - return self; + ptr->_internal_arr = res; + ptr->_internal_arr[ptr->_internal_size] = val; + ptr->_internal_size++; + return SUCCESS; } -_VecCharInternal +size_t _vec_char_pop_back(_VecCharInternal* ptr) { - _VecCharInternal err = _vec_char_create(nullptr, 0); - if (_vec_char_is_valid(ptr) != SUCCESS) + if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) { - return err; + return ERR_VECTOR_NULL; + } + if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) + { + return ERR_BUF_NULL; } if (ptr->_internal_size < 1) { LOGERROR(ERR_SIZE_ZERO); - return err; + return ERR_SIZE_ZERO; } - _VecCharInternal self = { NULL, ptr->_internal_size - 1, - ptr->_internal_capacity, - ptr->_internal_max_size }; - self._internal_arr = ptr->_internal_arr; - self._internal_arr[self._internal_size] = 0; - return self; + + ptr->_internal_arr[ptr->_internal_size - 1] = 0; + ptr->_internal_size--; + return SUCCESS; } -_VecCharInternal -_vec_char_reserve(const _VecCharInternal* ptr, const size_t capacity) +size_t +_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) { 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 (_vec_char_is_valid(&self) == ERR_BUF_NULL) + if (status == ERR_BUF_NULL) { - self._internal_arr = calloc(capacity, sizeof(char)); - if (!self._internal_arr) + res = calloc(ptr->_internal_capacity, sizeof(char)); + if (!res) { LOGERROR(ERR_ALLOC_FAIL); - self = *ptr; - return self; + return ERR_ALLOC_FAIL; } } - else if (_vec_char_is_valid(ptr) == SUCCESS) + else if (status == SUCCESS) { - self._internal_arr = - realloc(self._internal_arr, capacity * sizeof(char)); - if (!self._internal_arr) + res = realloc(ptr->_internal_arr, capacity * sizeof(char)); + if (!res) { LOGERROR(ERR_ALLOC_FAIL); - self = *ptr; - return self; + return ERR_ALLOC_FAIL; } - self._internal_size = ptr->_internal_size; - if (self._internal_size > capacity) + if (ptr->_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; - self._internal_max_size = capacity; - return self; + ptr->_internal_arr = res; + ptr->_internal_capacity = capacity; + ptr->_internal_max_size = capacity; + return SUCCESS; } -_VecCharInternal +size_t _vec_char_shrink_to_fit(_VecCharInternal* ptr) { - if (_vec_char_is_valid(ptr) != SUCCESS) - return _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_BUF_NULL) + { + return ERR_BUF_NULL; + } if (ptr->_internal_size == 0) { LOGERROR(ERR_SIZE_ZERO); - return _vec_char_create(nullptr, 0); + return ERR_SIZE_ZERO; } return _vec_char_reserve(ptr, ptr->_internal_size); } @@ -284,25 +300,26 @@ _vec_char_delete(const _VecCharInternal* ptr) } } -_VecCharInternal -_vec_char_clear(const _VecCharInternal* ptr) +size_t +_vec_char_clear(_VecCharInternal* ptr) { - _VecCharInternal self = _vec_char_create(nullptr, 0); - if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL || - _vec_char_is_valid(ptr) == ERR_BUF_NULL) + if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) { - return self; + return ERR_VECTOR_NULL; } - self = *ptr; - if (self._internal_size > 0) + if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) { - for (size_t i = 0; i < self._internal_size; i++) + return ERR_BUF_NULL; + } + if (ptr->_internal_size > 0) + { + for (size_t i = 0; i < ptr->_internal_size; i++) { - self._internal_arr[i] = 0; + ptr->_internal_arr[i] = 0; } - self._internal_size = 0; + ptr->_internal_size = 0; } - return self; + return SUCCESS; } char @@ -319,8 +336,8 @@ _vec_char_at(const _VecCharInternal* ptr, const size_t idx) if (idx >= ptr->_internal_size) { fprintf(stderr, - "At Index: [%zu] is out of bounds for size: [%zu]", idx, - ptr->_internal_size); + "At Index: [%zu] is out of bounds for size: [%zu]\n", + idx, ptr->_internal_size); return ERR_OUT_OF_BOUNDS; } return ptr->_internal_arr[idx]; @@ -382,82 +399,84 @@ _vec_char_empty(const _VecCharInternal* ptr) return FAILURE; } -__attribute__((overloadable)) _VecCharInternal -_vec_char_erase(const _VecCharInternal* ptr, const size_t iter) +// TODO: Fix return value +__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 self; - if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) return self; - if (iter >= ptr->_internal_size) return self; - self = *ptr; - memmove(&self._internal_arr[iter], &self._internal_arr[iter + 1], - (self._internal_size - iter - 1) * sizeof(char)); - self._internal_arr[self._internal_size - 1] = 0; - self._internal_size--; - return self; + if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) return ERR_VECTOR_NULL; + if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) return ERR_BUF_NULL; + if (iter >= ptr->_internal_size) return ERR_OUT_OF_BOUNDS; + memmove(&ptr->_internal_arr[iter], &ptr->_internal_arr[iter + 1], + (ptr->_internal_size - iter - 1) * sizeof(char)); + ptr->_internal_arr[ptr->_internal_size - 1] = 0; + ptr->_internal_size--; + return SUCCESS; } -__attribute__((overloadable)) _VecCharInternal -_vec_char_erase(const _VecCharInternal* ptr, const size_t iter_start, +// TODO: Fix return value +__attribute__((overloadable)) size_t +_vec_char_erase(_VecCharInternal* ptr, 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) return self; - if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) return self; - if (iter_end > ptr->_internal_size) return self; - if (iter_start >= iter_end) return self; - self = *ptr; + if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) return ERR_VECTOR_NULL; + if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) return ERR_BUF_NULL; + if (iter_end > ptr->_internal_size) return ERR_OUT_OF_BOUNDS; + if (iter_start >= iter_end) return ERR_OUT_OF_BOUNDS; size_t diff = iter_end - iter_start; - memmove(&self._internal_arr[iter_start], &self._internal_arr[iter_end], - (self._internal_size - iter_end) * sizeof(char)); - for (size_t i = self._internal_size - diff; i < self._internal_size; + memmove(&ptr->_internal_arr[iter_start], &ptr->_internal_arr[iter_end], + (ptr->_internal_size - iter_end) * sizeof(char)); + for (size_t i = ptr->_internal_size - diff; i < ptr->_internal_size; i++) { - self._internal_arr[i] = 0; + ptr->_internal_arr[i] = 0; } - self._internal_size -= diff; - return self; + ptr->_internal_size -= diff; + return SUCCESS; } -__attribute__((overloadable)) _VecCharInternal -_vec_char_resize(const _VecCharInternal* ptr, const size_t size) +__attribute__((overloadable)) size_t +_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 || - _vec_char_is_valid(ptr) == ERR_BUF_NULL) - return self; - self = *ptr; + if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) + { + return ERR_VECTOR_NULL; + } + if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) + { + return ERR_BUF_NULL; + } if (ptr->_internal_size == size) { LOG("ERR :- Ptr size and size are the same"); - self = *ptr; - return self; + return ERR_OUT_OF_BOUNDS; } else if (size > ptr->_internal_max_size) { LOGERROR(ERR_MAX_PASSED); - self = *ptr; - return self; + return ERR_MAX_PASSED; } else if (ptr->_internal_size == 0) { - size_t max = self._internal_max_size; - self = _vec_char_reserve(&self, size); - if (self._internal_capacity != size) + size_t max = ptr->_internal_max_size; + size_t status = _vec_char_reserve(ptr, size); + if (status != SUCCESS) + { + return status; + } + if (ptr->_internal_capacity != size) { LOGERROR(ERR_ALLOC_FAIL); - self = *ptr; - return self; + return ERR_ALLOC_FAIL; } for (size_t i = 0; i < size; i++) { - self._internal_arr[i] = '\0'; + ptr->_internal_arr[i] = '\0'; } - self._internal_max_size = max; - self._internal_size = size; - return self; + ptr->_internal_max_size = max; + ptr->_internal_size = size; + return SUCCESS; } else if (size > ptr->_internal_capacity) { @@ -471,72 +490,79 @@ _vec_char_resize(const _VecCharInternal* ptr, const size_t size) { tmpcp = size; } - self = _vec_char_reserve(ptr, tmpcp); - if (self._internal_capacity != tmpcp) + size_t status = _vec_char_reserve(ptr, tmpcp); + if (status != SUCCESS) + { + return status; + } + if (ptr->_internal_capacity != tmpcp) { LOGERROR(ERR_ALLOC_FAIL); - self = *ptr; - return self; + return ERR_ALLOC_FAIL; } - self._internal_max_size = tmpmx; + ptr->_internal_max_size = tmpmx; } 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; - return self; + ptr->_internal_size = size; + return SUCCESS; } -__attribute__((overloadable)) _VecCharInternal -_vec_char_resize(const _VecCharInternal* ptr, const size_t size, const char val) +__attribute__((overloadable)) size_t +_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 || - _vec_char_is_valid(ptr) == ERR_BUF_NULL) - return self; - self = *ptr; + if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) + { + return ERR_VECTOR_NULL; + } + if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) + { + return ERR_BUF_NULL; + } if (ptr->_internal_size == size) { LOG("ERR :- Ptr size and size are the same"); - self = *ptr; - return self; + return ERR_OUT_OF_BOUNDS; } else if (size > ptr->_internal_max_size) { LOGERROR(ERR_MAX_PASSED); - self = *ptr; - return self; + return ERR_MAX_PASSED; } else if (ptr->_internal_size == 0) { - size_t max = self._internal_max_size; - self = _vec_char_reserve(&self, size); - if (self._internal_capacity != size) + size_t max = ptr->_internal_max_size; + size_t status = _vec_char_reserve(ptr, size); + if (status != SUCCESS) + { + return status; + } + if (ptr->_internal_capacity != size) { LOGERROR(ERR_ALLOC_FAIL); - self = *ptr; - return self; + return ERR_ALLOC_FAIL; } for (size_t i = 0; i < size; i++) { - self._internal_arr[i] = val; + ptr->_internal_arr[i] = val; } - self._internal_max_size = max; - self._internal_size = size; - return self; + ptr->_internal_max_size = max; + ptr->_internal_size = size; + return SUCCESS; } 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; } - self = _vec_char_reserve(ptr, tmpcp); - if (self._internal_capacity != tmpcp) + size_t status = _vec_char_reserve(ptr, tmpcp); + if (status != SUCCESS) { LOGERROR(ERR_ALLOC_FAIL); - self = *ptr; - return self; + return ERR_ALLOC_FAIL; } - self._internal_max_size = tmpmx; + ptr->_internal_max_size = tmpmx; } 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; - return self; + ptr->_internal_size = size; + return SUCCESS; } -__attribute__((overloadable)) _VecCharInternal -_vec_char_assign(const _VecCharInternal* ptr, const _VecCharInternal* iterator, +__attribute__((overloadable)) size_t +_vec_char_assign(_VecCharInternal* ptr, const _VecCharInternal* iterator, 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 || - _vec_char_is_valid(ptr) == ERR_BUF_NULL) - return self; - if (_vec_char_is_valid(iterator) == ERR_VECTOR_NULL || - _vec_char_is_valid(iterator) == ERR_BUF_NULL) - return self; - + if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) + { + return ERR_VECTOR_NULL; + } + 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 (ptr->_internal_arr == iterator->_internal_arr) { LOGERROR(ERR_DOUBLE_BUF); - self = *ptr; - return self; + return ERR_DOUBLE_BUF; } if (iterator->_internal_size < iter_end || iter_end <= iter_start) { LOGERROR(ERR_OUT_OF_BOUNDS); - return self; + return ERR_OUT_OF_BOUNDS; } - self = *ptr; - self = + size_t status = _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++) { - 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 -_vec_char_assign(const _VecCharInternal* ptr, const size_t amount, - const char val) +__attribute__((overloadable)) size_t +_vec_char_assign(_VecCharInternal* ptr, const size_t amount, const char val) { - _VecCharInternal self = _vec_char_create(nullptr, 0); - if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL || - _vec_char_is_valid(ptr) == ERR_BUF_NULL) - return _vec_char_create(nullptr, 0); - self = *ptr; + if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) + { + return ERR_VECTOR_NULL; + } + if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) + { + return ERR_BUF_NULL; + } if (amount == 0) { LOGERROR(ERR_SIZE_ZERO); - return self; + return ERR_SIZE_ZERO; } - self = _vec_char_erase(&self, _vec_char_begin(&self), - _vec_char_end(&self) + 1); - self = _vec_char_reserve(&self, amount); + size_t status = + _vec_char_erase(ptr, _vec_char_begin(ptr), _vec_char_end(ptr) + 1); + if (status != SUCCESS) + { + return status; + } + status = _vec_char_reserve(ptr, amount); + if (status != SUCCESS) + { + return status; + } + 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 -_vec_char_insert(const _VecCharInternal* ptr, const size_t iter, const char val) +__attribute__((overloadable)) size_t +_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 || - _vec_char_is_valid(ptr) == ERR_BUF_NULL) + if (_vec_char_is_valid(ptr) == ERR_VECTOR_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) { LOGERROR(ERR_MAX_PASSED); - return self; + return ERR_MAX_PASSED; } if (iter > ptr->_internal_size) { LOGERROR(ERR_OUT_OF_BOUNDS); - return self; + return ERR_OUT_OF_BOUNDS; } - self = *ptr; - self = _vec_char_push_back(&self, 0); - for (size_t i = self._internal_size - 1; i > iter; i--) + size_t status = _vec_char_push_back(ptr, 0); + if (status != SUCCESS) { - 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 -_vec_char_insert(const _VecCharInternal* ptr, const size_t iter, - const size_t amount, const char val) +__attribute__((overloadable)) size_t +_vec_char_insert(_VecCharInternal* ptr, const size_t iter, const size_t amount, + const char val) { - _VecCharInternal self = _vec_char_create(nullptr, 0); - if (_vec_char_is_valid(ptr) != SUCCESS) + if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) { - LOGERROR(ERR_NULL); - return self; + return ERR_VECTOR_NULL; + } + if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) + { + return ERR_BUF_NULL; } if (iter > ptr->_internal_size) { LOGERROR(ERR_OUT_OF_BOUNDS); - return self; + return ERR_OUT_OF_BOUNDS; } if (amount <= 0) { LOGERROR(ERR_SIZE_ZERO); - return self; + return ERR_SIZE_ZERO; } - self = *ptr; 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 -_vec_char_insert(const _VecCharInternal* ptr, const _VecCharInternal* iterator, +__attribute__((overloadable)) size_t +_vec_char_insert(_VecCharInternal* ptr, const _VecCharInternal* iterator, const size_t iter, const size_t iter_start, const size_t iter_end) { - _VecCharInternal self = _vec_char_create(nullptr, 0); - if (_vec_char_is_valid(ptr) != SUCCESS || - _vec_char_is_valid(iterator) != SUCCESS) + if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) { - LOGERROR(ERR_NULL); - return self; + return ERR_VECTOR_NULL; } - 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); - return self; + return ERR_OUT_OF_BOUNDS; } if (ptr->_internal_arr == iterator->_internal_arr) { LOGERROR(ERR_DOUBLE_BUF); - return self; + return ERR_DOUBLE_BUF; } - self = *ptr; for (size_t i = iter_start; i < iter_end; i++) { - self = _vec_char_insert(&self, iter + (i - iter_start), - iterator->_internal_arr[i]); + size_t status = _vec_char_insert(ptr, iter + (i - iter_start), + iterator->_internal_arr[i]); + if (status != SUCCESS) + { + return status; + } } - return self; + return SUCCESS; } diff --git a/src/lib/char/char.h b/src/lib/char/char.h index bfb8cfe..60e62c7 100644 --- a/src/lib/char/char.h +++ b/src/lib/char/char.h @@ -22,16 +22,16 @@ _vec_char_is_valid(const _VecCharInternal* ptr); _VecCharInternal _vec_char_create(const char* src, const size_t size); -_VecCharInternal +size_t _vec_char_push_back(_VecCharInternal* vec, const char val); -_VecCharInternal +size_t _vec_char_pop_back(_VecCharInternal* ptr); -_VecCharInternal -_vec_char_reserve(const _VecCharInternal* ptr, const size_t max_size); +size_t +_vec_char_reserve(_VecCharInternal* ptr, const size_t max_size); -_VecCharInternal +size_t _vec_char_shrink_to_fit(_VecCharInternal* ptr); size_t @@ -49,8 +49,8 @@ _vec_char_max_size(const _VecCharInternal* ptr); void _vec_char_delete(const _VecCharInternal* ptr); -_VecCharInternal -_vec_char_clear(const _VecCharInternal* ptr); +size_t +_vec_char_clear(_VecCharInternal* ptr); char _vec_char_at(const _VecCharInternal* ptr, const size_t idx); @@ -73,38 +73,35 @@ _vec_char_back(const _VecCharInternal* ptr); size_t _vec_char_empty(const _VecCharInternal* ptr); -__attribute__((overloadable)) _VecCharInternal -_vec_char_erase(const _VecCharInternal* ptr, const size_t iter); +__attribute__((overloadable)) size_t +_vec_char_erase(_VecCharInternal* ptr, const size_t iter); -__attribute__((overloadable)) _VecCharInternal -_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); -__attribute__((overloadable)) _VecCharInternal -_vec_char_resize(const _VecCharInternal* ptr, const size_t size); +__attribute__((overloadable)) size_t +_vec_char_resize(_VecCharInternal* ptr, const size_t size); -__attribute__((overloadable)) _VecCharInternal -_vec_char_resize(const _VecCharInternal* ptr, const size_t size, - const char val); +__attribute__((overloadable)) size_t +_vec_char_resize(_VecCharInternal* ptr, const size_t size, const char val); -__attribute__((overloadable)) _VecCharInternal -_vec_char_assign(const _VecCharInternal* ptr, const _VecCharInternal* iterator, +__attribute__((overloadable)) size_t +_vec_char_assign(_VecCharInternal* ptr, const _VecCharInternal* iterator, const size_t iter_start, const size_t iter_end); -__attribute__((overloadable)) _VecCharInternal -_vec_char_assign(const _VecCharInternal* ptr, const size_t amount, +__attribute__((overloadable)) size_t +_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); -__attribute__((overloadable)) _VecCharInternal -_vec_char_insert(const _VecCharInternal* ptr, const size_t iter, - 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, +__attribute__((overloadable)) size_t +_vec_char_insert(_VecCharInternal* ptr, const _VecCharInternal* iterator, const size_t iter, const size_t iter_start, const size_t iter_end);