rewritten Array and List, changed str.size -> .len

This commit is contained in:
Timerix 2025-11-25 14:13:25 +05:00
parent b557881168
commit 82bd234d08
48 changed files with 389 additions and 227 deletions

View File

@ -1,57 +1,62 @@
#pragma once #pragma once
#include "../std.h" #include "../std.h"
#define Array(T) Array_ /*
Pointer and length.
Can be initialized with {0}.
*/
typedef struct Array_ { #define Array(T) Array_##T
void* data;
u32 size;
} Array_;
/// creates Array_ from self const array /// create Array(T) from array initializer list ({ a, b, c })
#define ARRAY(T, A...) Array_construct_size(((T[])A), sizeof((T[])A)) #define ARRAY(T, A...) ((Array(T)){ .data = ((T[])A), .len = sizeof((T[])A) / sizeof(T) })
#define Array_construct(DATA, T, COUNT) Array_construct_size(DATA, (COUNT) * sizeof(T)) #define Array_declare(T) \
#define Array_construct_size(DATA, LEN) ((Array_){ .data = (DATA), .size = (LEN) }) typedef struct Array(T) { \
T* data; \
u32 len; \
} Array(T); \
\
static inline Array(T) Array_##T##_construct(T* data, u32 len) { \
return (Array(T)){ .data = data, .len = len }; \
} \
\
static inline Array(T) Array_##T##_alloc(u32 len){ \
if(len == 0) \
return Array_##T##_construct(NULL, 0); \
return Array_##T##_construct(malloc(len * sizeof(T)), len); \
} \
\
static inline void Array_##T##_realloc(Array(T)* self, u32 new_len){ \
self->data = (T*)realloc(self->data, new_len * sizeof(T)); \
self->len = new_len; \
} \
\
static inline Array(T) Array_##T##_copy(const Array(T) src){ \
Array(T) copy = Array_##T##_alloc(src.len); \
if(copy.data != NULL) \
memcpy(copy.data, src.data, src.len * sizeof(T)); \
return copy; \
} \
\
static inline void Array_##T##_destroy(Array(T)* self){ \
if(!self) \
return; \
free(self->data); \
} \
\
static inline void Array_##T##_memset(Array(T)* self, u32 value){ \
memset(self->data, value, self->len * sizeof(T)); \
} \
\
/* @return self[0..i-1] */ \
static inline Array(T) Array_##T##_sliceTo(const Array(T) src, u32 i){ \
return Array_##T##_construct(src.data, i); \
} \
\
/* @return self[i...] */ \
static inline Array(T) Array_##T##_sliceFrom(const Array(T) src, u32 i){ \
return Array_##T##_construct(src.data + i, src.len - i); \
} \
#define Array_null Array_construct_size(NULL, 0) #define struct_castTo_Array_u8(STRUCT_PTR) Array_u8_construct((void*)(STRUCT_PTR), sizeof(*STRUCT_PTR))
#define Array_alloc(T, COUNT) Array_alloc_size((COUNT) * sizeof(T))
static inline Array_ Array_alloc_size(u32 size){
if(size == 0)
return Array_null;
return Array_construct_size(malloc(size), size);
}
#define Array_realloc(SELF, T, COUNT) Array_realloc_size(SELF, (COUNT) * sizeof(T))
static inline void Array_realloc_size(Array_* self, u32 new_size){
self->data = realloc(self->data, new_size);
self->size = new_size;
}
static inline Array_ Array_copy(const Array_ self){
Array_ copy = Array_alloc_size(self.size);
if(copy.data != NULL)
memcpy(copy.data, self.data, self.size);
return copy;
}
static inline void Array_free(Array_ self){
free(self.data);
}
#define Array_len(SELF, T) (SELF.size / sizeof(T))
#define Array_memset(SELF, VAL) memset(SELF.data, VAL, SELF.size)
#define struct_castTo_Array(STRUCT_PTR) Array_construct_size((STRUCT_PTR), sizeof(*STRUCT_PTR))
///@return self[0..i-1]
static inline Array(u8) Array_sliceTo(const Array(u8) self, u32 i){
return Array_construct_size(self.data, i);
}
///@return self[i...]
static inline Array(u8) Array_sliceFrom(const Array(u8) self, u32 i){
return Array_construct_size((u8*)self.data + i, self.size - i);
}

View File

@ -0,0 +1,2 @@
#pragma once
Array_declare(char);

View File

@ -0,0 +1,2 @@
#pragma once
Array_declare(f32);

View File

@ -0,0 +1,2 @@
#pragma once
Array_declare(f64);

View File

@ -0,0 +1,2 @@
#pragma once
Array_declare(i16);

View File

@ -0,0 +1,2 @@
#pragma once
Array_declare(i32);

View File

@ -0,0 +1,2 @@
#pragma once
Array_declare(i64);

View File

@ -0,0 +1,2 @@
#pragma once
Array_declare(i8);

View File

@ -0,0 +1,2 @@
#pragma once
Array_declare(u16);

View File

@ -0,0 +1,2 @@
#pragma once
Array_declare(u32);

View File

@ -0,0 +1,2 @@
#pragma once
Array_declare(u64);

View File

@ -0,0 +1,2 @@
#pragma once
Array_declare(u8);

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "../std.h" #include "../std.h"
#include "../string/str.h" #include "../string/str.h"
#include "Array.h"
#include "List.h" #include "List.h"
typedef struct HashMapKeyHash { typedef struct HashMapKeyHash {
@ -9,9 +8,11 @@ typedef struct HashMapKeyHash {
u32 hash; u32 hash;
} HashMapKeyHash; } HashMapKeyHash;
List_declare(HashMapKeyHash);
typedef struct HashMapBucket { typedef struct HashMapBucket {
List(HashMapKeyHash) key_hash_list; List(HashMapKeyHash) key_hash_list;
List(T) value_list; List_ value_list;
} HashMapBucket; } HashMapBucket;
#define HashMap(T) HashMap_ #define HashMap(T) HashMap_

View File

@ -2,7 +2,8 @@
#include "../std.h" #include "../std.h"
/* /*
Doubly linked list Doubly linked list.
Can be initialized with {0}.
*/ */
#define LLNode(T) LLNode_##T #define LLNode(T) LLNode_##T

View File

@ -0,0 +1,2 @@
#pragma once
LList_declare(char);

View File

@ -0,0 +1,2 @@
#pragma once
LList_declare(f32);

View File

@ -0,0 +1,2 @@
#pragma once
LList_declare(f64);

View File

@ -0,0 +1,2 @@
#pragma once
LList_declare(i16);

View File

@ -0,0 +1,2 @@
#pragma once
LList_declare(i32);

View File

@ -0,0 +1,2 @@
#pragma once
LList_declare(i64);

View File

@ -0,0 +1,2 @@
#pragma once
LList_declare(i8);

View File

@ -0,0 +1,2 @@
#pragma once
LList_declare(u16);

View File

@ -0,0 +1,2 @@
#pragma once
LList_declare(u32);

View File

@ -0,0 +1,2 @@
#pragma once
LList_declare(u64);

View File

@ -0,0 +1,2 @@
#pragma once
LList_declare(u8);

View File

@ -1,44 +1,89 @@
#pragma once #pragma once
#include "../std.h" #include "../std.h"
#define List(T) List_ /*
Dynamic array.
CAN NOT be initialized with {0}.
*/
#define List(T) List_##T
#define List_declare(T) \
typedef struct List(T) { \
T* data; \
u32 len; \
u32 capacity; \
u32 elem_t_size; \
} List(T); \
\
static inline List(T) List_##T##_construct(T* data_ptr, u32 occupied_len, u32 capacity) { \
return (List(T)){ \
.data = data_ptr, \
.len = occupied_len, \
.capacity = capacity, \
.elem_t_size = sizeof(T) \
}; \
} \
\
static inline List(T) List_##T##_alloc(u32 initial_capacity) { \
List_ l = _List_alloc(initial_capacity, sizeof(T)); \
return *(List(T)*)(void*)&l; \
} \
\
static inline List(T) List_##T##_copy(const List(T)* src) { \
List_ l = _List_copy((void*)src); \
return *(List(T)*)(void*)&l; \
} \
\
static inline void List_##T##_destroy(List(T)* self) { _List_destroy((void*)self); } \
\
static inline void List_##T##_push(List(T)* self, T value) { \
T* empty_cell = (T*)(_List_expand((void*)self, 1)); \
*empty_cell = value; \
} \
\
static inline void List_##T##_pushMany(List(T)* self, T* values_ptr, u32 len) { \
_List_push((void*)self, values_ptr, len); \
} \
\
/* alloc bigger buffer if size + len_to_add won't fit in current */ \
static inline void List_##T##_increaseCapacity(List(T)* self, u32 len_to_add){ \
_List_increaseCapacity((void*)self, len_to_add); \
} \
\
static inline bool List_##T##_tryRemoveAt(List(T)* self, u32 i, u32 remove_len) ATTRIBUTE_WARN_UNUSED_RESULT; \
static inline bool List_##T##_tryRemoveAt(List(T)* self, u32 i, u32 remove_len) { \
return _List_tryRemoveAt((void*)self, i, remove_len); \
} \
typedef struct List_ { typedef struct List_ {
void* data; void* data;
u32 size; u32 len;
u32 allocated_size; u32 capacity;
u32 elem_t_size;
} List_; } List_;
#define List_construct(T, DATA_PTR, OCCUPIED_COUNT, ALLOCATED_COUNT) \ static inline List_ _List_construct(void* data_ptr, u32 len, u32 capacity, u32 elem_t_size) {
List_construct_size(DATA_PTR, (OCCUPIED_COUNT) * sizeof(T), (ALLOCATED_COUNT) * sizeof(T)) return (List_){
.data = data_ptr,
static inline List_ List_construct_size(void* data_ptr, u32 occupied_size, u32 allocated_size) { .len = len,
return (List_){ .data = data_ptr, .size = occupied_size, .allocated_size = allocated_size }; .capacity = capacity,
.elem_t_size = elem_t_size
};
} }
#define List_null List_construct_size(NULL, 0, 0)
#define List_alloc(T, INITIAL_COUNT) List_alloc_size((INITIAL_COUNT) * sizeof(T)) static inline void _List_destroy(List_* self){
List_ List_alloc_size(u32 initial_size);
static inline void List_destroy(List_* self){
if(!self) if(!self)
return; return;
free(self->data); free(self->data);
} }
List_ List_copy(List_ src); List_ _List_alloc(u32 initial_capacity, u32 elem_t_size);
List_ _List_copy(const List_* src);
// alloc bigger buffer if size + size_to_add won't fit in current /* alloc bigger buffer if size + len_to_add won't fit in current */
void List_increaseCapacity_size(List_* self, u32 size_to_add); void _List_increaseCapacity(List_* self, u32 len_to_add);
void* List_expand_size(List_* self, u32 size_to_add); void* _List_expand(List_* self, u32 len_to_add);
#define List_push(SELF, T, VALUE) *(T*)(List_expand_size(SELF, sizeof(T))) = VALUE void _List_push(List_* self, void* values, u32 len);
#define List_pushMany(SELF, T, VALUES_PTR, COUNT) List_push_size(SELF, VALUES_PTR, (COUNT) * sizeof(T)) bool _List_tryRemoveAt(List_* self, u32 i, u32 remove_len) ATTRIBUTE_WARN_UNUSED_RESULT;
void List_push_size(List_* self, void* values, u32 size);
#define List_removeAt(SELF, T, I, COUNT) List_removeAt_size(SELF, (I)*sizeof(T), (COUNT) * sizeof(T))
bool List_removeAt_size(List_* self, u32 i, u32 remove_size);
#define List_len(SELF, T) (SELF.size / sizeof(T))
#define List_index(SELF, T, I) (((T*)SELF.data)[I])
#define List_castTo_Array(SELF) Array_construct_size(SELF.data, SELF.size)

View File

@ -0,0 +1,2 @@
#pragma once
List_declare(char);

View File

@ -0,0 +1,2 @@
#pragma once
List_declare(f32);

View File

@ -0,0 +1,2 @@
#pragma once
List_declare(f64);

View File

@ -0,0 +1,2 @@
#pragma once
List_declare(i16);

View File

@ -0,0 +1,2 @@
#pragma once
List_declare(i32);

View File

@ -0,0 +1,2 @@
#pragma once
List_declare(i64);

View File

@ -0,0 +1,2 @@
#pragma once
List_declare(i8);

View File

@ -0,0 +1,2 @@
#pragma once
List_declare(u16);

View File

@ -0,0 +1,2 @@
#pragma once
List_declare(u32);

View File

@ -0,0 +1,2 @@
#pragma once
List_declare(u64);

View File

@ -0,0 +1,2 @@
#pragma once
List_declare(u8);

View File

@ -9,12 +9,15 @@ typedef struct ErrorCallPos {
cstr file; cstr file;
cstr func; cstr func;
} ErrorCallPos; } ErrorCallPos;
#define ErrorCallPos_here() (ErrorCallPos){\ #define ErrorCallPos_here() (ErrorCallPos){\
.line = __LINE__,\ .line = __LINE__,\
.file = __FILE__,\ .file = __FILE__,\
.func = __func__\ .func = __func__\
} }
List_declare(ErrorCallPos);
typedef struct Error { typedef struct Error {
str msg; str msg;
bool is_msg_on_heap; bool is_msg_on_heap;

View File

@ -4,6 +4,7 @@
#include "errors.h" #include "errors.h"
#include "string/str.h" #include "string/str.h"
#include "collections/Array.h" #include "collections/Array.h"
#include "collections/Array_impl/Array_u8.h"
#if !defined(TLIBC_FS_USE_WINDOWS_H) #if !defined(TLIBC_FS_USE_WINDOWS_H)
#if defined(_WIN64) || defined(_WIN32) #if defined(_WIN64) || defined(_WIN32)
@ -93,7 +94,7 @@ static inline Result(void) file_writeBytes(FILE* f, const void* src, u64 size){
} }
static inline Result(void) file_writeBytesArray(FILE* f, Array(u8) src){ static inline Result(void) file_writeBytesArray(FILE* f, Array(u8) src){
return file_writeStructs(f, src.data, src.size, 1); return file_writeStructs(f, src.data, src.len, 1);
} }
@ -112,10 +113,10 @@ static inline Result(u64) file_readBytes(FILE* f, void* dst, u64 max_count){
return file_readStructs(f, dst, 1, max_count); return file_readStructs(f, dst, 1, max_count);
} }
/// @param dst array where .size is the maximum number of bytes to read /// @param dst array where .len is the maximum number of bytes to read
/// @return number of bytes that were read (<=max_count) /// @return number of bytes that were read (<=max_count)
static inline Result(u64) file_readBytesArray(FILE* f, Array(u8) dst){ static inline Result(u64) file_readBytesArray(FILE* f, Array(u8) dst){
return file_readStructs(f, dst.data, 1, dst.size); return file_readStructs(f, dst.data, 1, dst.len);
} }
@ -127,9 +128,9 @@ static inline Result(void) file_readBytesExactly(FILE* f, void* dst, u64 exact_c
return file_readStructsExactly(f, dst, 1, exact_count); return file_readStructsExactly(f, dst, 1, exact_count);
} }
/// @param dst array where .size is the exact number of bytes to read /// @param dst array where .len is the exact number of bytes to read
static inline Result(void) file_readBytesArrayExactly(FILE* f, Array(u8) dst){ static inline Result(void) file_readBytesArrayExactly(FILE* f, Array(u8) dst){
return file_readStructsExactly(f, dst.data, 1, dst.size); return file_readStructsExactly(f, dst.data, 1, dst.len);
} }
/// @brief allocates array of size equal `file_getSize()` and reads whole file /// @brief allocates array of size equal `file_getSize()` and reads whole file

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "../collections/List.h" #include "../collections/List.h"
#include "../collections/Array.h" #include "../collections/List_impl/List_char.h"
#include "str.h" #include "str.h"
typedef struct StringBuilder { typedef struct StringBuilder {
@ -9,17 +9,17 @@ typedef struct StringBuilder {
} StringBuilder; } StringBuilder;
static inline StringBuilder StringBuilder_alloc(u32 initial_size) { static inline StringBuilder StringBuilder_alloc(u32 initial_size) {
return (StringBuilder){ .buffer = List_alloc_size(initial_size) }; return (StringBuilder){ .buffer = List_char_alloc(initial_size) };
} }
void StringBuilder_destroy(StringBuilder* b); void StringBuilder_destroy(StringBuilder* b);
static inline StringBuilder StringBuilder_copy(const StringBuilder* b){ static inline StringBuilder StringBuilder_copy(const StringBuilder* b){
return (StringBuilder) { .buffer = List_copy(b->buffer) }; return (StringBuilder) { .buffer = List_char_copy(&b->buffer) };
} }
// alloc bigger buffer if size + size_to_add won't fit in current // alloc bigger buffer if size + len_to_add won't fit in current
static inline void StringBuilder_increaseCapacity(StringBuilder* b, u32 size_to_add){ static inline void StringBuilder_increaseCapacity(StringBuilder* b, u32 len_to_add){
List_increaseCapacity_size(&b->buffer, size_to_add); List_char_increaseCapacity(&b->buffer, len_to_add);
} }

View File

@ -4,10 +4,12 @@
#include "char.h" #include "char.h"
#include "cstr.h" #include "cstr.h"
#include "../collections/Array.h" #include "../collections/Array.h"
#include "../collections/Array_impl/Array_char.h"
#include "../collections/Array_impl/Array_u8.h"
typedef struct str { typedef struct str {
char* data; char* data;
u32 size; // size of data in bytes without \0 u32 len; // size of data in bytes without \0
bool isZeroTerminated; bool isZeroTerminated;
} str; } str;
@ -16,7 +18,7 @@ typedef struct str {
/// creates str from a string literal /// creates str from a string literal
#define STR(LITERAL) str_construct(LITERAL, ARRAY_LEN(LITERAL) - 1, true) #define STR(LITERAL) str_construct(LITERAL, ARRAY_LEN(LITERAL) - 1, true)
#define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .size = LEN, .isZeroTerminated = ZERO_TERMINATED }) #define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .len = LEN, .isZeroTerminated = ZERO_TERMINATED })
static inline str str_from_cstr(cstr s_ptr){ static inline str str_from_cstr(cstr s_ptr){
return str_construct((void*)s_ptr, strlen(s_ptr), true); return str_construct((void*)s_ptr, strlen(s_ptr), true);
@ -26,12 +28,18 @@ static inline void str_free(str s){
free(s.data); free(s.data);
} }
static inline Array_ str_castTo_Array(str s) { static inline Array(char) str_castTo_Array_char(str s) {
return Array_construct_size(s.data, s.size); return Array_char_construct(s.data, s.len);
}
static inline Array(u8) str_castTo_Array_u8(str s) {
return Array_u8_construct((void*)s.data, s.len);
} }
static inline str Array_castTo_str(Array_ a, bool isZeroTerminated) { static inline str Array_char_castTo_str(Array(char) a, bool isZeroTerminated) {
return str_construct(a.data, a.size, isZeroTerminated); return str_construct(a.data, a.len, isZeroTerminated);
}
static inline str Array_u8_castTo_str(Array(u8) a, bool isZeroTerminated) {
return str_construct((void*)a.data, a.len, isZeroTerminated);
} }
static const str str_null = str_construct(NULL, 0, 0); static const str str_null = str_construct(NULL, 0, 0);
@ -74,5 +82,5 @@ static inline str str_sliceBefore(str s, u32 n){
///@return s[n...] ///@return s[n...]
static inline str str_sliceAfter(str s, u32 n){ static inline str str_sliceAfter(str s, u32 n){
return str_construct(s.data + n, s.size - n, s.isZeroTerminated); return str_construct(s.data + n, s.len - n, s.isZeroTerminated);
} }

View File

@ -1,4 +1,6 @@
#include "tlibc/collections/HashMap.h" #include "tlibc/collections/HashMap.h"
#include "tlibc/collections/Array.h"
#include "tlibc/collections/Array_impl/Array_u32.h"
#include <assert.h> #include <assert.h>
//TODO: sort bucket keys for binary search //TODO: sort bucket keys for binary search
@ -9,9 +11,14 @@
static const Array(u32) __HashMap_heights = ARRAY(u32, { static const Array(u32) __HashMap_heights = ARRAY(u32, {
0, 17, 31, 61, 127, 257, 521, 1021, 2053, 4099, 8191, 16381, 0, 17, 31, 61, 127, 257, 521, 1021, 2053, 4099, 8191, 16381,
32771, 65521, 131071, 262147, 524287, 1048583, 2097169, 4194319, 32771, 65521, 131071, 262147, 524287, 1048583, 2097169, 4194319,
8388617, 16777213, 33554467, 67108859, 134217757, 268435493 8388617, 16777213, 33554467, 67108859, 134217757, 268435493,
536871001, 1073742037, 2147484061
}); });
void HashMapBucket_construct(HashMapBucket* self, u32 value_t_size){
self->key_hash_list = List_HashMapKeyHash_construct(NULL, 0, 0);
self->value_list = _List_construct(NULL, 0, 0, value_t_size);
}
void HashMap_construct_size(HashMap_* self, u32 value_t_size, Destructor_t NULLABLE(value_destructor)){ void HashMap_construct_size(HashMap_* self, u32 value_t_size, Destructor_t NULLABLE(value_destructor)){
self->value_t_size = value_t_size; self->value_t_size = value_t_size;
@ -27,9 +34,9 @@ void HashMap_destroy(HashMap_* self){
for(u32 i = 0; i < self->height; i++){ for(u32 i = 0; i < self->height; i++){
HashMapBucket* bu = &self->table[i]; HashMapBucket* bu = &self->table[i];
u32 len = List_len(bu->key_hash_list, HashMapKeyHash);
// free key strings // free key strings
u32 len = bu->key_hash_list.len;
for(u32 j = 0; j < len; j++){ for(u32 j = 0; j < len; j++){
HashMapKeyHash* kh = (HashMapKeyHash*)bu->key_hash_list.data + j; HashMapKeyHash* kh = (HashMapKeyHash*)bu->key_hash_list.data + j;
free(kh->key.data); free(kh->key.data);
@ -38,7 +45,7 @@ void HashMap_destroy(HashMap_* self){
// destroy values // destroy values
if(self->value_destructor){ if(self->value_destructor){
u8* value_ptr = (u8*)bu->value_list.data; u8* value_ptr = (u8*)bu->value_list.data;
u8* end = value_ptr + bu->value_list.size; u8* end = value_ptr + bu->value_list.len * bu->value_list.elem_t_size;
while(value_ptr < end){ while(value_ptr < end){
self->value_destructor(value_ptr); self->value_destructor(value_ptr);
value_ptr += self->value_t_size; value_ptr += self->value_t_size;
@ -68,7 +75,8 @@ static BucketAndIndex __HashMap_search(const HashMap_* self, const str key, u32
BucketAndIndex r; BucketAndIndex r;
r.bu = &self->table[hash % self->height]; r.bu = &self->table[hash % self->height];
for(r.i = 0; r.i < (i32)List_len(r.bu->key_hash_list, HashMapKeyHash); r.i++){ i32 len = r.bu->key_hash_list.len;
for(r.i = 0; r.i < len; r.i++){
HashMapKeyHash* kh = (HashMapKeyHash*)r.bu->key_hash_list.data + r.i; HashMapKeyHash* kh = (HashMapKeyHash*)r.bu->key_hash_list.data + r.i;
if(kh->hash == hash && str_equals(kh->key, key)){ if(kh->hash == hash && str_equals(kh->key, key)){
return r; return r;
@ -91,29 +99,33 @@ void* HashMap_tryGetPtr(const HashMap_* self, const str key){
static void __HashMap_expand(HashMap_* self){ static void __HashMap_expand(HashMap_* self){
u32 height_expanded_n = self->height_n + 1; u32 height_expanded_n = self->height_n + 1;
assert(height_expanded_n < Array_len(__HashMap_heights, u32) && "HashMap IS FULL! Fix your code."); assert(height_expanded_n < __HashMap_heights.len && "HashMap IS FULL! Fix your code.");
// alloc new HashMapBucket array // alloc new HashMapBucket array
u32 height_expanded = ((u32*)__HashMap_heights.data)[height_expanded_n]; u32 height_expanded = __HashMap_heights.data[height_expanded_n];
u32 table_expanded_size = height_expanded * sizeof(HashMapBucket); u32 table_expanded_size = height_expanded * sizeof(HashMapBucket);
HashMapBucket* table_expanded = (HashMapBucket*)malloc(table_expanded_size); HashMapBucket* table_expanded = (HashMapBucket*)malloc(table_expanded_size);
memset(table_expanded, 0, table_expanded_size); // init buckets
for(u32 i = 0; i < height_expanded; i++){
HashMapBucket_construct(table_expanded + i, self->value_t_size);
}
// copy values from old buckets to new // copy values from old buckets to new
for(u32 i = 0; i < self->height; i++){ for(u32 i = 0; i < self->height; i++){
HashMapBucket* old_bucket = &self->table[i]; HashMapBucket* old_bucket = &self->table[i];
u32 len = List_len(old_bucket->key_hash_list, HashMapKeyHash);
u32 len = old_bucket->key_hash_list.len;
for(u32 j = 0; j < len; j++){ for(u32 j = 0; j < len; j++){
HashMapKeyHash kh = ((HashMapKeyHash*)old_bucket->key_hash_list.data)[j]; HashMapKeyHash* kh = old_bucket->key_hash_list.data + j;
HashMapBucket* new_bucket = &table_expanded[kh.hash % height_expanded]; HashMapBucket* new_bucket = &table_expanded[kh->hash % height_expanded];
List_push(&new_bucket->key_hash_list, HashMapKeyHash, kh);
List_HashMapKeyHash_pushMany(&new_bucket->key_hash_list, kh, 1);
void* old_value_ptr = (u8*)old_bucket->value_list.data + j * self->value_t_size; void* old_value_ptr = (u8*)old_bucket->value_list.data + j * self->value_t_size;
List_push_size(&new_bucket->value_list, old_value_ptr, self->value_t_size); _List_push(&new_bucket->value_list, old_value_ptr, 1);
} }
free(old_bucket->key_hash_list.data); List_HashMapKeyHash_destroy(&old_bucket->key_hash_list);
free(old_bucket->value_list.data); _List_destroy(&old_bucket->value_list);
} }
free(self->table); free(self->table);
self->table = table_expanded; self->table = table_expanded;
@ -129,14 +141,14 @@ bool HashMap_tryPush(HashMap_* self, const str key, void* value_ptr){
return false; return false;
HashMapBucket* bu = r.bu; HashMapBucket* bu = r.bu;
if(bu == NULL || List_len(bu->key_hash_list, HashMapKeyHash) >= __HashMapBucket_MAX_LEN){ if(bu == NULL || r.bu->key_hash_list.len >= __HashMapBucket_MAX_LEN){
__HashMap_expand(self); __HashMap_expand(self);
bu = &self->table[hash % self->height]; bu = &self->table[hash % self->height];
} }
HashMapKeyHash kh = { .key = str_copy(key), .hash = hash }; HashMapKeyHash kh = { .key = str_copy(key), .hash = hash };
List_push(&bu->key_hash_list, HashMapKeyHash, kh); List_HashMapKeyHash_push(&bu->key_hash_list, kh);
List_push_size(&bu->value_list, value_ptr, self->value_t_size); _List_push(&bu->value_list, value_ptr, 1);
return true; return true;
} }
@ -151,14 +163,14 @@ void HashMap_pushOrUpdate(HashMap_* self, const str key, void* value_ptr){
} }
HashMapBucket* bu = r.bu; HashMapBucket* bu = r.bu;
if(bu == NULL || List_len(bu->key_hash_list, HashMapKeyHash) >= __HashMapBucket_MAX_LEN){ if(bu == NULL || r.bu->key_hash_list.len >= __HashMapBucket_MAX_LEN){
__HashMap_expand(self); __HashMap_expand(self);
bu = &self->table[hash % self->height]; bu = &self->table[hash % self->height];
} }
HashMapKeyHash kh = { .key = str_copy(key), .hash = hash }; HashMapKeyHash kh = { .key = str_copy(key), .hash = hash };
List_push(&bu->key_hash_list, HashMapKeyHash, kh); List_HashMapKeyHash_push(&bu->key_hash_list, kh);
List_push_size(&bu->value_list, value_ptr, self->value_t_size); _List_push(&bu->value_list, value_ptr, 1);
} }
bool HashMap_tryDelete(HashMap_* self, const str key){ bool HashMap_tryDelete(HashMap_* self, const str key){
@ -168,8 +180,11 @@ bool HashMap_tryDelete(HashMap_* self, const str key){
if(r.i == -1) if(r.i == -1)
return false; return false;
List_removeAt(&r.bu->key_hash_list, HashMapKeyHash, r.i, 1); if(!List_HashMapKeyHash_tryRemoveAt(&r.bu->key_hash_list, r.i, 1))
List_removeAt_size(&r.bu->value_list, r.i, self->value_t_size); return false;
if(!_List_tryRemoveAt(&r.bu->value_list, r.i, 1))
return false;
return true; return true;
} }
@ -182,7 +197,7 @@ bool HashMapIter_moveNext(HashMapIter* self){
} }
// move to next element in current bucket // move to next element in current bucket
const HashMapBucket* bu = &self->map->table[self->bucket_n]; const HashMapBucket* bu = &self->map->table[self->bucket_n];
i32 bu_elem_len = List_len(bu->key_hash_list, HashMapKeyHash); i32 bu_elem_len = bu->key_hash_list.len;
if(self->elem_n + 1 < bu_elem_len){ if(self->elem_n + 1 < bu_elem_len){
self->elem_n++; self->elem_n++;
return true; return true;
@ -193,7 +208,7 @@ bool HashMapIter_moveNext(HashMapIter* self){
self->bucket_n++; self->bucket_n++;
self->elem_n = 0; self->elem_n = 0;
bu = &self->map->table[self->bucket_n]; bu = &self->map->table[self->bucket_n];
if(bu->key_hash_list.size != 0) if(bu->key_hash_list.len != 0)
return true; return true;
} }
} }
@ -206,10 +221,10 @@ bool HashMapIter_getCurrent(HashMapIter* self, HashMapKeyValue* kv){
const HashMapBucket* bu = &self->map->table[self->bucket_n]; const HashMapBucket* bu = &self->map->table[self->bucket_n];
// table is empty // table is empty
if(bu->key_hash_list.size == 0) if(bu->key_hash_list.len == 0)
return false; return false;
kv->key = List_index(bu->key_hash_list, HashMapKeyHash, self->elem_n).key; kv->key = bu->key_hash_list.data[self->elem_n].key;
kv->value = (u8*)bu->value_list.data + self->map->value_t_size * self->elem_n; kv->value = (u8*)bu->value_list.data + self->map->value_t_size * self->elem_n;
return true; return true;
} }

View File

@ -1,55 +1,73 @@
#include "tlibc/collections/List.h" #include "tlibc/collections/List.h"
#include <assert.h>
List_ List_alloc_size(u32 initial_size){ List_ _List_alloc(u32 initial_capacity, u32 elem_t_size){
if(initial_size == 0) assert(elem_t_size != 0);
return List_null; if(initial_capacity == 0)
u32 allocated_size = ALIGN_TO(initial_size, sizeof(void*)); return _List_construct(NULL, 0, 0, elem_t_size);
return List_construct_size(malloc(allocated_size), 0, allocated_size); u32 initial_size = initial_capacity * elem_t_size;
u32 aligned_capacity = ALIGN_TO(initial_size, sizeof(void*));
return _List_construct(malloc(aligned_capacity), 0, aligned_capacity, elem_t_size);
} }
List_ List_copy(const List_ src){ List_ _List_copy(const List_* src){
List_ copy = List_alloc_size(src.allocated_size); assert(src->elem_t_size != 0);
if(copy.data != NULL) List_ copy = _List_alloc(src->capacity, src->elem_t_size);
memcpy(copy.data, src.data, src.size); if(copy.data != NULL){
memcpy(copy.data, src->data, src->len * src->elem_t_size);
}
return copy; return copy;
} }
void List_increaseCapacity_size(List_* self, u32 size_to_add){ void _List_increaseCapacity(List_* self, u32 len_to_add){
u32 occupied_size = self->size; assert(self->elem_t_size != 0);
u32 expanded_size = occupied_size + size_to_add;
if(self->allocated_size < expanded_size) { u32 expanded_len = self->len + len_to_add;
u32 expanded_alloc_size = self->allocated_size; if(self->capacity > expanded_len)
if(expanded_alloc_size == 0) return;
expanded_alloc_size = 32;
while(expanded_alloc_size < expanded_size){ u32 expanded_capacity = self->capacity;
expanded_alloc_size *= 2; if(expanded_capacity == 0)
expanded_capacity = 32;
while(expanded_capacity < expanded_len){
expanded_capacity *= 2;
} }
// if self->data is null, realloc acts like malloc // if self->data is null, realloc acts like malloc
self->data = realloc(self->data, expanded_alloc_size); self->data = realloc(self->data, expanded_capacity * self->elem_t_size);
self->allocated_size = expanded_alloc_size; self->capacity = expanded_capacity;
}
} }
void* List_expand_size(List_* self, u32 size_to_add){ void* _List_expand(List_* self, u32 len_to_add){
List_increaseCapacity_size(self, size_to_add); assert(self->elem_t_size != 0);
u8* empty_cell_ptr = (u8*)self->data + self->size; _List_increaseCapacity(self, len_to_add);
self->size += size_to_add; u8* empty_cell_ptr = (u8*)self->data + self->len * self->elem_t_size;
self->len += len_to_add;
return empty_cell_ptr; return empty_cell_ptr;
} }
void List_push_size(List_* self, void* values, u32 size){ void _List_push(List_* self, void* values, u32 len){
void* empty_cell_ptr = List_expand_size(self, size); assert(self->elem_t_size != 0);
memcpy(empty_cell_ptr, values, size); void* empty_cell_ptr = _List_expand(self, len);
memcpy(empty_cell_ptr, values, len * self->elem_t_size);
} }
bool List_removeAt_size(List_* self, u32 i, u32 remove_size){ bool _List_tryRemoveAt(List_* self, u32 i, u32 remove_len){
if(i + remove_size >= self->size) assert(self->elem_t_size != 0);
if(i >= self->len)
return false; return false;
self->size -= remove_size; if(remove_len + i > self->len)
u8* src = (u8*)self->data + i + remove_size; remove_len = self->len - i;
u8* dst = (u8*)self->data + i;
memmove(dst, src, self->size - i - remove_size); u32 move_back_n_elements = self->len - i - remove_len;
if(move_back_n_elements > 0){
u32 move_back_size = move_back_n_elements * self->elem_t_size;
u8* dst = (u8*)self->data + i * self->elem_t_size;
u8* src = (u8*)self->data + (i + remove_len) * self->elem_t_size;
memmove(dst, src, move_back_size);
}
self->len -= remove_len;
return true; return true;
} }

View File

@ -11,7 +11,7 @@ Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p,
e->is_msg_on_heap = is_msg_on_heap; e->is_msg_on_heap = is_msg_on_heap;
e->error_code_page = error_code_page; e->error_code_page = error_code_page;
e->error_code = error_code; e->error_code = error_code;
e->call_stack = List_alloc(ErrorCallPos, 16); e->call_stack = List_ErrorCallPos_alloc(16);
Error_addCallPos(e, p); Error_addCallPos(e, p);
return e; return e;
} }
@ -24,12 +24,12 @@ void Error_free(Error* e){
} }
void Error_addCallPos(Error* e, ErrorCallPos p){ void Error_addCallPos(Error* e, ErrorCallPos p){
List_push(&e->call_stack, ErrorCallPos, p); List_ErrorCallPos_push(&e->call_stack, p);
} }
str Error_toStr(Error* e){ str Error_toStr(Error* e){
u32 len = List_len(e->call_stack, ErrorCallPos); u32 len = e->call_stack.len;
StringBuilder b = StringBuilder_alloc(e->msg.size + 80 * len); StringBuilder b = StringBuilder_alloc(e->msg.len + 80 * len);
StringBuilder_append_str(&b, STR("Catched Error: ")); StringBuilder_append_str(&b, STR("Catched Error: "));
StringBuilder_append_str(&b, e->msg); StringBuilder_append_str(&b, e->msg);
@ -50,7 +50,7 @@ str Error_toStr(Error* e){
void Error_printAndExit(Error* e){ void Error_printAndExit(Error* e){
str e_str = Error_toStr(e); str e_str = Error_toStr(e);
printfe("\n"FMT_str"\n", e_str.size, e_str.data); printfe("\n"FMT_str"\n", e_str.len, e_str.data);
free(e_str.data); free(e_str.data);
Error_free(e); Error_free(e);
exit(111); exit(111);

View File

@ -141,7 +141,7 @@ Result(void) file_readWhole(FILE* f, Array(u8)* out_buf){
bool success = false; bool success = false;
try(i64 f_size, i, file_getSize(f)); try(i64 f_size, i, file_getSize(f));
Array(u8) buf = Array_alloc(u8, f_size); Array(u8) buf = Array_u8_alloc(f_size);
Defer(if(!success) free(buf.data)); Defer(if(!success) free(buf.data));
try_void(file_readBytesArray(f, buf)); try_void(file_readBytesArray(f, buf));

View File

@ -1,36 +1,36 @@
#include "tlibc/filesystem.h" #include "tlibc/filesystem.h"
str path_dirname(str path){ str path_dirname(str path){
if(path.size == 0) if(path.len == 0)
return path; return path;
// remove trailing slash (name/) // remove trailing slash (name/)
if(path.data[path.size - 1] == path_sep){ if(path.data[path.len - 1] == path_sep){
path.size -= 1; path.len -= 1;
} }
i32 sepIndex = str_seekCharReverse(path, path_sep, -1); i32 sepIndex = str_seekCharReverse(path, path_sep, -1);
if(sepIndex < 0) if(sepIndex < 0)
return STR("."); return STR(".");
path.size = sepIndex; path.len = sepIndex;
path.isZeroTerminated = false; path.isZeroTerminated = false;
return path; return path;
} }
str path_basename(str path, bool remove_ext){ str path_basename(str path, bool remove_ext){
if(path.size == 0) if(path.len == 0)
return path; return path;
// remove trailing slash (name/) // remove trailing slash (name/)
if(path.data[path.size - 1] == path_sep){ if(path.data[path.len - 1] == path_sep){
path.size -= 1; path.len -= 1;
} }
i32 nameIndex = str_seekCharReverse(path, path_sep, -1) + 1; i32 nameIndex = str_seekCharReverse(path, path_sep, -1) + 1;
if((u32)nameIndex != 0){ if((u32)nameIndex != 0){
path.data += nameIndex; path.data += nameIndex;
path.size -= nameIndex; path.len -= nameIndex;
} }
if(!remove_ext) if(!remove_ext)
@ -38,7 +38,7 @@ str path_basename(str path, bool remove_ext){
i32 extIndex = str_seekCharReverse(path, '.', -1); i32 extIndex = str_seekCharReverse(path, '.', -1);
if(extIndex > 0){ if(extIndex > 0){
path.size = extIndex; path.len = extIndex;
path.isZeroTerminated = false; path.isZeroTerminated = false;
} }
return path; return path;

View File

@ -8,41 +8,40 @@ void StringBuilder_destroy(StringBuilder* b){
} }
str StringBuilder_getStr(StringBuilder* b){ str StringBuilder_getStr(StringBuilder* b){
if(b->buffer.size == 0 || ((char*)b->buffer.data)[b->buffer.size - 1] != '\0'){ if(b->buffer.len == 0 || b->buffer.data[b->buffer.len - 1] != '\0'){
List_push(&b->buffer, u8, '\0'); List_char_push(&b->buffer, '\0');
// '\0' at the end doesn't increase buffer.size // '\0' at the end doesn't increase buffer.len
b->buffer.size -= 1; b->buffer.len -= 1;
} }
str result = str_construct((char*)b->buffer.data, b->buffer.size, true); str result = str_construct(b->buffer.data, b->buffer.len, true);
return result; return result;
} }
bool StringBuilder_equals(const StringBuilder* a, const StringBuilder* b){ bool StringBuilder_equals(const StringBuilder* a, const StringBuilder* b){
str a_str = str_construct((char*)a->buffer.data, a->buffer.size, false); str a_str = str_construct(a->buffer.data, a->buffer.len, false);
str b_str = str_construct((char*)b->buffer.data, b->buffer.size, false); str b_str = str_construct(b->buffer.data, b->buffer.len, false);
return str_equals(a_str, b_str); return str_equals(a_str, b_str);
} }
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count){ void StringBuilder_removeFromEnd(StringBuilder* b, u32 count){
if(count < b->buffer.size){ if(count < b->buffer.len){
b->buffer.size -= count; b->buffer.len -= count;
} }
else{ else{
b->buffer.size = 0; b->buffer.len = 0;
} }
} }
void StringBuilder_append_char(StringBuilder* b, char c){ void StringBuilder_append_char(StringBuilder* b, char c){
List_push(&b->buffer, u8, c); List_char_push(&b->buffer, c);
} }
void StringBuilder_append_str(StringBuilder* b, str s){ void StringBuilder_append_str(StringBuilder* b, str s){
if(s.data == NULL) if(s.data == NULL)
return; return;
List_push_size(&b->buffer, s.data, s.size); List_char_pushMany(&b->buffer, s.data, s.len);
} }
void StringBuilder_append_cstr(StringBuilder* b, cstr s){ void StringBuilder_append_cstr(StringBuilder* b, cstr s){
@ -78,7 +77,7 @@ void StringBuilder_append_memory(StringBuilder* b, Array(u8) mem, bool uppercase
return; return;
char buf[8]; char buf[8];
for (u32 i=0; i < mem.size; i++) { for (u32 i=0; i < mem.len; i++) {
sprintf(buf, uppercase ? "%02X" : "%02x", ((u8*)mem.data)[i]); sprintf(buf, uppercase ? "%02X" : "%02x", ((u8*)mem.data)[i]);
StringBuilder_append_str(b, str_construct(buf, 2, true)); StringBuilder_append_str(b, str_construct(buf, 2, true));
} }

View File

@ -2,17 +2,17 @@
#include "tlibc/string/StringBuilder.h" #include "tlibc/string/StringBuilder.h"
str str_copy(const str self){ str str_copy(const str self){
if(self.data == NULL || self.size == 0) if(self.data == NULL || self.len == 0)
return self; return self;
str copy = str_construct((char*)malloc(self.size + 1), self.size, true); str copy = str_construct((char*)malloc(self.len + 1), self.len, true);
memcpy(copy.data, self.data, self.size); memcpy(copy.data, self.data, self.len);
copy.data[copy.size] = '\0'; copy.data[copy.len] = '\0';
return copy; return copy;
} }
bool str_equals(const str self, const str other){ bool str_equals(const str self, const str other){
if(self.size != other.size) if(self.len != other.len)
return false; return false;
if(self.data == other.data) if(self.data == other.data)
return true; return true;
@ -23,26 +23,26 @@ bool str_equals(const str self, const str other){
strncmp: 1.611s strncmp: 1.611s
memcmp: 0.710s memcmp: 0.710s
*/ */
return memcmp(self.data, other.data, self.size) == 0; return memcmp(self.data, other.data, self.len) == 0;
} }
str str_reverse(str s){ str str_reverse(str s){
if(s.data == NULL || s.size == 0) if(s.data == NULL || s.len == 0)
return s; return s;
str r = str_construct(malloc(s.size), s.size, s.isZeroTerminated); str r = str_construct(malloc(s.len), s.len, s.isZeroTerminated);
for(u32 i = 0; i < s.size; i++ ) for(u32 i = 0; i < s.len; i++ )
r.data[i] = s.data[s.size - i - 1]; r.data[i] = s.data[s.len - i - 1];
return r; return r;
} }
i32 str_seek(const str src, const str fragment, u32 startIndex){ i32 str_seek(const str src, const str fragment, u32 startIndex){
if(src.size == 0 || fragment.size == 0) if(src.len == 0 || fragment.len == 0)
return -1; return -1;
for(u32 i = startIndex; i < src.size - fragment.size + 1; i++){ for(u32 i = startIndex; i < src.len - fragment.len + 1; i++){
for(u32 j = 0;; j++){ for(u32 j = 0;; j++){
if(j == fragment.size) if(j == fragment.len)
return i; return i;
if(src.data[i + j] != fragment.data[j]) if(src.data[i + j] != fragment.data[j])
break; break;
@ -52,16 +52,16 @@ i32 str_seek(const str src, const str fragment, u32 startIndex){
} }
i32 str_seekReverse(const str src, const str fragment, u32 startIndex){ i32 str_seekReverse(const str src, const str fragment, u32 startIndex){
if(src.size == 0 || fragment.size == 0) if(src.len == 0 || fragment.len == 0)
return -1; return -1;
if(startIndex > src.size - 1) if(startIndex > src.len - 1)
startIndex = src.size - 1; startIndex = src.len - 1;
for(u32 i = startIndex; i >= fragment.size - 1; i--){ for(u32 i = startIndex; i >= fragment.len - 1; i--){
for(u32 j = 0;; j++){ for(u32 j = 0;; j++){
if(j == fragment.size) if(j == fragment.len)
return i - j + 1; return i - j + 1;
if(src.data[i - j] != fragment.data[fragment.size - 1 - j]) if(src.data[i - j] != fragment.data[fragment.len - 1 - j])
break; break;
} }
} }
@ -69,7 +69,7 @@ i32 str_seekReverse(const str src, const str fragment, u32 startIndex){
} }
i32 str_seekChar(const str src, char c, u32 startIndex){ i32 str_seekChar(const str src, char c, u32 startIndex){
for(u32 i = startIndex; i < src.size; i++){ for(u32 i = startIndex; i < src.len; i++){
if(src.data[i] == c) if(src.data[i] == c)
return i; return i;
} }
@ -77,8 +77,8 @@ i32 str_seekChar(const str src, char c, u32 startIndex){
} }
i32 str_seekCharReverse(const str src, char c, u32 startIndex){ i32 str_seekCharReverse(const str src, char c, u32 startIndex){
if(startIndex > src.size - 1) if(startIndex > src.len - 1)
startIndex = src.size - 1; startIndex = src.len - 1;
for(u32 i = startIndex; i != (u32)-1; i--){ for(u32 i = startIndex; i != (u32)-1; i--){
if(src.data[i] == c) if(src.data[i] == c)
return i; return i;
@ -87,36 +87,36 @@ i32 str_seekCharReverse(const str src, char c, u32 startIndex){
} }
bool str_startsWith(const str src, const str fragment){ bool str_startsWith(const str src, const str fragment){
if(src.size < fragment.size) if(src.len < fragment.len)
return false; return false;
str src_fragment = str_null; str src_fragment = str_null;
src_fragment.data = src.data; src_fragment.data = src.data;
src_fragment.size = fragment.size; src_fragment.len = fragment.len;
return str_equals(src_fragment, fragment); return str_equals(src_fragment, fragment);
} }
bool str_endsWith(const str src, const str fragment){ bool str_endsWith(const str src, const str fragment){
if(src.size < fragment.size) if(src.len < fragment.len)
return false; return false;
str src_fragment = str_null; str src_fragment = str_null;
src_fragment.data = (char*)(src.data + src.size - fragment.size); src_fragment.data = (char*)(src.data + src.len - fragment.len);
src_fragment.size = fragment.size; src_fragment.len = fragment.len;
return str_equals(src_fragment, fragment); return str_equals(src_fragment, fragment);
} }
u32 str_hash32(const str s){ u32 str_hash32(const str s){
u8* ubuf = (u8*)s.data; u8* ubuf = (u8*)s.data;
u32 hash=0; u32 hash=0;
for (u32 i = 0; i < s.size; i++) for (u32 i = 0; i < s.len; i++)
hash = (hash<<6) + (hash<<16) - hash + ubuf[i]; hash = (hash<<6) + (hash<<16) - hash + ubuf[i];
return hash; return hash;
} }
str str_toUpper(const str src){ str str_toUpper(const str src){
str r = str_copy(src); str r = str_copy(src);
for (u32 i = 0; i < r.size; i++){ for (u32 i = 0; i < r.len; i++){
if(char_isLatinLower(r.data[i])) if(char_isLatinLower(r.data[i]))
r.data[i] = r.data[i] - 'a' + 'A'; r.data[i] = r.data[i] - 'a' + 'A';
} }
@ -125,7 +125,7 @@ str str_toUpper(const str src){
str str_toLower(const str src){ str str_toLower(const str src){
str r = str_copy(src); str r = str_copy(src);
for (u32 i = 0; i < r.size; i++){ for (u32 i = 0; i < r.len; i++){
if(char_isLatinUpper(r.data[i])) if(char_isLatinUpper(r.data[i]))
r.data[i] = r.data[i] - 'A' + 'a'; r.data[i] = r.data[i] - 'A' + 'a';
} }
@ -133,7 +133,7 @@ str str_toLower(const str src){
} }
str hex_to_str(Array(u8) buf, bool uppercase){ str hex_to_str(Array(u8) buf, bool uppercase){
StringBuilder sb = StringBuilder_alloc(buf.size * 2 + 1); StringBuilder sb = StringBuilder_alloc(buf.len * 2 + 1);
StringBuilder_append_memory(&sb, buf, uppercase); StringBuilder_append_memory(&sb, buf, uppercase);
return StringBuilder_getStr(&sb); return StringBuilder_getStr(&sb);
} }
@ -141,13 +141,13 @@ str hex_to_str(Array(u8) buf, bool uppercase){
void str_trim(str* line, bool set_zero_at_end){ void str_trim(str* line, bool set_zero_at_end){
// loop forward // loop forward
bool stop = false; bool stop = false;
while(line->size > 0 && !stop){ while(line->len > 0 && !stop){
char first_char = line->data[0]; char first_char = line->data[0];
switch(first_char){ switch(first_char){
case '\0': case '\r': case '\n': case '\0': case '\r': case '\n':
case '\t': case ' ': case '\t': case ' ':
line->data++; line->data++;
line->size--; line->len--;
break; break;
default: default:
stop = true; stop = true;
@ -157,13 +157,13 @@ void str_trim(str* line, bool set_zero_at_end){
// loop backward // loop backward
stop = false; stop = false;
while(line->size > 0 && !stop) while(line->len > 0 && !stop)
{ {
char last_char = line->data[line->size - 1]; char last_char = line->data[line->len - 1];
switch(last_char){ switch(last_char){
case '\0': case '\r': case '\n': case '\0': case '\r': case '\n':
case '\t': case ' ': case '\t': case ' ':
line->size--; line->len--;
break; break;
default: default:
stop = true; stop = true;
@ -172,7 +172,7 @@ void str_trim(str* line, bool set_zero_at_end){
} }
if(set_zero_at_end){ if(set_zero_at_end){
line->data[line->size] = '\0'; line->data[line->len] = '\0';
line->isZeroTerminated = true; line->isZeroTerminated = true;
} }
} }