diff --git a/include/tlibc/collections/Array.h b/include/tlibc/collections/Array.h index 737c184..9b08cd8 100755 --- a/include/tlibc/collections/Array.h +++ b/include/tlibc/collections/Array.h @@ -1,57 +1,62 @@ #pragma once #include "../std.h" -#define Array(T) Array_ +/* +Pointer and length. +Can be initialized with {0}. +*/ -typedef struct Array_ { - void* data; - u32 size; -} Array_; +#define Array(T) Array_##T -/// creates Array_ from self const array -#define ARRAY(T, A...) Array_construct_size(((T[])A), sizeof((T[])A)) +/// create Array(T) from array initializer list ({ a, b, c }) +#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_construct_size(DATA, LEN) ((Array_){ .data = (DATA), .size = (LEN) }) +#define Array_declare(T) \ +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 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); -} +#define struct_castTo_Array_u8(STRUCT_PTR) Array_u8_construct((void*)(STRUCT_PTR), sizeof(*STRUCT_PTR)) diff --git a/include/tlibc/collections/Array_impl/Array_char.h b/include/tlibc/collections/Array_impl/Array_char.h new file mode 100644 index 0000000..0aefc70 --- /dev/null +++ b/include/tlibc/collections/Array_impl/Array_char.h @@ -0,0 +1,2 @@ +#pragma once +Array_declare(char); diff --git a/include/tlibc/collections/Array_impl/Array_f32.h b/include/tlibc/collections/Array_impl/Array_f32.h new file mode 100644 index 0000000..ec2e841 --- /dev/null +++ b/include/tlibc/collections/Array_impl/Array_f32.h @@ -0,0 +1,2 @@ +#pragma once +Array_declare(f32); diff --git a/include/tlibc/collections/Array_impl/Array_f64.h b/include/tlibc/collections/Array_impl/Array_f64.h new file mode 100644 index 0000000..509be7d --- /dev/null +++ b/include/tlibc/collections/Array_impl/Array_f64.h @@ -0,0 +1,2 @@ +#pragma once +Array_declare(f64); diff --git a/include/tlibc/collections/Array_impl/Array_i16.h b/include/tlibc/collections/Array_impl/Array_i16.h new file mode 100644 index 0000000..a603748 --- /dev/null +++ b/include/tlibc/collections/Array_impl/Array_i16.h @@ -0,0 +1,2 @@ +#pragma once +Array_declare(i16); diff --git a/include/tlibc/collections/Array_impl/Array_i32.h b/include/tlibc/collections/Array_impl/Array_i32.h new file mode 100644 index 0000000..6cf6f7b --- /dev/null +++ b/include/tlibc/collections/Array_impl/Array_i32.h @@ -0,0 +1,2 @@ +#pragma once +Array_declare(i32); diff --git a/include/tlibc/collections/Array_impl/Array_i64.h b/include/tlibc/collections/Array_impl/Array_i64.h new file mode 100644 index 0000000..0c0e567 --- /dev/null +++ b/include/tlibc/collections/Array_impl/Array_i64.h @@ -0,0 +1,2 @@ +#pragma once +Array_declare(i64); diff --git a/include/tlibc/collections/Array_impl/Array_i8.h b/include/tlibc/collections/Array_impl/Array_i8.h new file mode 100644 index 0000000..47851e7 --- /dev/null +++ b/include/tlibc/collections/Array_impl/Array_i8.h @@ -0,0 +1,2 @@ +#pragma once +Array_declare(i8); diff --git a/include/tlibc/collections/Array_impl/Array_u16.h b/include/tlibc/collections/Array_impl/Array_u16.h new file mode 100644 index 0000000..6b944ac --- /dev/null +++ b/include/tlibc/collections/Array_impl/Array_u16.h @@ -0,0 +1,2 @@ +#pragma once +Array_declare(u16); diff --git a/include/tlibc/collections/Array_impl/Array_u32.h b/include/tlibc/collections/Array_impl/Array_u32.h new file mode 100644 index 0000000..d9b07f3 --- /dev/null +++ b/include/tlibc/collections/Array_impl/Array_u32.h @@ -0,0 +1,2 @@ +#pragma once +Array_declare(u32); diff --git a/include/tlibc/collections/Array_impl/Array_u64.h b/include/tlibc/collections/Array_impl/Array_u64.h new file mode 100644 index 0000000..65edd5d --- /dev/null +++ b/include/tlibc/collections/Array_impl/Array_u64.h @@ -0,0 +1,2 @@ +#pragma once +Array_declare(u64); diff --git a/include/tlibc/collections/Array_impl/Array_u8.h b/include/tlibc/collections/Array_impl/Array_u8.h new file mode 100644 index 0000000..f40f7f7 --- /dev/null +++ b/include/tlibc/collections/Array_impl/Array_u8.h @@ -0,0 +1,2 @@ +#pragma once +Array_declare(u8); diff --git a/include/tlibc/collections/HashMap.h b/include/tlibc/collections/HashMap.h index 6e2d5c8..6e9cf29 100755 --- a/include/tlibc/collections/HashMap.h +++ b/include/tlibc/collections/HashMap.h @@ -1,7 +1,6 @@ #pragma once #include "../std.h" #include "../string/str.h" -#include "Array.h" #include "List.h" typedef struct HashMapKeyHash { @@ -9,9 +8,11 @@ typedef struct HashMapKeyHash { u32 hash; } HashMapKeyHash; +List_declare(HashMapKeyHash); + typedef struct HashMapBucket { List(HashMapKeyHash) key_hash_list; - List(T) value_list; + List_ value_list; } HashMapBucket; #define HashMap(T) HashMap_ diff --git a/include/tlibc/collections/LList.h b/include/tlibc/collections/LList.h index 99a7bb2..c5fafe4 100644 --- a/include/tlibc/collections/LList.h +++ b/include/tlibc/collections/LList.h @@ -2,7 +2,8 @@ #include "../std.h" /* -Doubly linked list +Doubly linked list. +Can be initialized with {0}. */ #define LLNode(T) LLNode_##T diff --git a/include/tlibc/collections/LList_impl/LList_char.h b/include/tlibc/collections/LList_impl/LList_char.h new file mode 100644 index 0000000..8cf69ef --- /dev/null +++ b/include/tlibc/collections/LList_impl/LList_char.h @@ -0,0 +1,2 @@ +#pragma once +LList_declare(char); diff --git a/include/tlibc/collections/LList_impl/LList_f32.h b/include/tlibc/collections/LList_impl/LList_f32.h new file mode 100644 index 0000000..0843953 --- /dev/null +++ b/include/tlibc/collections/LList_impl/LList_f32.h @@ -0,0 +1,2 @@ +#pragma once +LList_declare(f32); diff --git a/include/tlibc/collections/LList_impl/LList_f64.h b/include/tlibc/collections/LList_impl/LList_f64.h new file mode 100644 index 0000000..7111038 --- /dev/null +++ b/include/tlibc/collections/LList_impl/LList_f64.h @@ -0,0 +1,2 @@ +#pragma once +LList_declare(f64); diff --git a/include/tlibc/collections/LList_impl/LList_i16.h b/include/tlibc/collections/LList_impl/LList_i16.h new file mode 100644 index 0000000..4d23da6 --- /dev/null +++ b/include/tlibc/collections/LList_impl/LList_i16.h @@ -0,0 +1,2 @@ +#pragma once +LList_declare(i16); diff --git a/include/tlibc/collections/LList_impl/LList_i32.h b/include/tlibc/collections/LList_impl/LList_i32.h new file mode 100644 index 0000000..f5393cf --- /dev/null +++ b/include/tlibc/collections/LList_impl/LList_i32.h @@ -0,0 +1,2 @@ +#pragma once +LList_declare(i32); diff --git a/include/tlibc/collections/LList_impl/LList_i64.h b/include/tlibc/collections/LList_impl/LList_i64.h new file mode 100644 index 0000000..2f3397a --- /dev/null +++ b/include/tlibc/collections/LList_impl/LList_i64.h @@ -0,0 +1,2 @@ +#pragma once +LList_declare(i64); diff --git a/include/tlibc/collections/LList_impl/LList_i8.h b/include/tlibc/collections/LList_impl/LList_i8.h new file mode 100644 index 0000000..0a60b1e --- /dev/null +++ b/include/tlibc/collections/LList_impl/LList_i8.h @@ -0,0 +1,2 @@ +#pragma once +LList_declare(i8); diff --git a/include/tlibc/collections/LList_impl/LList_u16.h b/include/tlibc/collections/LList_impl/LList_u16.h new file mode 100644 index 0000000..b969beb --- /dev/null +++ b/include/tlibc/collections/LList_impl/LList_u16.h @@ -0,0 +1,2 @@ +#pragma once +LList_declare(u16); diff --git a/include/tlibc/collections/LList_impl/LList_u32.h b/include/tlibc/collections/LList_impl/LList_u32.h new file mode 100644 index 0000000..3d5a966 --- /dev/null +++ b/include/tlibc/collections/LList_impl/LList_u32.h @@ -0,0 +1,2 @@ +#pragma once +LList_declare(u32); diff --git a/include/tlibc/collections/LList_impl/LList_u64.h b/include/tlibc/collections/LList_impl/LList_u64.h new file mode 100644 index 0000000..cca09d4 --- /dev/null +++ b/include/tlibc/collections/LList_impl/LList_u64.h @@ -0,0 +1,2 @@ +#pragma once +LList_declare(u64); diff --git a/include/tlibc/collections/LList_impl/LList_u8.h b/include/tlibc/collections/LList_impl/LList_u8.h new file mode 100644 index 0000000..2fbb1dc --- /dev/null +++ b/include/tlibc/collections/LList_impl/LList_u8.h @@ -0,0 +1,2 @@ +#pragma once +LList_declare(u8); diff --git a/include/tlibc/collections/List.h b/include/tlibc/collections/List.h index f7f76f8..9824fdf 100755 --- a/include/tlibc/collections/List.h +++ b/include/tlibc/collections/List.h @@ -1,44 +1,89 @@ #pragma once #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_ { void* data; - u32 size; - u32 allocated_size; + u32 len; + u32 capacity; + u32 elem_t_size; } List_; -#define List_construct(T, DATA_PTR, OCCUPIED_COUNT, ALLOCATED_COUNT) \ - List_construct_size(DATA_PTR, (OCCUPIED_COUNT) * sizeof(T), (ALLOCATED_COUNT) * sizeof(T)) - -static inline List_ List_construct_size(void* data_ptr, u32 occupied_size, u32 allocated_size) { - return (List_){ .data = data_ptr, .size = occupied_size, .allocated_size = allocated_size }; +static inline List_ _List_construct(void* data_ptr, u32 len, u32 capacity, u32 elem_t_size) { + return (List_){ + .data = data_ptr, + .len = len, + .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)) -List_ List_alloc_size(u32 initial_size); - -static inline void List_destroy(List_* self){ +static inline void _List_destroy(List_* self){ if(!self) return; free(self->data); } -List_ List_copy(List_ src); - -// alloc bigger buffer if size + size_to_add won't fit in current -void List_increaseCapacity_size(List_* self, u32 size_to_add); -void* List_expand_size(List_* self, u32 size_to_add); -#define List_push(SELF, T, VALUE) *(T*)(List_expand_size(SELF, sizeof(T))) = VALUE -#define List_pushMany(SELF, T, VALUES_PTR, COUNT) List_push_size(SELF, VALUES_PTR, (COUNT) * sizeof(T)) -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) +List_ _List_alloc(u32 initial_capacity, u32 elem_t_size); +List_ _List_copy(const List_* src); +/* alloc bigger buffer if size + len_to_add won't fit in current */ +void _List_increaseCapacity(List_* self, u32 len_to_add); +void* _List_expand(List_* self, u32 len_to_add); +void _List_push(List_* self, void* values, u32 len); +bool _List_tryRemoveAt(List_* self, u32 i, u32 remove_len) ATTRIBUTE_WARN_UNUSED_RESULT; diff --git a/include/tlibc/collections/List_impl/List_char.h b/include/tlibc/collections/List_impl/List_char.h new file mode 100644 index 0000000..a6781d4 --- /dev/null +++ b/include/tlibc/collections/List_impl/List_char.h @@ -0,0 +1,2 @@ +#pragma once +List_declare(char); diff --git a/include/tlibc/collections/List_impl/List_f32.h b/include/tlibc/collections/List_impl/List_f32.h new file mode 100644 index 0000000..c20e0c5 --- /dev/null +++ b/include/tlibc/collections/List_impl/List_f32.h @@ -0,0 +1,2 @@ +#pragma once +List_declare(f32); diff --git a/include/tlibc/collections/List_impl/List_f64.h b/include/tlibc/collections/List_impl/List_f64.h new file mode 100644 index 0000000..3e7ad67 --- /dev/null +++ b/include/tlibc/collections/List_impl/List_f64.h @@ -0,0 +1,2 @@ +#pragma once +List_declare(f64); diff --git a/include/tlibc/collections/List_impl/List_i16.h b/include/tlibc/collections/List_impl/List_i16.h new file mode 100644 index 0000000..876cbd9 --- /dev/null +++ b/include/tlibc/collections/List_impl/List_i16.h @@ -0,0 +1,2 @@ +#pragma once +List_declare(i16); diff --git a/include/tlibc/collections/List_impl/List_i32.h b/include/tlibc/collections/List_impl/List_i32.h new file mode 100644 index 0000000..2bedc6b --- /dev/null +++ b/include/tlibc/collections/List_impl/List_i32.h @@ -0,0 +1,2 @@ +#pragma once +List_declare(i32); diff --git a/include/tlibc/collections/List_impl/List_i64.h b/include/tlibc/collections/List_impl/List_i64.h new file mode 100644 index 0000000..b02ae75 --- /dev/null +++ b/include/tlibc/collections/List_impl/List_i64.h @@ -0,0 +1,2 @@ +#pragma once +List_declare(i64); diff --git a/include/tlibc/collections/List_impl/List_i8.h b/include/tlibc/collections/List_impl/List_i8.h new file mode 100644 index 0000000..55531f5 --- /dev/null +++ b/include/tlibc/collections/List_impl/List_i8.h @@ -0,0 +1,2 @@ +#pragma once +List_declare(i8); diff --git a/include/tlibc/collections/List_impl/List_u16.h b/include/tlibc/collections/List_impl/List_u16.h new file mode 100644 index 0000000..49d908b --- /dev/null +++ b/include/tlibc/collections/List_impl/List_u16.h @@ -0,0 +1,2 @@ +#pragma once +List_declare(u16); diff --git a/include/tlibc/collections/List_impl/List_u32.h b/include/tlibc/collections/List_impl/List_u32.h new file mode 100644 index 0000000..b524fac --- /dev/null +++ b/include/tlibc/collections/List_impl/List_u32.h @@ -0,0 +1,2 @@ +#pragma once +List_declare(u32); diff --git a/include/tlibc/collections/List_impl/List_u64.h b/include/tlibc/collections/List_impl/List_u64.h new file mode 100644 index 0000000..88afbac --- /dev/null +++ b/include/tlibc/collections/List_impl/List_u64.h @@ -0,0 +1,2 @@ +#pragma once +List_declare(u64); diff --git a/include/tlibc/collections/List_impl/List_u8.h b/include/tlibc/collections/List_impl/List_u8.h new file mode 100644 index 0000000..cf450d4 --- /dev/null +++ b/include/tlibc/collections/List_impl/List_u8.h @@ -0,0 +1,2 @@ +#pragma once +List_declare(u8); diff --git a/include/tlibc/errors.h b/include/tlibc/errors.h index 8cb498c..64e0257 100755 --- a/include/tlibc/errors.h +++ b/include/tlibc/errors.h @@ -9,12 +9,15 @@ typedef struct ErrorCallPos { cstr file; cstr func; } ErrorCallPos; + #define ErrorCallPos_here() (ErrorCallPos){\ .line = __LINE__,\ .file = __FILE__,\ .func = __func__\ } +List_declare(ErrorCallPos); + typedef struct Error { str msg; bool is_msg_on_heap; diff --git a/include/tlibc/filesystem.h b/include/tlibc/filesystem.h index 47dea68..8447410 100644 --- a/include/tlibc/filesystem.h +++ b/include/tlibc/filesystem.h @@ -4,6 +4,7 @@ #include "errors.h" #include "string/str.h" #include "collections/Array.h" +#include "collections/Array_impl/Array_u8.h" #if !defined(TLIBC_FS_USE_WINDOWS_H) #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){ - 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); } -/// @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) 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); } -/// @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){ - 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 diff --git a/include/tlibc/string/StringBuilder.h b/include/tlibc/string/StringBuilder.h index ee4e721..a96c2ac 100755 --- a/include/tlibc/string/StringBuilder.h +++ b/include/tlibc/string/StringBuilder.h @@ -1,7 +1,7 @@ #pragma once #include "../collections/List.h" -#include "../collections/Array.h" +#include "../collections/List_impl/List_char.h" #include "str.h" typedef struct StringBuilder { @@ -9,17 +9,17 @@ typedef struct StringBuilder { } StringBuilder; 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); 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 -static inline void StringBuilder_increaseCapacity(StringBuilder* b, u32 size_to_add){ - List_increaseCapacity_size(&b->buffer, size_to_add); +// alloc bigger buffer if size + len_to_add won't fit in current +static inline void StringBuilder_increaseCapacity(StringBuilder* b, u32 len_to_add){ + List_char_increaseCapacity(&b->buffer, len_to_add); } diff --git a/include/tlibc/string/str.h b/include/tlibc/string/str.h index ec8af8d..78562e9 100755 --- a/include/tlibc/string/str.h +++ b/include/tlibc/string/str.h @@ -4,10 +4,12 @@ #include "char.h" #include "cstr.h" #include "../collections/Array.h" +#include "../collections/Array_impl/Array_char.h" +#include "../collections/Array_impl/Array_u8.h" typedef struct str { char* data; - u32 size; // size of data in bytes without \0 + u32 len; // size of data in bytes without \0 bool isZeroTerminated; } str; @@ -16,7 +18,7 @@ typedef struct str { /// creates str from a string literal #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){ return str_construct((void*)s_ptr, strlen(s_ptr), true); @@ -26,12 +28,18 @@ static inline void str_free(str s){ free(s.data); } -static inline Array_ str_castTo_Array(str s) { - return Array_construct_size(s.data, s.size); +static inline Array(char) str_castTo_Array_char(str s) { + 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) { - return str_construct(a.data, a.size, isZeroTerminated); +static inline str Array_char_castTo_str(Array(char) a, bool 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); @@ -74,5 +82,5 @@ static inline str str_sliceBefore(str s, u32 n){ ///@return s[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); } diff --git a/src/collections/HashMap.c b/src/collections/HashMap.c index b82e44d..7bb30a7 100755 --- a/src/collections/HashMap.c +++ b/src/collections/HashMap.c @@ -1,4 +1,6 @@ #include "tlibc/collections/HashMap.h" +#include "tlibc/collections/Array.h" +#include "tlibc/collections/Array_impl/Array_u32.h" #include //TODO: sort bucket keys for binary search @@ -9,9 +11,14 @@ static const Array(u32) __HashMap_heights = ARRAY(u32, { 0, 17, 31, 61, 127, 257, 521, 1021, 2053, 4099, 8191, 16381, 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)){ self->value_t_size = value_t_size; @@ -27,9 +34,9 @@ void HashMap_destroy(HashMap_* self){ for(u32 i = 0; i < self->height; i++){ HashMapBucket* bu = &self->table[i]; - u32 len = List_len(bu->key_hash_list, HashMapKeyHash); // free key strings + u32 len = bu->key_hash_list.len; for(u32 j = 0; j < len; j++){ HashMapKeyHash* kh = (HashMapKeyHash*)bu->key_hash_list.data + j; free(kh->key.data); @@ -38,7 +45,7 @@ void HashMap_destroy(HashMap_* self){ // destroy values if(self->value_destructor){ 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){ self->value_destructor(value_ptr); value_ptr += self->value_t_size; @@ -68,7 +75,8 @@ static BucketAndIndex __HashMap_search(const HashMap_* self, const str key, u32 BucketAndIndex r; 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; if(kh->hash == hash && str_equals(kh->key, key)){ return r; @@ -91,29 +99,33 @@ void* HashMap_tryGetPtr(const HashMap_* self, const str key){ static void __HashMap_expand(HashMap_* self){ 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 - 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); 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 for(u32 i = 0; i < self->height; 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++){ - HashMapKeyHash kh = ((HashMapKeyHash*)old_bucket->key_hash_list.data)[j]; - HashMapBucket* new_bucket = &table_expanded[kh.hash % height_expanded]; - List_push(&new_bucket->key_hash_list, HashMapKeyHash, kh); + HashMapKeyHash* kh = old_bucket->key_hash_list.data + j; + HashMapBucket* new_bucket = &table_expanded[kh->hash % height_expanded]; + + 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; - 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); - free(old_bucket->value_list.data); + List_HashMapKeyHash_destroy(&old_bucket->key_hash_list); + _List_destroy(&old_bucket->value_list); } free(self->table); self->table = table_expanded; @@ -129,14 +141,14 @@ bool HashMap_tryPush(HashMap_* self, const str key, void* value_ptr){ return false; 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); bu = &self->table[hash % self->height]; } HashMapKeyHash kh = { .key = str_copy(key), .hash = hash }; - List_push(&bu->key_hash_list, HashMapKeyHash, kh); - List_push_size(&bu->value_list, value_ptr, self->value_t_size); + List_HashMapKeyHash_push(&bu->key_hash_list, kh); + _List_push(&bu->value_list, value_ptr, 1); return true; } @@ -151,14 +163,14 @@ void HashMap_pushOrUpdate(HashMap_* self, const str key, void* value_ptr){ } 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); bu = &self->table[hash % self->height]; } HashMapKeyHash kh = { .key = str_copy(key), .hash = hash }; - List_push(&bu->key_hash_list, HashMapKeyHash, kh); - List_push_size(&bu->value_list, value_ptr, self->value_t_size); + List_HashMapKeyHash_push(&bu->key_hash_list, kh); + _List_push(&bu->value_list, value_ptr, 1); } bool HashMap_tryDelete(HashMap_* self, const str key){ @@ -168,8 +180,11 @@ bool HashMap_tryDelete(HashMap_* self, const str key){ if(r.i == -1) return false; - List_removeAt(&r.bu->key_hash_list, HashMapKeyHash, r.i, 1); - List_removeAt_size(&r.bu->value_list, r.i, self->value_t_size); + if(!List_HashMapKeyHash_tryRemoveAt(&r.bu->key_hash_list, r.i, 1)) + return false; + if(!_List_tryRemoveAt(&r.bu->value_list, r.i, 1)) + return false; + return true; } @@ -182,7 +197,7 @@ bool HashMapIter_moveNext(HashMapIter* self){ } // move to next element in current bucket 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){ self->elem_n++; return true; @@ -193,7 +208,7 @@ bool HashMapIter_moveNext(HashMapIter* self){ self->bucket_n++; self->elem_n = 0; bu = &self->map->table[self->bucket_n]; - if(bu->key_hash_list.size != 0) + if(bu->key_hash_list.len != 0) return true; } } @@ -206,10 +221,10 @@ bool HashMapIter_getCurrent(HashMapIter* self, HashMapKeyValue* kv){ const HashMapBucket* bu = &self->map->table[self->bucket_n]; // table is empty - if(bu->key_hash_list.size == 0) + if(bu->key_hash_list.len == 0) 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; return true; } diff --git a/src/collections/List.c b/src/collections/List.c index d2fa4f2..9355981 100755 --- a/src/collections/List.c +++ b/src/collections/List.c @@ -1,55 +1,73 @@ #include "tlibc/collections/List.h" +#include -List_ List_alloc_size(u32 initial_size){ - if(initial_size == 0) - return List_null; - u32 allocated_size = ALIGN_TO(initial_size, sizeof(void*)); - return List_construct_size(malloc(allocated_size), 0, allocated_size); +List_ _List_alloc(u32 initial_capacity, u32 elem_t_size){ + assert(elem_t_size != 0); + if(initial_capacity == 0) + return _List_construct(NULL, 0, 0, elem_t_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_ copy = List_alloc_size(src.allocated_size); - if(copy.data != NULL) - memcpy(copy.data, src.data, src.size); +List_ _List_copy(const List_* src){ + assert(src->elem_t_size != 0); + List_ copy = _List_alloc(src->capacity, src->elem_t_size); + if(copy.data != NULL){ + memcpy(copy.data, src->data, src->len * src->elem_t_size); + } return copy; } -void List_increaseCapacity_size(List_* self, u32 size_to_add){ - u32 occupied_size = self->size; - u32 expanded_size = occupied_size + size_to_add; +void _List_increaseCapacity(List_* self, u32 len_to_add){ + assert(self->elem_t_size != 0); + + u32 expanded_len = self->len + len_to_add; + if(self->capacity > expanded_len) + return; - if(self->allocated_size < expanded_size) { - u32 expanded_alloc_size = self->allocated_size; - if(expanded_alloc_size == 0) - expanded_alloc_size = 32; - while(expanded_alloc_size < expanded_size){ - expanded_alloc_size *= 2; - } - // if self->data is null, realloc acts like malloc - self->data = realloc(self->data, expanded_alloc_size); - self->allocated_size = expanded_alloc_size; + u32 expanded_capacity = self->capacity; + if(expanded_capacity == 0) + expanded_capacity = 32; + while(expanded_capacity < expanded_len){ + expanded_capacity *= 2; } + // if self->data is null, realloc acts like malloc + self->data = realloc(self->data, expanded_capacity * self->elem_t_size); + self->capacity = expanded_capacity; } -void* List_expand_size(List_* self, u32 size_to_add){ - List_increaseCapacity_size(self, size_to_add); - u8* empty_cell_ptr = (u8*)self->data + self->size; - self->size += size_to_add; +void* _List_expand(List_* self, u32 len_to_add){ + assert(self->elem_t_size != 0); + _List_increaseCapacity(self, len_to_add); + u8* empty_cell_ptr = (u8*)self->data + self->len * self->elem_t_size; + self->len += len_to_add; return empty_cell_ptr; } -void List_push_size(List_* self, void* values, u32 size){ - void* empty_cell_ptr = List_expand_size(self, size); - memcpy(empty_cell_ptr, values, size); +void _List_push(List_* self, void* values, u32 len){ + assert(self->elem_t_size != 0); + 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){ - if(i + remove_size >= self->size) +bool _List_tryRemoveAt(List_* self, u32 i, u32 remove_len){ + assert(self->elem_t_size != 0); + + if(i >= self->len) return false; - self->size -= remove_size; - u8* src = (u8*)self->data + i + remove_size; - u8* dst = (u8*)self->data + i; - memmove(dst, src, self->size - i - remove_size); + if(remove_len + i > self->len) + remove_len = self->len - i; + + 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; } diff --git a/src/errors.c b/src/errors.c index 8458a58..2ecbe22 100755 --- a/src/errors.c +++ b/src/errors.c @@ -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->error_code_page = error_code_page; e->error_code = error_code; - e->call_stack = List_alloc(ErrorCallPos, 16); + e->call_stack = List_ErrorCallPos_alloc(16); Error_addCallPos(e, p); return e; } @@ -24,12 +24,12 @@ void Error_free(Error* e){ } 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){ - u32 len = List_len(e->call_stack, ErrorCallPos); - StringBuilder b = StringBuilder_alloc(e->msg.size + 80 * len); + u32 len = e->call_stack.len; + StringBuilder b = StringBuilder_alloc(e->msg.len + 80 * len); StringBuilder_append_str(&b, STR("Catched Error: ")); StringBuilder_append_str(&b, e->msg); @@ -50,7 +50,7 @@ str Error_toStr(Error* e){ void Error_printAndExit(Error* 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); Error_free(e); exit(111); diff --git a/src/filesystem/file.c b/src/filesystem/file.c index bc68cbf..f100b7a 100644 --- a/src/filesystem/file.c +++ b/src/filesystem/file.c @@ -141,7 +141,7 @@ Result(void) file_readWhole(FILE* f, Array(u8)* out_buf){ bool success = false; 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)); try_void(file_readBytesArray(f, buf)); diff --git a/src/filesystem/path.c b/src/filesystem/path.c index ebd90f4..1ca48e2 100644 --- a/src/filesystem/path.c +++ b/src/filesystem/path.c @@ -1,36 +1,36 @@ #include "tlibc/filesystem.h" str path_dirname(str path){ - if(path.size == 0) + if(path.len == 0) return path; // remove trailing slash (name/) - if(path.data[path.size - 1] == path_sep){ - path.size -= 1; + if(path.data[path.len - 1] == path_sep){ + path.len -= 1; } i32 sepIndex = str_seekCharReverse(path, path_sep, -1); if(sepIndex < 0) return STR("."); - path.size = sepIndex; + path.len = sepIndex; path.isZeroTerminated = false; return path; } str path_basename(str path, bool remove_ext){ - if(path.size == 0) + if(path.len == 0) return path; // remove trailing slash (name/) - if(path.data[path.size - 1] == path_sep){ - path.size -= 1; + if(path.data[path.len - 1] == path_sep){ + path.len -= 1; } i32 nameIndex = str_seekCharReverse(path, path_sep, -1) + 1; if((u32)nameIndex != 0){ path.data += nameIndex; - path.size -= nameIndex; + path.len -= nameIndex; } if(!remove_ext) @@ -38,7 +38,7 @@ str path_basename(str path, bool remove_ext){ i32 extIndex = str_seekCharReverse(path, '.', -1); if(extIndex > 0){ - path.size = extIndex; + path.len = extIndex; path.isZeroTerminated = false; } return path; diff --git a/src/string/StringBuilder.c b/src/string/StringBuilder.c index 84cc1ce..81c2b5c 100755 --- a/src/string/StringBuilder.c +++ b/src/string/StringBuilder.c @@ -8,41 +8,40 @@ void StringBuilder_destroy(StringBuilder* b){ } str StringBuilder_getStr(StringBuilder* b){ - if(b->buffer.size == 0 || ((char*)b->buffer.data)[b->buffer.size - 1] != '\0'){ - List_push(&b->buffer, u8, '\0'); - // '\0' at the end doesn't increase buffer.size - b->buffer.size -= 1; + if(b->buffer.len == 0 || b->buffer.data[b->buffer.len - 1] != '\0'){ + List_char_push(&b->buffer, '\0'); + // '\0' at the end doesn't increase buffer.len + 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; } bool StringBuilder_equals(const StringBuilder* a, const StringBuilder* b){ - str a_str = str_construct((char*)a->buffer.data, a->buffer.size, false); - str b_str = str_construct((char*)b->buffer.data, b->buffer.size, false); + str a_str = str_construct(a->buffer.data, a->buffer.len, false); + str b_str = str_construct(b->buffer.data, b->buffer.len, false); return str_equals(a_str, b_str); } void StringBuilder_removeFromEnd(StringBuilder* b, u32 count){ - if(count < b->buffer.size){ - b->buffer.size -= count; + if(count < b->buffer.len){ + b->buffer.len -= count; } else{ - b->buffer.size = 0; + b->buffer.len = 0; } } 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){ if(s.data == NULL) 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){ @@ -78,7 +77,7 @@ void StringBuilder_append_memory(StringBuilder* b, Array(u8) mem, bool uppercase return; 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]); StringBuilder_append_str(b, str_construct(buf, 2, true)); } diff --git a/src/string/str.c b/src/string/str.c index e88aed0..3898491 100755 --- a/src/string/str.c +++ b/src/string/str.c @@ -2,17 +2,17 @@ #include "tlibc/string/StringBuilder.h" str str_copy(const str self){ - if(self.data == NULL || self.size == 0) + if(self.data == NULL || self.len == 0) return self; - str copy = str_construct((char*)malloc(self.size + 1), self.size, true); - memcpy(copy.data, self.data, self.size); - copy.data[copy.size] = '\0'; + str copy = str_construct((char*)malloc(self.len + 1), self.len, true); + memcpy(copy.data, self.data, self.len); + copy.data[copy.len] = '\0'; return copy; } bool str_equals(const str self, const str other){ - if(self.size != other.size) + if(self.len != other.len) return false; if(self.data == other.data) return true; @@ -23,26 +23,26 @@ bool str_equals(const str self, const str other){ strncmp: 1.611s 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){ - if(s.data == NULL || s.size == 0) + if(s.data == NULL || s.len == 0) return s; - str r = str_construct(malloc(s.size), s.size, s.isZeroTerminated); - for(u32 i = 0; i < s.size; i++ ) - r.data[i] = s.data[s.size - i - 1]; + str r = str_construct(malloc(s.len), s.len, s.isZeroTerminated); + for(u32 i = 0; i < s.len; i++ ) + r.data[i] = s.data[s.len - i - 1]; return r; } 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; - 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++){ - if(j == fragment.size) + if(j == fragment.len) return i; if(src.data[i + j] != fragment.data[j]) 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){ - if(src.size == 0 || fragment.size == 0) + if(src.len == 0 || fragment.len == 0) return -1; - if(startIndex > src.size - 1) - startIndex = src.size - 1; - for(u32 i = startIndex; i >= fragment.size - 1; i--){ + if(startIndex > src.len - 1) + startIndex = src.len - 1; + for(u32 i = startIndex; i >= fragment.len - 1; i--){ for(u32 j = 0;; j++){ - if(j == fragment.size) + if(j == fragment.len) 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; } } @@ -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){ - for(u32 i = startIndex; i < src.size; i++){ + for(u32 i = startIndex; i < src.len; i++){ if(src.data[i] == c) 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){ - if(startIndex > src.size - 1) - startIndex = src.size - 1; + if(startIndex > src.len - 1) + startIndex = src.len - 1; for(u32 i = startIndex; i != (u32)-1; i--){ if(src.data[i] == c) 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){ - if(src.size < fragment.size) + if(src.len < fragment.len) return false; str src_fragment = str_null; src_fragment.data = src.data; - src_fragment.size = fragment.size; + src_fragment.len = fragment.len; return str_equals(src_fragment, fragment); } bool str_endsWith(const str src, const str fragment){ - if(src.size < fragment.size) + if(src.len < fragment.len) return false; str src_fragment = str_null; - src_fragment.data = (char*)(src.data + src.size - fragment.size); - src_fragment.size = fragment.size; + src_fragment.data = (char*)(src.data + src.len - fragment.len); + src_fragment.len = fragment.len; return str_equals(src_fragment, fragment); } u32 str_hash32(const str s){ u8* ubuf = (u8*)s.data; 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]; return hash; } str str_toUpper(const str 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])) 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 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])) 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){ - StringBuilder sb = StringBuilder_alloc(buf.size * 2 + 1); + StringBuilder sb = StringBuilder_alloc(buf.len * 2 + 1); StringBuilder_append_memory(&sb, buf, uppercase); 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){ // loop forward bool stop = false; - while(line->size > 0 && !stop){ + while(line->len > 0 && !stop){ char first_char = line->data[0]; switch(first_char){ case '\0': case '\r': case '\n': case '\t': case ' ': line->data++; - line->size--; + line->len--; break; default: stop = true; @@ -157,13 +157,13 @@ void str_trim(str* line, bool set_zero_at_end){ // loop backward 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){ case '\0': case '\r': case '\n': case '\t': case ' ': - line->size--; + line->len--; break; default: stop = true; @@ -172,7 +172,7 @@ void str_trim(str* line, bool set_zero_at_end){ } if(set_zero_at_end){ - line->data[line->size] = '\0'; + line->data[line->len] = '\0'; line->isZeroTerminated = true; } }