Use asserts to validate arguments for Str8
This commit is contained in:
parent
8e952d9bc8
commit
3689c17d09
@ -6,17 +6,14 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define STR8_BUF_ALLOC_SIZE(CAPACITY) (sizeof(Str8) + sizeof(c8) * CAPACITY)
|
||||
|
||||
Str8 *wapp_str8_alloc_buf(const Allocator *allocator, u64 capacity) {
|
||||
Str8 *str = NULL;
|
||||
assert(allocator != NULL && "allocator argument shouldn't be NULL");
|
||||
|
||||
if (!allocator) {
|
||||
goto RETURN_STR8;
|
||||
}
|
||||
|
||||
str = wapp_mem_allocator_alloc(allocator, STR8_BUF_ALLOC_SIZE(capacity));
|
||||
Str8 *str = wapp_mem_allocator_alloc(allocator, STR8_BUF_ALLOC_SIZE(capacity));
|
||||
if (!str) {
|
||||
goto RETURN_STR8;
|
||||
}
|
||||
@ -30,14 +27,10 @@ RETURN_STR8:
|
||||
}
|
||||
|
||||
Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str) {
|
||||
Str8 *output = NULL;
|
||||
|
||||
if (!allocator || !str) {
|
||||
goto RETURN_ALLOC_CSTR;
|
||||
}
|
||||
assert(allocator != NULL && str != NULL && "Arguments passed shouldn't be NULL");
|
||||
|
||||
u64 length = strlen(str);
|
||||
output = wapp_str8_alloc_buf(allocator, length * 2);
|
||||
Str8 *output = wapp_str8_alloc_buf(allocator, length * 2);
|
||||
if (!output) {
|
||||
goto RETURN_ALLOC_CSTR;
|
||||
}
|
||||
@ -50,13 +43,9 @@ RETURN_ALLOC_CSTR:
|
||||
}
|
||||
|
||||
Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str) {
|
||||
Str8 *output = NULL;
|
||||
assert(allocator != NULL && str != NULL && "Arguments passed shouldn't be NULL");
|
||||
|
||||
if (!allocator || !str) {
|
||||
goto RETURN_ALLOC_STR8;
|
||||
}
|
||||
|
||||
output = wapp_str8_alloc_buf(allocator, str->capacity);
|
||||
Str8 *output = wapp_str8_alloc_buf(allocator, str->capacity);
|
||||
if (!output) {
|
||||
goto RETURN_ALLOC_STR8;
|
||||
}
|
||||
@ -69,10 +58,9 @@ RETURN_ALLOC_STR8:
|
||||
}
|
||||
|
||||
Str8 *wapp_str8_alloc_substr(const Allocator *allocator, Str8RO *str, u64 start, u64 end) {
|
||||
assert(allocator != NULL && str != NULL && "Arguments passed shouldn't be NULL");
|
||||
|
||||
Str8 *output = NULL;
|
||||
if (!allocator || !str) {
|
||||
goto RETURN_ALLOC_SUBSTR;
|
||||
}
|
||||
|
||||
if (start >= str->size || start >= end) {
|
||||
goto RETURN_ALLOC_SUBSTR;
|
||||
@ -95,10 +83,7 @@ RETURN_ALLOC_SUBSTR:
|
||||
}
|
||||
|
||||
void wapp_str8_dealloc_buf(const Allocator *allocator, Str8 **str) {
|
||||
if (!allocator || !str || !(*str)) {
|
||||
return;
|
||||
}
|
||||
|
||||
assert(allocator != NULL && str != NULL && (*str) != NULL && "Arguments passed shouldn't be NULL");
|
||||
wapp_mem_allocator_free(allocator, (void **)str, STR8_BUF_ALLOC_SIZE((*str)->capacity));
|
||||
}
|
||||
|
||||
@ -136,11 +121,11 @@ bool wapp_str8_equal(Str8RO *s1, Str8RO *s2) {
|
||||
}
|
||||
|
||||
bool wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count) {
|
||||
if (!s1 || !s2) {
|
||||
return false;
|
||||
}
|
||||
if (!s1 || !s2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return memcmp(s1->buf, s2->buf, count) == 0;
|
||||
return memcmp(s1->buf, s2->buf, count) == 0;
|
||||
}
|
||||
|
||||
Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end) {
|
||||
@ -161,9 +146,7 @@ Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end) {
|
||||
}
|
||||
|
||||
Str8 *wapp_str8_alloc_concat(const Allocator *allocator, Str8 *dst, Str8RO *src) {
|
||||
if (!allocator || !dst || !src) {
|
||||
return NULL;
|
||||
}
|
||||
assert(allocator != NULL && dst != NULL && src != NULL && "Arguments passed shouldn't be NULL");
|
||||
|
||||
Str8 *output = NULL;
|
||||
u64 remaining = dst->capacity - dst->size;
|
||||
@ -189,9 +172,7 @@ RETURN_STR8_CONCAT:
|
||||
}
|
||||
|
||||
void wapp_str8_concat_capped(Str8 *dst, Str8RO *src) {
|
||||
if (!dst || !src) {
|
||||
return;
|
||||
}
|
||||
assert(dst != NULL && src != NULL && "Arguments passed shouldn't be NULL");
|
||||
|
||||
u64 remaining = dst->capacity - dst->size;
|
||||
u64 to_copy = remaining < src->size ? remaining : src->size;
|
||||
@ -201,9 +182,7 @@ void wapp_str8_concat_capped(Str8 *dst, Str8RO *src) {
|
||||
}
|
||||
|
||||
void wapp_str8_copy_cstr_capped(Str8 *dst, const char *src) {
|
||||
if (!dst || !src) {
|
||||
return;
|
||||
}
|
||||
assert(dst != NULL && src != NULL && "Arguments passed shouldn't be NULL");
|
||||
|
||||
u64 length = strlen(src);
|
||||
u64 to_copy = length <= dst->capacity ? length : dst->capacity;
|
||||
@ -214,9 +193,7 @@ void wapp_str8_copy_cstr_capped(Str8 *dst, const char *src) {
|
||||
}
|
||||
|
||||
void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src) {
|
||||
if (!dst || !src) {
|
||||
return;
|
||||
}
|
||||
assert(dst != NULL && src != NULL && "Arguments passed shouldn't be NULL");
|
||||
|
||||
u64 to_copy = src->size <= dst->capacity ? src->size : dst->capacity;
|
||||
|
||||
@ -226,9 +203,7 @@ void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src) {
|
||||
}
|
||||
|
||||
void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity) {
|
||||
if (!dst || !src) {
|
||||
return;
|
||||
}
|
||||
assert(dst != NULL && src != NULL && "Arguments passed shouldn't be NULL");
|
||||
|
||||
u64 to_copy = src->size < dst_capacity ? src->size : dst_capacity - 1;
|
||||
|
||||
@ -237,9 +212,7 @@ void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity) {
|
||||
}
|
||||
|
||||
void wapp_str8_format(Str8 *dst, const char *format, ...) {
|
||||
if (!dst || !format) {
|
||||
return;
|
||||
}
|
||||
assert(dst != NULL && format != NULL && "Arguments passed shouldn't be NULL");
|
||||
|
||||
va_list args1;
|
||||
va_list args2;
|
||||
@ -301,9 +274,7 @@ i64 wapp_str8_rfind(Str8RO *str, Str8RO substr) {
|
||||
}
|
||||
|
||||
Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) {
|
||||
if (!allocator || !str || !delimiter) {
|
||||
return NULL;
|
||||
}
|
||||
assert(allocator != NULL && str != NULL && delimiter != NULL && "Arguments passed shouldn't be NULL");
|
||||
|
||||
Str8List *output = wapp_mem_allocator_alloc(allocator, sizeof(Str8List));
|
||||
|
||||
@ -356,9 +327,7 @@ RETURN_STR8_SPLIT:
|
||||
}
|
||||
|
||||
Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) {
|
||||
if (!allocator || !str || !delimiter) {
|
||||
return NULL;
|
||||
}
|
||||
assert(allocator != NULL && str != NULL && delimiter != NULL && "Arguments passed shouldn't be NULL");
|
||||
|
||||
Str8List *output = wapp_mem_allocator_alloc(allocator, sizeof(Str8List));
|
||||
|
||||
@ -408,9 +377,7 @@ RETURN_STR8_SPLIT:
|
||||
}
|
||||
|
||||
Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8RO *delimiter) {
|
||||
if (!allocator || !list || !delimiter) {
|
||||
return NULL;
|
||||
}
|
||||
assert(allocator != NULL && list != NULL && delimiter != NULL && "Arguments passed shouldn't be NULL");
|
||||
|
||||
u64 capacity = wapp_str8_list_total_size(list) + (delimiter->size * (list->node_count - 1));
|
||||
Str8 *output = wapp_str8_alloc_buf(allocator, capacity * 2);
|
||||
|
Loading…
x
Reference in New Issue
Block a user