Compare commits

...

4 Commits

Author SHA1 Message Date
ebe6e58ef3 added StringBuilder_append_memory 2025-07-19 04:41:26 +03:00
3fb4d03f1b string-array cast 2025-07-19 04:41:07 +03:00
c0c845bee8 renamed some functions 2025-07-19 03:38:33 +03:00
f7b0b53d05 fixed Array marcos 2025-07-19 03:02:36 +03:00
6 changed files with 34 additions and 14 deletions

View File

@@ -7,12 +7,12 @@ typedef struct Array {
} Array; } Array;
/// creates Array from a const array /// creates Array from a const array
#define Array_CONST(T, A...) Array_construct_size(((T[])A), sizeof((T[])A)) #define ARRAY(T, A...) Array_construct_size(((T[])A), sizeof((T[])A))
#define Array_construct(DATA, T, COUNT) Array_construct_size(DATA, (COUNT) * 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_construct_size(DATA, LEN) ((Array){ .data = (DATA), .size = (LEN) })
#define Array_alloc(T, COUNT) Array_alloc((COUNT) * sizeof(T)) #define Array_alloc(T, COUNT) Array_alloc_size((COUNT) * sizeof(T))
static inline Array Array_alloc_size(u32 size){ static inline Array Array_alloc_size(u32 size){
return Array_construct_size(malloc(size), size); return Array_construct_size(malloc(size), size);
@@ -26,3 +26,7 @@ static inline void Array_realloc_size(Array* ar, u32 new_size){
} }
#define Array_len(AR, T) ((AR)->size / sizeof(T)) #define Array_len(AR, T) ((AR)->size / sizeof(T))
#define Array_memset(A, VAL) memset((A)->data, VAL, (A)->size)
#define str_castTo_Array(S) Array_construct_size((S).data, (S).size)
#define Array_castTo_str(S, IS_ZERO_TERMINATED) str_construct((S).data, (S).size, IS_ZERO_TERMINATED)

View File

@@ -24,8 +24,8 @@ typedef struct HashMap {
u16 height_n; u16 height_n;
} HashMap; } HashMap;
void HashMap_alloc(HashMap* ptr, u32 value_t_size, FreeFunction NULLABLE(value_destructor)); void HashMap_create(HashMap* ptr, u32 value_t_size, FreeFunction NULLABLE(value_destructor));
void HashMap_free(HashMap* ptr); void HashMap_destroy(HashMap* ptr);
void* NULLABLE(HashMap_tryGetPtr)(HashMap* ptr, str key); void* NULLABLE(HashMap_tryGetPtr)(HashMap* ptr, str key);
bool HashMap_tryPush(HashMap* ptr, str key, void* value_ptr); bool HashMap_tryPush(HashMap* ptr, str key, void* value_ptr);
bool HashMap_tryDelete(HashMap* ptr, str key); bool HashMap_tryDelete(HashMap* ptr, str key);

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "../collections/List.h" #include "../collections/List.h"
#include "../collections/Array.h"
#include "str.h" #include "str.h"
typedef struct StringBuilder { typedef struct StringBuilder {
@@ -10,16 +11,17 @@ typedef struct StringBuilder {
static inline StringBuilder StringBuilder_alloc(u32 initial_size) { static inline StringBuilder StringBuilder_alloc(u32 initial_size) {
return (StringBuilder){ .buffer = List_alloc_size(initial_size) }; return (StringBuilder){ .buffer = List_alloc_size(initial_size) };
} }
void StringBuilder_free(StringBuilder* b); void StringBuilder_destroy(StringBuilder* b);
/// @param count set to -1 to clear StringBuilder /// @param count set to -1 to clear StringBuilder
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count); void StringBuilder_removeFromEnd(StringBuilder* b, u32 count);
void StringBuilder_append_char(StringBuilder* b, char c); void StringBuilder_append_char(StringBuilder* b, char c);
void StringBuilder_append_cstr(StringBuilder* b, char* s); void StringBuilder_append_cstr(StringBuilder* b, char* s);
void StringBuilder_append_string(StringBuilder* b, str s); void StringBuilder_append_str(StringBuilder* b, str s);
void StringBuilder_append_i64(StringBuilder* b, i64 a); void StringBuilder_append_i64(StringBuilder* b, i64 a);
void StringBuilder_append_u64(StringBuilder* b, u64 a); void StringBuilder_append_u64(StringBuilder* b, u64 a);
void StringBuilder_append_f64(StringBuilder* b, f64 a); void StringBuilder_append_f64(StringBuilder* b, f64 a);
void StringBuilder_append_memory(StringBuilder* b, Array mem);
// adds '\0' to the buffer and returns pointer to buffer content // adds '\0' to the buffer and returns pointer to buffer content
str StringBuilder_getStr(StringBuilder* b); str StringBuilder_getStr(StringBuilder* b);

View File

@@ -6,7 +6,7 @@
typedef struct str { typedef struct str {
char* data; char* data;
u32 size; u32 size; // size of data in bytes without \0
bool isZeroTerminated; bool isZeroTerminated;
} str; } str;

View File

@@ -6,13 +6,13 @@
#define __HashMap_HASH_FUNC str_hash32 #define __HashMap_HASH_FUNC str_hash32
#define __HashMapBucket_MAX_LEN 16 #define __HashMapBucket_MAX_LEN 16
static const Array __HashMap_heights = Array_CONST(u32, { static const Array __HashMap_heights = ARRAY(u32, {
17, 31, 61, 127, 257, 521, 1021, 2053, 4099, 8191, 16381, 32771, 17, 31, 61, 127, 257, 521, 1021, 2053, 4099, 8191, 16381, 32771,
65521, 131071, 262147, 524287, 1048583, 2097169, 4194319, 65521, 131071, 262147, 524287, 1048583, 2097169, 4194319,
8388617, 16777213, 33554467, 67108859, 134217757, 268435493 8388617, 16777213, 33554467, 67108859, 134217757, 268435493
}); });
void HashMap_alloc(HashMap* ptr, u32 value_t_size, FreeFunction NULLABLE(value_destructor)){ void HashMap_create(HashMap* ptr, u32 value_t_size, FreeFunction NULLABLE(value_destructor)){
ptr->value_t_size = value_t_size; ptr->value_t_size = value_t_size;
ptr->value_destructor = value_destructor; ptr->value_destructor = value_destructor;
ptr->height_n = 0; ptr->height_n = 0;
@@ -22,7 +22,7 @@ void HashMap_alloc(HashMap* ptr, u32 value_t_size, FreeFunction NULLABLE(value_d
memset(ptr->table, 0, alloc_size); memset(ptr->table, 0, alloc_size);
} }
void HashMap_free(HashMap* ptr){ void HashMap_destroy(HashMap* ptr){
for(u32 i = 0; i < ptr->height; i++){ for(u32 i = 0; i < ptr->height; i++){
HashMapBucket* bu = &ptr->table[i]; HashMapBucket* bu = &ptr->table[i];
u32 len = List_len(&bu->key_hash_list, KeyHash); u32 len = List_len(&bu->key_hash_list, KeyHash);

View File

@@ -1,6 +1,6 @@
#include "string/StringBuilder.h" #include "string/StringBuilder.h"
void StringBuilder_free(StringBuilder* b){ void StringBuilder_destroy(StringBuilder* b){
free(b->buffer.data); free(b->buffer.data);
b->buffer = List_construct_size(NULL, 0, 0); b->buffer = List_construct_size(NULL, 0, 0);
} }
@@ -26,12 +26,15 @@ void StringBuilder_append_char(StringBuilder* b, char c){
} }
void StringBuilder_append_string(StringBuilder* b, str s){ void StringBuilder_append_str(StringBuilder* b, str s){
if(s.data == NULL)
return;
List_push_size(&b->buffer, s.data, s.size); List_push_size(&b->buffer, s.data, s.size);
} }
void StringBuilder_append_cstr(StringBuilder* b, char* s){ void StringBuilder_append_cstr(StringBuilder* b, char* s){
StringBuilder_append_string(b, str_construct(s, strlen(s), true)); StringBuilder_append_str(b, str_construct(s, strlen(s), true));
} }
void StringBuilder_append_i64(StringBuilder* b, i64 n){ void StringBuilder_append_i64(StringBuilder* b, i64 n){
@@ -51,3 +54,14 @@ void StringBuilder_append_f64(StringBuilder* b, f64 n){
sprintf(buf, "%lf", n); sprintf(buf, "%lf", n);
StringBuilder_append_cstr(b, buf); StringBuilder_append_cstr(b, buf);
} }
void StringBuilder_append_memory(StringBuilder* b, Array mem) {
if(mem.data == NULL)
return;
char buf[8];
for (u32 i=0; i < mem.size; i++) {
sprintf(buf, "%02x", ((u8*)mem.data)[i]);
StringBuilder_append_str(b, str_construct(buf, 2, true));
}
}