CHORE: Heavily cleaned up broken and redundant code

This commit is contained in:
2026-10-12 10:16:48 -04:00
parent b39911f57e
commit 6b7a560156
8 changed files with 582 additions and 491 deletions
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+183 -103
View File
@@ -1,27 +1,68 @@
#include "char.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils/utils.h"
/**
* @file char.c
* @brief Dynamic contiguous array specialization for raw char elements.
*
* DESIGN CONTRACT:
* - RAW BYTE CONTAINER:
* This vector manages a sequence of independent 'char' values (raw bytes).
* It is NOT a C-string container. Buffers are NEVER null-terminated ('\0').
* Passing _vec_char_data() directly to string utilities (e.g., strlen, printf
* %s) or treating it as a NUL-terminated string is undefined behavior.
*
* - ELEMENT VALIDITY & BOUNDS:
* Valid elements reside strictly in the half-open range [0, _internal_size).
* Slots in the range [_internal_size, _internal_capacity) are spare capacity
* and must be treated as uninitialized storage. Any access past
* _internal_size - 1 is out of bounds.
*
* - ERROR HANDLING:
* Functions taking an `err_t*` out-parameter require a non-NULL pointer.
* On error, appropriate bit flags (defined in utils.h) are bitwise OR-ed into
* `err->flags`, and the initial failure site is recorded in `err->origin`.
*
* - MEMORY OWNERSHIP:
* Buffers are heap-allocated via malloc/realloc and owned exclusively by
* the vector instance. Use _vec_char_delete() to release resources.
*/
__attribute__((overloadable)) size_t
__null_check(const _VecCharInternal* ptr, err_t* status)
{
unsigned int res = 0;
if (status == nullptr)
{
status->flags = ERR_ERR_T_NULL;
status->origin = "NULL_CHECK";
return ERR_ERR_T_NULL;
}
if (ptr == nullptr)
{
status->flags |= ERR_VEC_NULL;
return ERR_VEC_NULL;
}
if (ptr->_internal_arr == nullptr) status->flags |= ERR_BUF_NULL;
if (ptr->_internal_size == 0) status->flags |= ERR_SIZE_ZERO;
if (ptr->_internal_arr == nullptr)
{
status->flags |= ERR_BUF_NULL;
}
return status->flags;
}
__attribute__((overloadable)) size_t
__null_check(const char* ptr, err_t* status)
{
if (ptr == nullptr) status->flags |= ERR_BUF_NULL;
if (ptr == nullptr)
{
status->flags |= ERR_BUF_NULL;
}
return status->flags;
}
@@ -48,9 +89,14 @@ __origin_override(err_t* status, char* str)
void
_vec_char_print_vec(const _VecCharInternal* ptr, err_t* err)
{
if (__null_check(ptr, err) != 0) return;
if (__null_check(ptr, err) != 0)
{
return;
}
for (size_t i = 0; i < ptr->_internal_size; i++)
{
printf("%c ", ptr->_internal_arr[i]);
}
printf("\n");
}
@@ -73,14 +119,17 @@ _vec_char_cpy(const char* src, const size_t src_size, err_t* err)
err->flags |= status.flags;
return self;
}
self._internal_arr = malloc(src_size * sizeof(char));
__null_check(self._internal_arr, &status);
if (status.flags != 0)
if (src_size > self._internal_max_size)
{
__origin_override(err, "CPY");
err->flags |= status.flags;
free(self._internal_arr);
self._internal_arr = NULL;
err->flags |= ERR_SIZE_OVERFLOW;
return self;
}
self._internal_arr = malloc(src_size * sizeof(char));
if (self._internal_arr == nullptr)
{
__origin_override(err, "CPY");
err->flags |= ERR_ALLOC_FAIL;
return self;
}
memcpy(self._internal_arr, src, src_size * sizeof(char));
@@ -120,6 +169,7 @@ _vec_char_push_back(_VecCharInternal* ptr, const char val, err_t* err)
}
ptr->_internal_arr = res;
ptr->_internal_capacity = 1;
ptr->_internal_size = 0;
}
else if (ptr->_internal_size + 1 > ptr->_internal_capacity)
@@ -158,7 +208,13 @@ _vec_char_pop_back(_VecCharInternal* ptr, err_t* err)
return;
}
ptr->_internal_arr[ptr->_internal_size - 1] = '\0';
if (ptr->_internal_size == 0)
{
__origin_override(err, "POP_BACK");
return;
}
ptr->_internal_arr[ptr->_internal_size - 1] = 0;
ptr->_internal_size--;
}
@@ -167,33 +223,35 @@ _vec_char_reserve(_VecCharInternal* ptr, const size_t capacity, err_t* err)
{
err_t status = ERR_T_INIT;
char* res;
if (__null_check(ptr, &status) != 0)
{
if ((status.flags & ERR_VEC_NULL) != 0)
{
__origin_override(err, "RESERVE");
err->flags |= ERR_VEC_NULL;
return;
}
}
if (capacity == 0)
{
__origin_override(err, "RESERVE");
err->flags |= ERR_SIZE_ZERO;
return;
}
if (__null_check(ptr, &status) != 0)
{
if ((status.flags & ERR_BUF_NULL) != 0)
{
res = calloc(ptr->_internal_capacity, sizeof(char));
if (res == nullptr)
{
__origin_override(err, "RESERVE");
err->flags |= ERR_ALLOC_FAIL;
return;
}
}
else
if ((status.flags & ERR_BUF_NULL) != 0)
{
res = calloc(capacity, sizeof(char));
if (res == nullptr)
{
__origin_override(err, "RESERVE");
err->flags |= (status.flags ^ ERR_BUF_NULL);
err->flags |= ERR_ALLOC_FAIL;
return;
}
ptr->_internal_arr = res;
ptr->_internal_capacity = capacity;
return;
}
else
{
res = realloc(ptr->_internal_arr, capacity * sizeof(char));
@@ -203,18 +261,16 @@ _vec_char_reserve(_VecCharInternal* ptr, const size_t capacity, err_t* err)
err->flags |= ERR_ALLOC_FAIL;
return;
}
ptr->_internal_arr = res;
if (ptr->_internal_size > capacity)
{
for (size_t i = capacity; i < ptr->_internal_size; i++)
{
ptr->_internal_arr[i] = '\0';
}
memset(&ptr->_internal_arr[capacity], 0,
ptr->_internal_size - capacity);
ptr->_internal_size = capacity;
}
}
ptr->_internal_arr = res;
ptr->_internal_capacity = capacity;
ptr->_internal_max_size = capacity;
return;
}
@@ -229,39 +285,31 @@ _vec_char_shrink_to_fit(_VecCharInternal* ptr, err_t* err)
err->flags |= status.flags;
return;
}
if (ptr->_internal_size == 0)
{
return;
}
return _vec_char_reserve(ptr, ptr->_internal_size, err);
}
void
_vec_char_swap(_VecCharInternal* ptr1, _VecCharInternal* ptr2, err_t* err)
{
_VecCharInternal self = VEC_INIT;
err_t status_1 = ERR_T_INIT, status_2 = ERR_T_INIT;
if (__null_check(ptr1, &status_1) != 0 ||
__null_check(ptr2, &status_2) != 0)
if ((__null_check(ptr1, &status_1) & ERR_VEC_NULL) != 0 ||
(__null_check(ptr2, &status_2) & ERR_VEC_NULL) != 0)
{
__origin_override(err, "SWAP");
err->flags |= status_1.flags | status_2.flags;
err->flags |= ERR_VEC_NULL;
return;
}
self._internal_arr = ptr1->_internal_arr;
ptr1->_internal_arr = ptr2->_internal_arr;
ptr2->_internal_arr = self._internal_arr;
self._internal_capacity = ptr1->_internal_capacity;
self._internal_size = ptr1->_internal_size;
self._internal_max_size = ptr1->_internal_max_size;
ptr1->_internal_capacity = ptr2->_internal_capacity;
ptr1->_internal_size = ptr2->_internal_size;
ptr1->_internal_max_size = ptr2->_internal_max_size;
ptr2->_internal_capacity = self._internal_capacity;
ptr2->_internal_size = self._internal_size;
ptr2->_internal_max_size = self._internal_max_size;
_VecCharInternal self = *ptr1;
*ptr1 = *ptr2;
*ptr2 = self;
return;
}
char*
const char*
_vec_char_data(const _VecCharInternal* ptr, err_t* err)
{
err_t status = ERR_T_INIT;
@@ -274,6 +322,19 @@ _vec_char_data(const _VecCharInternal* ptr, err_t* err)
return &ptr->_internal_arr[0];
}
size_t
_vec_char_size(_VecCharInternal* ptr, err_t* err)
{
err_t status = ERR_T_INIT;
if (__null_check(ptr, &status) != 0)
{
__origin_override(err, "CAPACITY");
err->flags |= status.flags;
return 0;
}
return ptr->_internal_size;
}
size_t
_vec_char_capacity(const _VecCharInternal* ptr, err_t* err)
{
@@ -301,7 +362,7 @@ _vec_char_max_size(const _VecCharInternal* ptr, err_t* err)
}
void
_vec_char_delete(const _VecCharInternal* ptr, err_t* err)
_vec_char_delete(_VecCharInternal* ptr, err_t* err)
{
err_t status = ERR_T_INIT;
if (__null_check(ptr, &status) != 0)
@@ -310,7 +371,13 @@ _vec_char_delete(const _VecCharInternal* ptr, err_t* err)
err->flags |= status.flags;
return;
}
free((void*)ptr->_internal_arr);
if (ptr->_internal_arr != nullptr)
{
free(ptr->_internal_arr);
ptr->_internal_arr = nullptr;
}
ptr->_internal_size = 0;
ptr->_internal_capacity = 0;
}
void
@@ -325,15 +392,12 @@ _vec_char_clear(_VecCharInternal* ptr, err_t* err)
}
if (ptr->_internal_size > 0)
{
for (size_t i = 0; i < ptr->_internal_size; i++)
{
ptr->_internal_arr[i] = 0;
}
memset(ptr->_internal_arr, 0, ptr->_internal_size);
ptr->_internal_size = 0;
}
}
char
char*
_vec_char_at(const _VecCharInternal* ptr, const size_t idx, err_t* err)
{
err_t status = ERR_T_INIT;
@@ -341,16 +405,17 @@ _vec_char_at(const _VecCharInternal* ptr, const size_t idx, err_t* err)
{
__origin_override(err, "AT");
err->flags |= status.flags;
return status.flags;
return nullptr;
}
if (idx >= ptr->_internal_size)
{
fprintf(stderr,
"At Index: [%zu] is out of bounds for size: [%zu]\n",
idx, ptr->_internal_size);
return ERR_OUT_OF_BOUNDS;
printf("At Index: [%zu] is out of bounds for size: [%zu]\n",
idx, ptr->_internal_size);
__origin_override(err, "AT");
err->flags |= ERR_OUT_OF_BOUNDS;
return nullptr;
}
return ptr->_internal_arr[idx];
return &ptr->_internal_arr[idx];
}
size_t
@@ -376,7 +441,7 @@ _vec_char_end(const _VecCharInternal* ptr, err_t* err)
err->flags |= status.flags;
return status.flags;
}
return (int)(ptr->_internal_size - 1);
return (size_t)(ptr->_internal_size - 1);
}
char
@@ -389,7 +454,12 @@ _vec_char_front(const _VecCharInternal* ptr, err_t* err)
err->flags |= status.flags;
return status.flags;
}
return _vec_char_at(ptr, 0, err);
if (_vec_char_at(ptr, 0, err) != nullptr)
{
err->flags |= ERR_OUT_OF_BOUNDS;
return 0;
}
return *_vec_char_at(ptr, 0, err);
}
char
@@ -402,7 +472,12 @@ _vec_char_back(const _VecCharInternal* ptr, err_t* err)
err->flags |= status.flags;
return status.flags;
}
return _vec_char_at(ptr, ptr->_internal_size - 1, err);
if (_vec_char_at(ptr, 0, err) != nullptr)
{
err->flags |= ERR_OUT_OF_BOUNDS;
return 0;
}
return *_vec_char_at(ptr, ptr->_internal_size - 1, err);
}
size_t
@@ -411,15 +486,14 @@ _vec_char_empty(const _VecCharInternal* ptr, err_t* err)
err_t status = ERR_T_INIT;
if (__null_check(ptr, &status) != 0)
{
if ((status.flags & ERR_SIZE_ZERO) != 0)
{
err->flags ^= ERR_SIZE_ZERO;
return SUCCESS;
}
__origin_override(err, "EMPTY");
err->flags |= status.flags;
return status.flags;
}
if (ptr->_internal_size == 0)
{
return SUCCESS;
}
return status.flags;
}
@@ -464,11 +538,8 @@ _vec_char_erase(_VecCharInternal* ptr, const size_t iter_start,
size_t diff = iter_end - iter_start;
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++)
{
ptr->_internal_arr[i] = 0;
}
memset(&ptr->_internal_arr[ptr->_internal_size - diff], 0,
sizeof(char) * ptr->_internal_size);
ptr->_internal_size -= diff;
return;
}
@@ -478,10 +549,10 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, err_t* err)
{
err_t status = ERR_T_INIT;
__null_check(ptr, &status);
if ((status.flags & (ERR_BUF_NULL | ERR_VEC_NULL)) != 0)
if ((status.flags & ERR_VEC_NULL) != 0)
{
__origin_override(err, "RESIZE");
err->flags |= ERR_BUF_NULL | ERR_VEC_NULL;
err->flags |= ERR_VEC_NULL;
return;
}
@@ -499,7 +570,7 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, err_t* err)
return;
}
else if ((status.flags & ERR_SIZE_ZERO) != 0)
else if ((status.flags & ERR_BUF_NULL) != 0 || ptr->_internal_size == 0)
{
size_t max = ptr->_internal_max_size;
err_t verr = ERR_T_INIT;
@@ -518,7 +589,7 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, err_t* err)
}
for (size_t i = 0; i < size; i++)
{
ptr->_internal_arr[i] = '\0';
ptr->_internal_arr[i] = 0;
}
ptr->_internal_max_size = max;
ptr->_internal_size = size;
@@ -554,17 +625,12 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, err_t* err)
}
if (size < ptr->_internal_size)
{
for (size_t i = size; i < ptr->_internal_size; i++)
{
ptr->_internal_arr[i] = '\0';
}
memset(&ptr->_internal_arr[size], 0, ptr->_internal_size);
}
if (ptr->_internal_size < size && size <= ptr->_internal_capacity)
{
for (size_t i = ptr->_internal_size; i < size; i++)
{
ptr->_internal_arr[i] = '\0';
}
memset(&ptr->_internal_arr[ptr->_internal_size], 0,
ptr->_internal_arr[size]);
}
ptr->_internal_size = size;
return;
@@ -576,10 +642,10 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, const char val,
{
err_t status = ERR_T_INIT;
__null_check(ptr, &status);
if ((status.flags & (ERR_BUF_NULL | ERR_VEC_NULL)) != 0)
if ((status.flags & ERR_VEC_NULL) != 0)
{
__origin_override(err, "RESIZE");
err->flags |= ERR_BUF_NULL | ERR_VEC_NULL;
err->flags |= ERR_VEC_NULL;
return;
}
if (ptr->_internal_size == size)
@@ -596,7 +662,7 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, const char val,
return;
}
else if ((status.flags & ERR_SIZE_ZERO) != 0)
else if ((status.flags & ERR_BUF_NULL) != 0 || ptr->_internal_size == 0)
{
size_t max = ptr->_internal_max_size;
err_t verr = ERR_T_INIT;
@@ -645,17 +711,12 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, const char val,
}
if (size < ptr->_internal_size)
{
for (size_t i = size; i < ptr->_internal_size; i++)
{
ptr->_internal_arr[i] = val;
}
memset(&ptr->_internal_arr[size], val, ptr->_internal_size);
}
if (ptr->_internal_size < size && size <= ptr->_internal_capacity)
{
for (size_t i = ptr->_internal_size; i < size; i++)
{
ptr->_internal_arr[i] = val;
}
memset(&ptr->_internal_arr[ptr->_internal_size], val,
ptr->_internal_arr[size]);
}
ptr->_internal_size = size;
return;
@@ -702,7 +763,12 @@ _vec_char_assign(_VecCharInternal* ptr, const _VecCharInternal* iterator,
}
for (size_t i = iter_start; i < iter_end; i++)
{
_vec_char_push_back(ptr, _vec_char_at(iterator, i, &status),
if (_vec_char_at(iterator, i, &status) != nullptr)
{
err->flags |= ERR_OUT_OF_BOUNDS;
return;
}
_vec_char_push_back(ptr, *_vec_char_at(iterator, i, &status),
&status);
if (status.flags != 0)
{
@@ -764,12 +830,27 @@ _vec_char_insert(_VecCharInternal* ptr, const size_t iter, const char val,
{
err_t status = ERR_T_INIT;
__null_check(ptr, &status);
if ((status.flags & (ERR_VEC_NULL | ERR_BUF_NULL)) != 0)
if ((status.flags & ERR_VEC_NULL) != 0)
{
__origin_override(err, "INSERT");
err->flags |= status.flags;
return;
}
if ((status.flags & ERR_BUF_NULL) != 0)
{
char* res = malloc(1 * sizeof(char));
if (res == nullptr)
{
__origin_override(err, "INSERT");
err->flags |= ERR_BUF_NULL;
return;
}
ptr->_internal_arr = res;
ptr->_internal_capacity = 1;
ptr->_internal_arr[0] = val;
ptr->_internal_size = 1;
return;
}
if (ptr->_internal_max_size < ptr->_internal_size + 1)
{
__origin_override(err, "INSERT");
@@ -843,7 +924,6 @@ _vec_char_insert(_VecCharInternal* ptr, const _VecCharInternal* iterator,
__null_check(ptr, &status);
__null_check(iterator, &status);
if ((status.flags & (ERR_VEC_NULL | ERR_BUF_NULL)) != 0)
{
__origin_override(err, "INSERT");
err->flags |= status.flags;
+8 -8
View File
@@ -1,13 +1,10 @@
#ifndef CHAR_H
#define CHAR_H
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utils/utils.h>
// Will add DOCS later
typedef struct
{
char* _internal_arr;
@@ -37,9 +34,12 @@ _vec_char_shrink_to_fit(_VecCharInternal* ptr, err_t* err);
void
_vec_char_swap(_VecCharInternal* ptr, _VecCharInternal* ptr_other, err_t* err);
char*
const char*
_vec_char_data(const _VecCharInternal* ptr, err_t* err);
size_t
_vec_char_size(_VecCharInternal* ptr, err_t* err);
size_t
_vec_char_capacity(const _VecCharInternal* ptr, err_t* err);
@@ -47,12 +47,12 @@ size_t
_vec_char_max_size(const _VecCharInternal* ptr, err_t* err);
void
_vec_char_delete(const _VecCharInternal* ptr, err_t* err);
_vec_char_delete(_VecCharInternal* ptr, err_t* err);
void
_vec_char_clear(_VecCharInternal* ptr, err_t* err);
char
char*
_vec_char_at(const _VecCharInternal* ptr, const size_t idx, err_t* err);
size_t
+3 -9
View File
@@ -22,7 +22,8 @@ static const size_t VEC_MAX = SIZE_MAX / sizeof(int);
"Max :- Vector Max value is overloaded") \
X(ERR_ORIGIN_UNALTERABLE, (1u << 7), \
"Origin :- Origin is non-null and cannot be overridden") \
X(ERR_STR_NULL, (1u << 8), "String :- String is null value")
X(ERR_STR_NULL, (1u << 8), "String :- String is null value") \
X(ERR_ERR_T_NULL, (1u << 9), "ERR_T :- Passed error is null")
typedef enum
{
@@ -56,18 +57,11 @@ vec_get_error_desc(ERROR_CODES err);
void
vec_print_errs(const err_t err);
// void
// _vec_update_origin(err_t* err, char* str);
void
print_bits(unsigned int num);
#define LOG(msg) printf("LOG: %s\n", msg) /**< Print log message*/
#define DEBUG(msg) printf("DEBUB: %s\n", msg) /**< Print debug message*/
#define LOGERROR(error_code) \
printf("ERROR: %s\n", \
error_messages[error_code]) /**< Print log error message*/
#define DEBUG(msg) printf("DEBUG: %s\n", msg) /**< Print debug message*/
#define find_len(arr) sizeof(arr) / sizeof(arr[0])
#endif
-58
View File
@@ -12,13 +12,11 @@
* String 32 Bits with suffix of 's'
*/
// #include <libvec.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib/char/char.h"
#include "lib/utils/utils.h"
void
@@ -39,67 +37,11 @@ main()
char res[4] = { 'a', 'b', 'c', 'd' };
err_t status = ERR_T_INIT, err = ERR_T_INIT;
status.flags |= ERR_BUF_NULL | ERR_VEC_NULL | ERR_STR_NULL;
// _VecCharInternal v1 = VEC_INIT;
// _vec_char_push_back(&v1, 'c', &status);
// _vec_char_print_vec(&v1, &status);
unsigned int mask = 0;
mask = ERR_BUF_NULL ^ status.flags;
print_bits(mask);
print_bits(status.flags);
// if (status.origin)
// {
// printf("Error started in: %s\n", status.origin);
// }
// printf("%s: %s", err.origin, vec_print_errs(err));
// vec_print_errs(status);
// printf("%u\n", status.err);
// _vec_update_origin(&err, "Test");
// printf("%s", err.origin);
// for (int i = 0; i < VEC_FAULT_BITS; i++)
// {
// print_bits(mark);
// mark <<= 1;
// }
// const char* str = vec_get_error_desc(ERR_ALLOC_FAIL);
// const unsigned int error = ERR_BUF_NULL;
// if (!(ERR_ALLOC_FAIL & SUCCESS))
// {
// printf("Hello\n");
// }
// printf("%s", str);
// char ar1[] = { '1', '2', '3', '4' };
// char ar2[] = { 'a', 'b', 'c', 'd' };
// Vec8_t v1 = create(ar2, find_len(ar2));
// v1 = reserve(&v1, 9);
// print_vec(&v1);
// v1 = insert(&v1, 1, 'x');
// v1 = resize(&v1, 12);
// print_vec(&v1);
// v2 = resize(&v2, 3);
// print_vec(&v2);
// if (swap(&v1, &v2))
// {
// print_vec(&v1);
// print_vec(&v2);
// }
// Vec8_t v1 = create(nullptr, 0);
// v1 = reserve(&v1, 10);
// v1 = pop_back(&v1);
// for (int i = 0; i < 10; i++)
// {
// v1 = push_back(&v1, 'a');
// }
// print_vec(&v1);
// printf("SIZE: %zu\n", v1.size);
// v1 = push_back(&v1, 'a');
// print_vec(&v1);
// printf("SIZE: %zu\n", v1.size);
// delete(&v1);
// delete(&v2);
return 0;
}