refactor: improve string methods
This commit is contained in:
@@ -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 = {
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
31
examples/string/str2.c
Normal file
31
examples/string/str2.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -6,7 +6,7 @@ project(
|
||||
)
|
||||
|
||||
string = files(
|
||||
'examples/string/str1.c',
|
||||
'examples/string/str2.c',
|
||||
'string/mystring.c',
|
||||
)
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <threads.h>
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
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]);
|
||||
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_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);
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user