Compare commits

..

No commits in common. "4c97787c27af5b7867c8a1262a3383a84a1d4957" and "b557881168a15797bd3bd3d989cd7d48d3fb0804" have entirely different histories.

50 changed files with 228 additions and 626 deletions

View File

@ -1,62 +1,57 @@
#pragma once #pragma once
#include "../std.h" #include "../std.h"
/* #define Array(T) Array_
Pointer and length.
Can be initialized with {0}.
*/
#define Array(T) Array_##T typedef struct Array_ {
void* data;
u32 size;
} Array_;
/// create Array(T) from array initializer list ({ a, b, c }) /// creates Array_ from self const array
#define ARRAY(T, A...) ((Array(T)){ .data = ((T[])A), .len = sizeof((T[])A) / sizeof(T) }) #define ARRAY(T, A...) Array_construct_size(((T[])A), sizeof((T[])A))
#define Array_declare(T) \ #define Array_construct(DATA, T, COUNT) Array_construct_size(DATA, (COUNT) * sizeof(T))
typedef struct Array(T) { \ #define Array_construct_size(DATA, LEN) ((Array_){ .data = (DATA), .size = (LEN) })
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 struct_castTo_Array_u8(STRUCT_PTR) Array_u8_construct((void*)(STRUCT_PTR), sizeof(*STRUCT_PTR)) #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);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,7 @@
#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 {
@ -8,11 +9,9 @@ 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_ value_list; List(T) value_list;
} HashMapBucket; } HashMapBucket;
#define HashMap(T) HashMap_ #define HashMap(T) HashMap_

View File

@ -2,8 +2,7 @@
#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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,89 +1,44 @@
#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 len; u32 size;
u32 capacity; u32 allocated_size;
u32 elem_t_size;
} List_; } List_;
static inline List_ _List_construct(void* data_ptr, u32 len, u32 capacity, u32 elem_t_size) { #define List_construct(T, DATA_PTR, OCCUPIED_COUNT, ALLOCATED_COUNT) \
return (List_){ List_construct_size(DATA_PTR, (OCCUPIED_COUNT) * sizeof(T), (ALLOCATED_COUNT) * sizeof(T))
.data = data_ptr,
.len = len,
.capacity = capacity,
.elem_t_size = elem_t_size
};
}
static inline void _List_destroy(List_* self){ 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 };
}
#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){
if(!self) if(!self)
return; return;
free(self->data); free(self->data);
} }
List_ _List_alloc(u32 initial_capacity, u32 elem_t_size); List_ List_copy(List_ src);
List_ _List_copy(const List_* src);
/* alloc bigger buffer if size + len_to_add won't fit in current */ // alloc bigger buffer if size + size_to_add won't fit in current
void _List_increaseCapacity(List_* self, u32 len_to_add); void List_increaseCapacity_size(List_* self, u32 size_to_add);
void* _List_expand(List_* self, u32 len_to_add); void* List_expand_size(List_* self, u32 size_to_add);
void _List_push(List_* self, void* values, u32 len); #define List_push(SELF, T, VALUE) *(T*)(List_expand_size(SELF, sizeof(T))) = VALUE
bool _List_tryRemoveAt(List_* self, u32 i, u32 remove_len) ATTRIBUTE_WARN_UNUSED_RESULT; #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)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -9,15 +9,12 @@ 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,7 +4,6 @@
#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)
@ -94,7 +93,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.len, 1); return file_writeStructs(f, src.data, src.size, 1);
} }
@ -113,10 +112,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 .len is the maximum number of bytes to read /// @param dst array where .size 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.len); return file_readStructs(f, dst.data, 1, dst.size);
} }
@ -128,9 +127,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 .len is the exact number of bytes to read /// @param dst array where .size 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.len); return file_readStructsExactly(f, dst.data, 1, dst.size);
} }
/// @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/List_impl/List_char.h" #include "../collections/Array.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_char_alloc(initial_size) }; return (StringBuilder){ .buffer = List_alloc_size(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_char_copy(&b->buffer) }; return (StringBuilder) { .buffer = List_copy(b->buffer) };
} }
// alloc bigger buffer if size + len_to_add won't fit in current // alloc bigger buffer if size + size_to_add won't fit in current
static inline void StringBuilder_increaseCapacity(StringBuilder* b, u32 len_to_add){ static inline void StringBuilder_increaseCapacity(StringBuilder* b, u32 size_to_add){
List_char_increaseCapacity(&b->buffer, len_to_add); List_increaseCapacity_size(&b->buffer, size_to_add);
} }

View File

@ -4,12 +4,10 @@
#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 len; // size of data in bytes without \0 u32 size; // size of data in bytes without \0
bool isZeroTerminated; bool isZeroTerminated;
} str; } str;
@ -18,7 +16,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, .len = LEN, .isZeroTerminated = ZERO_TERMINATED }) #define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .size = 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);
@ -28,18 +26,12 @@ static inline void str_free(str s){
free(s.data); free(s.data);
} }
static inline Array(char) str_castTo_Array_char(str s) { static inline Array_ str_castTo_Array(str s) {
return Array_char_construct(s.data, s.len); return Array_construct_size(s.data, s.size);
}
static inline Array(u8) str_castTo_Array_u8(str s) {
return Array_u8_construct((void*)s.data, s.len);
} }
static inline str Array_char_castTo_str(Array(char) a, bool isZeroTerminated) { static inline str Array_castTo_str(Array_ a, bool isZeroTerminated) {
return str_construct(a.data, a.len, isZeroTerminated); return str_construct(a.data, a.size, 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);
@ -82,5 +74,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.len - n, s.isZeroTerminated); return str_construct(s.data + n, s.size - n, s.isZeroTerminated);
} }

View File

@ -1,47 +0,0 @@
#pragma once
#include "tlibc/errors.h"
typedef struct TerminalSize {
i16 cols;
i16 rows;
} TerminalSize;
typedef enum Color16 {
Color16_Black = 30,
Color16_DarkRed = 31,
Color16_DarkGreen = 32,
Color16_DarkYellow = 33,
Color16_DarkBlue = 34,
Color16_DarkMagenta = 35,
Color16_DarkCyan = 36,
Color16_Gray = 37,
Color16_DarkGray = 90,
Color16_Red = 91,
Color16_Green = 92,
Color16_Yellow = 93,
Color16_Blue = 94,
Color16_Magenta = 95,
Color16_Cyan = 96,
Color16_White = 97
} Color16;
Result(void) term_init();
Result(void) term_getSize(TerminalSize* out);
Result(void) term_readLine(char* buf, u32 bufsize);
Result(void) term_readLineHidden(char *buf, u32 bufsize);
void term_setFgColor16(Color16 c);
void term_setBgColor16(Color16 c);
void term_bold();
void term_italic();
void term_underline();
void term_strikethrough();
void term_resetColors();
void term_clear();
void term_eraseRow();
void term_resetCursor();
void term_cursorMove(u16 row, u16 column);
void term_cursorHide();
void term_cursorShow();

View File

@ -1,6 +1,4 @@
#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
@ -11,14 +9,9 @@
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;
@ -34,9 +27,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);
@ -45,7 +38,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.len * bu->value_list.elem_t_size; u8* end = value_ptr + bu->value_list.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;
@ -75,8 +68,7 @@ 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];
i32 len = r.bu->key_hash_list.len; for(r.i = 0; r.i < (i32)List_len(r.bu->key_hash_list, HashMapKeyHash); r.i++){
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;
@ -99,33 +91,29 @@ 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 < __HashMap_heights.len && "HashMap IS FULL! Fix your code."); assert(height_expanded_n < Array_len(__HashMap_heights, u32) && "HashMap IS FULL! Fix your code.");
// alloc new HashMapBucket array // alloc new HashMapBucket array
u32 height_expanded = __HashMap_heights.data[height_expanded_n]; u32 height_expanded = ((u32*)__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);
// init buckets memset(table_expanded, 0, table_expanded_size);
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 = old_bucket->key_hash_list.data + j; HashMapKeyHash kh = ((HashMapKeyHash*)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(&new_bucket->value_list, old_value_ptr, 1); List_push_size(&new_bucket->value_list, old_value_ptr, self->value_t_size);
} }
List_HashMapKeyHash_destroy(&old_bucket->key_hash_list); free(old_bucket->key_hash_list.data);
_List_destroy(&old_bucket->value_list); free(old_bucket->value_list.data);
} }
free(self->table); free(self->table);
self->table = table_expanded; self->table = table_expanded;
@ -141,14 +129,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 || r.bu->key_hash_list.len >= __HashMapBucket_MAX_LEN){ if(bu == NULL || List_len(bu->key_hash_list, HashMapKeyHash) >= __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_HashMapKeyHash_push(&bu->key_hash_list, kh); List_push(&bu->key_hash_list, HashMapKeyHash, kh);
_List_push(&bu->value_list, value_ptr, 1); List_push_size(&bu->value_list, value_ptr, self->value_t_size);
return true; return true;
} }
@ -163,14 +151,14 @@ void HashMap_pushOrUpdate(HashMap_* self, const str key, void* value_ptr){
} }
HashMapBucket* bu = r.bu; HashMapBucket* bu = r.bu;
if(bu == NULL || r.bu->key_hash_list.len >= __HashMapBucket_MAX_LEN){ if(bu == NULL || List_len(bu->key_hash_list, HashMapKeyHash) >= __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_HashMapKeyHash_push(&bu->key_hash_list, kh); List_push(&bu->key_hash_list, HashMapKeyHash, kh);
_List_push(&bu->value_list, value_ptr, 1); List_push_size(&bu->value_list, value_ptr, self->value_t_size);
} }
bool HashMap_tryDelete(HashMap_* self, const str key){ bool HashMap_tryDelete(HashMap_* self, const str key){
@ -180,11 +168,8 @@ bool HashMap_tryDelete(HashMap_* self, const str key){
if(r.i == -1) if(r.i == -1)
return false; return false;
if(!List_HashMapKeyHash_tryRemoveAt(&r.bu->key_hash_list, r.i, 1)) List_removeAt(&r.bu->key_hash_list, HashMapKeyHash, r.i, 1);
return false; List_removeAt_size(&r.bu->value_list, r.i, self->value_t_size);
if(!_List_tryRemoveAt(&r.bu->value_list, r.i, 1))
return false;
return true; return true;
} }
@ -197,7 +182,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 = bu->key_hash_list.len; i32 bu_elem_len = List_len(bu->key_hash_list, HashMapKeyHash);
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;
@ -208,7 +193,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.len != 0) if(bu->key_hash_list.size != 0)
return true; return true;
} }
} }
@ -221,10 +206,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.len == 0) if(bu->key_hash_list.size == 0)
return false; return false;
kv->key = bu->key_hash_list.data[self->elem_n].key; kv->key = List_index(bu->key_hash_list, HashMapKeyHash, 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,73 +1,55 @@
#include "tlibc/collections/List.h" #include "tlibc/collections/List.h"
#include <assert.h>
List_ _List_alloc(u32 initial_capacity, u32 elem_t_size){ List_ List_alloc_size(u32 initial_size){
assert(elem_t_size != 0); if(initial_size == 0)
if(initial_capacity == 0) return List_null;
return _List_construct(NULL, 0, 0, elem_t_size); u32 allocated_size = ALIGN_TO(initial_size, sizeof(void*));
u32 initial_size = initial_capacity * elem_t_size; return List_construct_size(malloc(allocated_size), 0, allocated_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){
assert(src->elem_t_size != 0); List_ copy = List_alloc_size(src.allocated_size);
List_ copy = _List_alloc(src->capacity, src->elem_t_size); if(copy.data != NULL)
if(copy.data != NULL){ memcpy(copy.data, src.data, src.size);
memcpy(copy.data, src->data, src->len * src->elem_t_size);
}
return copy; return copy;
} }
void _List_increaseCapacity(List_* self, u32 len_to_add){ void List_increaseCapacity_size(List_* self, u32 size_to_add){
assert(self->elem_t_size != 0); u32 occupied_size = self->size;
u32 expanded_size = occupied_size + size_to_add;
u32 expanded_len = self->len + len_to_add; if(self->allocated_size < expanded_size) {
if(self->capacity > expanded_len) u32 expanded_alloc_size = self->allocated_size;
return; if(expanded_alloc_size == 0)
expanded_alloc_size = 32;
u32 expanded_capacity = self->capacity; while(expanded_alloc_size < expanded_size){
if(expanded_capacity == 0) expanded_alloc_size *= 2;
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_capacity * self->elem_t_size); self->data = realloc(self->data, expanded_alloc_size);
self->capacity = expanded_capacity; self->allocated_size = expanded_alloc_size;
}
} }
void* _List_expand(List_* self, u32 len_to_add){ void* List_expand_size(List_* self, u32 size_to_add){
assert(self->elem_t_size != 0); List_increaseCapacity_size(self, size_to_add);
_List_increaseCapacity(self, len_to_add); u8* empty_cell_ptr = (u8*)self->data + self->size;
u8* empty_cell_ptr = (u8*)self->data + self->len * self->elem_t_size; self->size += size_to_add;
self->len += len_to_add;
return empty_cell_ptr; return empty_cell_ptr;
} }
void _List_push(List_* self, void* values, u32 len){ void List_push_size(List_* self, void* values, u32 size){
assert(self->elem_t_size != 0); void* empty_cell_ptr = List_expand_size(self, size);
void* empty_cell_ptr = _List_expand(self, len); memcpy(empty_cell_ptr, values, size);
memcpy(empty_cell_ptr, values, len * self->elem_t_size);
} }
bool _List_tryRemoveAt(List_* self, u32 i, u32 remove_len){ bool List_removeAt_size(List_* self, u32 i, u32 remove_size){
assert(self->elem_t_size != 0); if(i + remove_size >= self->size)
if(i >= self->len)
return false; return false;
if(remove_len + i > self->len) self->size -= remove_size;
remove_len = self->len - i; u8* src = (u8*)self->data + i + remove_size;
u8* dst = (u8*)self->data + i;
u32 move_back_n_elements = self->len - i - remove_len; memmove(dst, src, self->size - i - remove_size);
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_ErrorCallPos_alloc(16); e->call_stack = List_alloc(ErrorCallPos, 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_ErrorCallPos_push(&e->call_stack, p); List_push(&e->call_stack, ErrorCallPos, p);
} }
str Error_toStr(Error* e){ str Error_toStr(Error* e){
u32 len = e->call_stack.len; u32 len = List_len(e->call_stack, ErrorCallPos);
StringBuilder b = StringBuilder_alloc(e->msg.len + 80 * len); StringBuilder b = StringBuilder_alloc(e->msg.size + 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.len, e_str.data); printfe("\n"FMT_str"\n", e_str.size, 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_u8_alloc(f_size); Array(u8) buf = Array_alloc(u8, 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.len == 0) if(path.size == 0)
return path; return path;
// remove trailing slash (name/) // remove trailing slash (name/)
if(path.data[path.len - 1] == path_sep){ if(path.data[path.size - 1] == path_sep){
path.len -= 1; path.size -= 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.len = sepIndex; path.size = 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.len == 0) if(path.size == 0)
return path; return path;
// remove trailing slash (name/) // remove trailing slash (name/)
if(path.data[path.len - 1] == path_sep){ if(path.data[path.size - 1] == path_sep){
path.len -= 1; path.size -= 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.len -= nameIndex; path.size -= 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.len = extIndex; path.size = extIndex;
path.isZeroTerminated = false; path.isZeroTerminated = false;
} }
return path; return path;

View File

@ -8,40 +8,41 @@ void StringBuilder_destroy(StringBuilder* b){
} }
str StringBuilder_getStr(StringBuilder* b){ str StringBuilder_getStr(StringBuilder* b){
if(b->buffer.len == 0 || b->buffer.data[b->buffer.len - 1] != '\0'){ if(b->buffer.size == 0 || ((char*)b->buffer.data)[b->buffer.size - 1] != '\0'){
List_char_push(&b->buffer, '\0'); List_push(&b->buffer, u8, '\0');
// '\0' at the end doesn't increase buffer.len // '\0' at the end doesn't increase buffer.size
b->buffer.len -= 1; b->buffer.size -= 1;
} }
str result = str_construct(b->buffer.data, b->buffer.len, true); str result = str_construct((char*)b->buffer.data, b->buffer.size, 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(a->buffer.data, a->buffer.len, false); str a_str = str_construct((char*)a->buffer.data, a->buffer.size, false);
str b_str = str_construct(b->buffer.data, b->buffer.len, false); str b_str = str_construct((char*)b->buffer.data, b->buffer.size, 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.len){ if(count < b->buffer.size){
b->buffer.len -= count; b->buffer.size -= count;
} }
else{ else{
b->buffer.len = 0; b->buffer.size = 0;
} }
} }
void StringBuilder_append_char(StringBuilder* b, char c){ void StringBuilder_append_char(StringBuilder* b, char c){
List_char_push(&b->buffer, c); List_push(&b->buffer, u8, 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_char_pushMany(&b->buffer, s.data, s.len); List_push_size(&b->buffer, s.data, s.size);
} }
void StringBuilder_append_cstr(StringBuilder* b, cstr s){ void StringBuilder_append_cstr(StringBuilder* b, cstr s){
@ -77,7 +78,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.len; i++) { for (u32 i=0; i < mem.size; 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.len == 0) if(self.data == NULL || self.size == 0)
return self; return self;
str copy = str_construct((char*)malloc(self.len + 1), self.len, true); str copy = str_construct((char*)malloc(self.size + 1), self.size, true);
memcpy(copy.data, self.data, self.len); memcpy(copy.data, self.data, self.size);
copy.data[copy.len] = '\0'; copy.data[copy.size] = '\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.len != other.len) if(self.size != other.size)
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.len) == 0; return memcmp(self.data, other.data, self.size) == 0;
} }
str str_reverse(str s){ str str_reverse(str s){
if(s.data == NULL || s.len == 0) if(s.data == NULL || s.size == 0)
return s; return s;
str r = str_construct(malloc(s.len), s.len, s.isZeroTerminated); str r = str_construct(malloc(s.size), s.size, s.isZeroTerminated);
for(u32 i = 0; i < s.len; i++ ) for(u32 i = 0; i < s.size; i++ )
r.data[i] = s.data[s.len - i - 1]; r.data[i] = s.data[s.size - 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.len == 0 || fragment.len == 0) if(src.size == 0 || fragment.size == 0)
return -1; return -1;
for(u32 i = startIndex; i < src.len - fragment.len + 1; i++){ for(u32 i = startIndex; i < src.size - fragment.size + 1; i++){
for(u32 j = 0;; j++){ for(u32 j = 0;; j++){
if(j == fragment.len) if(j == fragment.size)
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.len == 0 || fragment.len == 0) if(src.size == 0 || fragment.size == 0)
return -1; return -1;
if(startIndex > src.len - 1) if(startIndex > src.size - 1)
startIndex = src.len - 1; startIndex = src.size - 1;
for(u32 i = startIndex; i >= fragment.len - 1; i--){ for(u32 i = startIndex; i >= fragment.size - 1; i--){
for(u32 j = 0;; j++){ for(u32 j = 0;; j++){
if(j == fragment.len) if(j == fragment.size)
return i - j + 1; return i - j + 1;
if(src.data[i - j] != fragment.data[fragment.len - 1 - j]) if(src.data[i - j] != fragment.data[fragment.size - 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.len; i++){ for(u32 i = startIndex; i < src.size; 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.len - 1) if(startIndex > src.size - 1)
startIndex = src.len - 1; startIndex = src.size - 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.len < fragment.len) if(src.size < fragment.size)
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.len = fragment.len; src_fragment.size = fragment.size;
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.len < fragment.len) if(src.size < fragment.size)
return false; return false;
str src_fragment = str_null; str src_fragment = str_null;
src_fragment.data = (char*)(src.data + src.len - fragment.len); src_fragment.data = (char*)(src.data + src.size - fragment.size);
src_fragment.len = fragment.len; src_fragment.size = fragment.size;
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.len; i++) for (u32 i = 0; i < s.size; 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.len; i++){ for (u32 i = 0; i < r.size; 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.len; i++){ for (u32 i = 0; i < r.size; 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.len * 2 + 1); StringBuilder sb = StringBuilder_alloc(buf.size * 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->len > 0 && !stop){ while(line->size > 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->len--; line->size--;
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->len > 0 && !stop) while(line->size > 0 && !stop)
{ {
char last_char = line->data[line->len - 1]; char last_char = line->data[line->size - 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->len--; line->size--;
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->len] = '\0'; line->data[line->size] = '\0';
line->isZeroTerminated = true; line->isZeroTerminated = true;
} }
} }

View File

@ -1,189 +0,0 @@
#include "tlibc/term.h"
#if defined(_WIN64) || defined(_WIN32)
#include <windows.h>
#define try_win_bool(EXPR) if(!(EXPR)) { Return RESULT_ERROR_FMT(#EXPR " failed with WindowsError %lu", GetLastError()); }
#else
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#define try_zero_errno(EXPR) if((EXPR) != 0) { \
char* errno_s = strerror_malloc(errno); \
ResultVar(void) err = RESULT_ERROR_FMT(#EXPR " failed with errno: %s", strerror(errno)); \
free(errno_s); \
Return err; \
}
#endif
Result(void) term_init(){
Deferral(8);
#if defined(_WIN64) || defined(_WIN32)
DWORD mode = 0;
HANDLE console_handle;
// configure stdout
console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
try_assert(console_handle != INVALID_HANDLE_VALUE);
GetConsoleMode(console_handle, &mode);
// https://learn.microsoft.com/en-us/windows/console/setconsolemode
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
mode |= ENABLE_PROCESSED_OUTPUT;
mode |= DISABLE_NEWLINE_AUTO_RETURN;
SetConsoleMode(console_handle, mode);
// configure stderr
console_handle = GetStdHandle(STD_ERROR_HANDLE);
try_assert(console_handle != INVALID_HANDLE_VALUE);
GetConsoleMode(console_handle, &mode);
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
mode |= ENABLE_PROCESSED_OUTPUT;
mode |= DISABLE_NEWLINE_AUTO_RETURN;
SetConsoleMode(console_handle, mode);
// configure stdin
console_handle = GetStdHandle(STD_INPUT_HANDLE);
try_assert(console_handle != INVALID_HANDLE_VALUE);
GetConsoleMode(console_handle, &mode);
mode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
SetConsoleMode(console_handle, mode);
#endif
Return RESULT_VOID;
}
i64 getenv_int(const char* var_name){
char* s = getenv(var_name);
if(s == NULL)
return -1;
return strtoll(s, NULL, 0);
}
Result(void) term_getSize(TerminalSize* out) {
Deferral(4);
#if defined(_WIN64) || defined(_WIN32)
// helps when STD_OUT is redirected to a file
HANDLE console_handle_stderr = GetStdHandle(STD_ERROR_HANDLE);
try_assert(console_handle_stderr != INVALID_HANDLE_VALUE)
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
try_win_bool(GetConsoleScreenBufferInfo(console_handle_stderr, &consoleInfo));
out->cols = consoleInfo.srWindow.Right - consoleInfo.srWindow.Left + 1;
out->rows = consoleInfo.srWindow.Bottom - consoleInfo.srWindow.Top + 1;
#else
struct winsize ws = {0};
// try to get terminal size from stdin, stdout, stderr
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0 ||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 ||
ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == 0 ){
out->cols = ws.ws_col;
out->rows = ws.ws_row;
}
// try to get size from environtent variables
else {
out->cols = getenv_int("COLUMNS");
out->rows = getenv_int("LINES");
}
#endif
try_assert(out->cols > 0);
try_assert(out->rows > 0);
Return RESULT_VOID;
}
Result(void) term_readLine(char* buf, u32 bufsize) {
Deferral(1);
if(fgets(buf, bufsize, stdin) == NULL){
try_stderrcode(ferror(stdin));
}
Return RESULT_VOID;
}
Result(void) term_readLineHidden(char *buf, u32 bufsize) {
Deferral(4);
#if defined(_WIN64) || defined(_WIN32)
HANDLE console_handle_stdin = GetStdHandle(STD_INPUT_HANDLE);
try_assert(console_handle_stdin != INVALID_HANDLE_VALUE);
DWORD old_mode;
GetConsoleMode(console_handle_stdin, &old_mode);
// turn off echo
DWORD new_mode = old_mode & ~(ENABLE_ECHO_INPUT);
SetConsoleMode(console_handle_stdin, new_mode);
// restore echo
Defer(SetConsoleMode(console_handle_stdin, old_mode));
#else
struct termios old_mode, new_mode;
try_zero_errno(tcgetattr(STDIN_FILENO, &old_mode));
new_mode = old_mode;
// turn off echo
new_mode.c_lflag &= ~(ECHO);
try_zero_errno(tcsetattr(STDIN_FILENO, TCSAFLUSH, &new_mode));
// restore echo
Defer(tcsetattr(STDIN_FILENO, TCSAFLUSH, &old_mode));
#endif
// read line
try_void(term_readLine(buf, bufsize));
fputc('\n', stdout);
Return RESULT_VOID;
}
/*
Most of escape sequences can be found there
https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
*/
#define ESC "\x1b"
#define CSI ESC"["
void term_setFgColor16(Color16 c){
printf(CSI"%um", c);
}
void term_setBgColor16(Color16 c){
printf(CSI"%um", c + 10);
}
void term_bold(){
printf(CSI"1m");
}
void term_italic(){
printf(CSI"3m");
}
void term_underline(){
printf(CSI"4m");
}
void term_strikethrough(){
printf(CSI"9m");
}
void term_resetCursor() {
printf(CSI"H");
}
void term_resetColors() {
printf(CSI"0m");
}
void term_clear() {
printf(CSI"0m" CSI"H" CSI"2J");
}
void term_eraseRow(){
printf(CSI"2K\r");
}
void term_cursorMove(u16 row, u16 column) {
printf(CSI"%u;%uH", row, column);
}
void term_cursorHide() {
printf(CSI"?25l");
}
void term_cursorShow() {
printf(CSI"?25h");
}