From be154db6cffd2fdb4c8ab006963a1fcaab5ca8bf Mon Sep 17 00:00:00 2001 From: Francesco Date: Fri, 5 Sep 2025 21:15:27 +0200 Subject: [PATCH] refactor: improve string methods --- examples/hashmap/hm1.c | 2 +- examples/queue/q1.c | 2 +- examples/string/str2.c | 31 ++++++++++++++++ hashmap/myhashmap.c | 2 +- hashmap/myhashmap.h | 2 +- meson.build | 2 +- queue/myqueue.c | 2 +- queue/myqueue.h | 2 +- string/mystring.c | 81 ++++++++++++++++++++++++++++++++++++------ string/mystring.h | 16 +++++++-- 10 files changed, 122 insertions(+), 20 deletions(-) create mode 100644 examples/string/str2.c diff --git a/examples/hashmap/hm1.c b/examples/hashmap/hm1.c index 26d02b8..6d2af41 100644 --- a/examples/hashmap/hm1.c +++ b/examples/hashmap/hm1.c @@ -51,7 +51,7 @@ int main(void) { /* This hashmap will contain names as keys and a custom type as value */ size_t key_size = sizeof(char) * MAX_STR_LEN; size_t value_size = sizeof(int) + sizeof(char) * MAX_STR_LEN; - mcl_hashmap_s *map = mcl_hm_init(my_hash_func, my_equal_fun, my_free_key, my_free_value, key_size, value_size); + mcl_hashmap_s *map = mcl_hm_new(my_hash_func, my_equal_fun, my_free_key, my_free_value, key_size, value_size); /* Set a new value */ struct my_custom_type p1 = { diff --git a/examples/queue/q1.c b/examples/queue/q1.c index 7027027..1671829 100644 --- a/examples/queue/q1.c +++ b/examples/queue/q1.c @@ -5,7 +5,7 @@ int main(void) { /* Allocate a new queue */ /* Always remember to check return values */ - mcl_queue_s *queue = mcl_queue_init(3, sizeof(int)); + mcl_queue_s *queue = mcl_queue_new(3, sizeof(int)); int val, out; diff --git a/examples/string/str2.c b/examples/string/str2.c new file mode 100644 index 0000000..a2a8823 --- /dev/null +++ b/examples/string/str2.c @@ -0,0 +1,31 @@ +#include + +#include "../../string/mystring.h" + +int main(void) { + mcl_string_s *s1 = mcl_string_new("Hello, world!", 0); + mcl_string_s *s2 = mcl_string_new("Hello, world!", 0); + + /* Dont' call mcl_string_cstr() more than once in the same printf function */ + printf("s1: %s\n", mcl_string_cstr(s1)); + printf("s2: %s\n", mcl_string_cstr(s2)); + + int ret = mcl_string_compare(s1, s2); + if (ret == 0) { + printf("Same string!\n"); + } + + mcl_string_clear(s1); + if (mcl_string_compare(s1, s2) != 0) { + printf("Not the same!\n"); + } + + mcl_string_toupper(s1); + mcl_string_tolower(s2); + + printf("s1: %s\n", mcl_string_cstr(s1)); + printf("s2: %s\n", mcl_string_cstr(s2)); + + mcl_string_free(s1); + mcl_string_free(s2); +} diff --git a/hashmap/myhashmap.c b/hashmap/myhashmap.c index 49bad88..5b5ba80 100644 --- a/hashmap/myhashmap.c +++ b/hashmap/myhashmap.c @@ -50,7 +50,7 @@ static mcl_bucket_s *mcl_find_bucket(mcl_hashmap_s *hashmap, void *key, mcl_buck return NULL; } -mcl_hashmap_s *mcl_hm_init(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size) { +mcl_hashmap_s *mcl_hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size) { mcl_hashmap_s *hashmap = malloc(sizeof(mcl_hashmap_s)); if (hashmap == NULL) { return NULL; diff --git a/hashmap/myhashmap.h b/hashmap/myhashmap.h index a14317b..891fa4d 100644 --- a/hashmap/myhashmap.h +++ b/hashmap/myhashmap.h @@ -83,7 +83,7 @@ typedef struct mcl_hashmap { * @param[in] value_size Size in bytes of each value to be stored * @return A pointer to the newly initialized hash map, or NULL on failure */ -mcl_hashmap_s *mcl_hm_init(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size); +mcl_hashmap_s *mcl_hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size); /** * @brief Free all resources used by the hash map diff --git a/meson.build b/meson.build index f005a9c..b1a46fa 100644 --- a/meson.build +++ b/meson.build @@ -6,7 +6,7 @@ project( ) string = files( - 'examples/string/str1.c', + 'examples/string/str2.c', 'string/mystring.c', ) diff --git a/queue/myqueue.c b/queue/myqueue.c index 16951e3..6ed331f 100644 --- a/queue/myqueue.c +++ b/queue/myqueue.c @@ -3,7 +3,7 @@ #include #include -mcl_queue_s *mcl_queue_init(size_t queue_size, size_t elem_size) { +mcl_queue_s *mcl_queue_new(size_t queue_size, size_t elem_size) { mcl_queue_s *queue = malloc(sizeof(mcl_queue_s)); if (queue == NULL) { return NULL; diff --git a/queue/myqueue.h b/queue/myqueue.h index 88ccb47..d59ec64 100644 --- a/queue/myqueue.h +++ b/queue/myqueue.h @@ -24,7 +24,7 @@ typedef struct mcl_queue { * @param elem_size Size in bytes of each element. * @return Pointer to the new queue, or NULL on failure. */ -mcl_queue_s *mcl_queue_init(size_t queue_size, size_t elem_size); +mcl_queue_s *mcl_queue_new(size_t queue_size, size_t elem_size); /** * @brief Add an element to the queue. diff --git a/string/mystring.c b/string/mystring.c index 8c2e43e..a11b6a9 100644 --- a/string/mystring.c +++ b/string/mystring.c @@ -4,6 +4,7 @@ #include #include #include +#include /* Initialize Thread-Specific Data Keys */ static tss_t buffer_key; @@ -134,6 +135,12 @@ int mcl_string_append(mcl_string_s *string, const char *text) { return 0; } +int mcl_string_extend(mcl_string_s *destination, mcl_string_s *source) { + /* TODO */ + + return 0; +} + void mcl_string_free(mcl_string_s *string) { if (string == NULL) { return; @@ -238,18 +245,72 @@ char *mcl_string_cstr(mcl_string_s *string) { return tb->buf; } -int mcl_string_compare(mcl_string_s *s1, mcl_string_s *s2) { return strcmp(s1->data, s2->data); } - -void mcl_string_clear(mcl_string_s *string) { memset(string->data, 0, mcl_string_length(string)); } - -void mcl_string_to_upper(mcl_string_s *string) { - for (size_t i = 0; i < mcl_string_length(string); ++i) { - string->data[i] = toupper(string->data[i]); +int mcl_string_compare(mcl_string_s *s1, mcl_string_s *s2) { + if (s1 == NULL || s2 == NULL) { + return -123; } + + if (mtx_lock(&s1->lock) != thrd_success) { + return -123; + } + + if (mtx_lock(&s2->lock) != thrd_success) { + mtx_unlock(&s1->lock); + + return -123; + } + + int ret = strcmp(s1->data, s2->data); + + mtx_unlock(&s1->lock); + mtx_unlock(&s2->lock); + + return ret; } -void mcl_string_to_lower(mcl_string_s *string) { - for (size_t i = 0; i < mcl_string_length(string); ++i) { - string->data[i] = tolower(string->data[i]); +void mcl_string_clear(mcl_string_s *string) { + if (string == NULL) { + return; } + + if (mtx_lock(&string->lock) != thrd_success) { + return; + } + + memset(string->data, 0, string->size); + string->size = 0; + + mtx_unlock(&string->lock); +} + +void mcl_string_toupper(mcl_string_s *string) { + if (string == NULL) { + return; + } + + if (mtx_lock(&string->lock) != thrd_success) { + return; + } + + for (size_t i = 0; i < string->size; ++i) { + string->data[i] = (char)toupper((unsigned char)string->data[i]); + } + + mtx_unlock(&string->lock); +} + +void mcl_string_tolower(mcl_string_s *string) { + if (string == NULL) { + return; + } + + if (mtx_lock(&string->lock) != thrd_success) { + return; + } + + for (size_t i = 0; i < string->size; ++i) { + string->data[i] = (char)tolower((unsigned char)string->data[i]); + } + + mtx_unlock(&string->lock); } diff --git a/string/mystring.h b/string/mystring.h index a20c40c..aa2e192 100644 --- a/string/mystring.h +++ b/string/mystring.h @@ -39,6 +39,15 @@ void mcl_string_free(mcl_string_s *string); */ int mcl_string_append(mcl_string_s *string, const char *text); +/** + * @brief Extend by adding another string. + * + * @param destination Destination string. + * @param source Source string. + * @return 0 on success, -1 on failure. + */ +int mcl_string_extend(mcl_string_s *destination, mcl_string_s *source); + /** * @brief Clear the string content without freeing the memory. * @@ -69,6 +78,7 @@ size_t mcl_string_capacity(mcl_string_s *string); * @return Pointer to a thread-local buffer, or NULL on failure. * * @note Valid until the next call in the same thread. Do NOT free the returned pointer. + * Do NOT call more than once this function in a print function. */ char *mcl_string_cstr(mcl_string_s *string); @@ -77,7 +87,7 @@ char *mcl_string_cstr(mcl_string_s *string); * * @param s1 First string. * @param s2 Second string. - * @return Same as strcmp(). + * @return -123 on failure or same as strcmp(). */ int mcl_string_compare(mcl_string_s *s1, mcl_string_s *s2); @@ -86,13 +96,13 @@ int mcl_string_compare(mcl_string_s *s1, mcl_string_s *s2); * * @param string String to modify. */ -void mcl_string_to_upper(mcl_string_s *string); +void mcl_string_toupper(mcl_string_s *string); /** * @brief Convert the string to lowercase. * * @param string String to modify. */ -void mcl_string_to_lower(mcl_string_s *string); +void mcl_string_tolower(mcl_string_s *string); #endif /* MYCLIB_STRING_H */