refactor: move to mcl_string_s
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
#ifndef MYCLIB_HASHMAP_H
|
#ifndef MYCLIB_HASHMAP_H
|
||||||
#define MYCLIB_HASHMAP_H
|
#define MYCLIB_HASHMAP_H
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <threads.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
@@ -13,11 +13,11 @@
|
|||||||
* Each bucket can hold one key-value pair and points to the next bucket
|
* Each bucket can hold one key-value pair and points to the next bucket
|
||||||
* in case of hash collisions (separate chaining).
|
* in case of hash collisions (separate chaining).
|
||||||
*/
|
*/
|
||||||
typedef struct mcl_bucket_t {
|
typedef struct mcl_bucket {
|
||||||
void *key; /**< Pointer to the key */
|
void *key; /**< Pointer to the key */
|
||||||
void *value; /**< Pointer to the value */
|
void *value; /**< Pointer to the value */
|
||||||
struct mcl_bucket_t *next; /**< Pointer to the next bucket in case of collision */
|
struct mcl_bucket *next; /**< Pointer to the next bucket in case of collision */
|
||||||
} mcl_bucket;
|
} mcl_bucket_s;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Function pointer type for a hash function
|
* @brief Function pointer type for a hash function
|
||||||
|
|||||||
@@ -40,12 +40,12 @@ size_t mcl_next_power_two(size_t len) {
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
mcl_string *mcl_string_new(const char *text, size_t initial_capacity) {
|
mcl_string_s *mcl_string_new(const char *text, size_t initial_capacity) {
|
||||||
if (text == NULL) {
|
if (text == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
mcl_string *str = malloc(sizeof(mcl_string));
|
mcl_string_s *str = malloc(sizeof(mcl_string_s));
|
||||||
if (str == NULL) {
|
if (str == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -88,7 +88,7 @@ mcl_string *mcl_string_new(const char *text, size_t initial_capacity) {
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mcl_string_append(mcl_string *string, const char *text) {
|
int mcl_string_append(mcl_string_s *string, const char *text) {
|
||||||
if (string == NULL || text == NULL) {
|
if (string == NULL || text == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -133,7 +133,7 @@ int mcl_string_append(mcl_string *string, const char *text) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mcl_string_free(mcl_string *string) {
|
void mcl_string_free(mcl_string_s *string) {
|
||||||
if (string == NULL) {
|
if (string == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -147,7 +147,7 @@ void mcl_string_free(mcl_string *string) {
|
|||||||
free(string);
|
free(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t mcl_string_length(mcl_string *string) {
|
size_t mcl_string_length(mcl_string_s *string) {
|
||||||
if (string == NULL) {
|
if (string == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -163,7 +163,7 @@ size_t mcl_string_length(mcl_string *string) {
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t mcl_string_capacity(mcl_string *string) {
|
size_t mcl_string_capacity(mcl_string_s *string) {
|
||||||
if (string == NULL) {
|
if (string == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -179,7 +179,7 @@ size_t mcl_string_capacity(mcl_string *string) {
|
|||||||
return cap;
|
return cap;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *mcl_string_cstr(mcl_string *string) {
|
char *mcl_string_cstr(mcl_string_s *string) {
|
||||||
if (string == NULL || string->data == NULL) {
|
if (string == NULL || string->data == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ typedef struct mcl_string {
|
|||||||
size_t size; /**< Current length of the string (excluding null terminator) */
|
size_t size; /**< Current length of the string (excluding null terminator) */
|
||||||
size_t capacity; /**< Allocated capacity including null terminator */
|
size_t capacity; /**< Allocated capacity including null terminator */
|
||||||
mtx_t lock; /**< Mutex for thread safety */
|
mtx_t lock; /**< Mutex for thread safety */
|
||||||
} mcl_string;
|
} mcl_string_s;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Create a new string initialized with the given text.
|
* @brief Create a new string initialized with the given text.
|
||||||
@@ -24,7 +24,7 @@ typedef struct mcl_string {
|
|||||||
*
|
*
|
||||||
* @note Caller must release the string using mcl_string_free().
|
* @note Caller must release the string using mcl_string_free().
|
||||||
*/
|
*/
|
||||||
mcl_string *mcl_string_new(const char *text, size_t initial_capacity);
|
mcl_string_s *mcl_string_new(const char *text, size_t initial_capacity);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Append text to the string.
|
* @brief Append text to the string.
|
||||||
@@ -35,7 +35,7 @@ mcl_string *mcl_string_new(const char *text, size_t initial_capacity);
|
|||||||
*
|
*
|
||||||
* @note On failure, the original string remains unchanged.
|
* @note On failure, the original string remains unchanged.
|
||||||
*/
|
*/
|
||||||
int mcl_string_append(mcl_string *string, const char *text);
|
int mcl_string_append(mcl_string_s *string, const char *text);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Free the string and its resources.
|
* @brief Free the string and its resources.
|
||||||
@@ -44,7 +44,7 @@ int mcl_string_append(mcl_string *string, const char *text);
|
|||||||
*
|
*
|
||||||
* @note Caller must ensure no other thread is concurrently using this mcl_string.
|
* @note Caller must ensure no other thread is concurrently using this mcl_string.
|
||||||
*/
|
*/
|
||||||
void mcl_string_free(mcl_string *string);
|
void mcl_string_free(mcl_string_s *string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the current length of the string.
|
* @brief Get the current length of the string.
|
||||||
@@ -52,7 +52,7 @@ void mcl_string_free(mcl_string *string);
|
|||||||
* @param string String to query
|
* @param string String to query
|
||||||
* @return Length of the string (excluding null terminator), or 0 if NULL
|
* @return Length of the string (excluding null terminator), or 0 if NULL
|
||||||
*/
|
*/
|
||||||
size_t mcl_string_length(mcl_string *string);
|
size_t mcl_string_length(mcl_string_s *string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the total allocated capacity of the string buffer.
|
* @brief Get the total allocated capacity of the string buffer.
|
||||||
@@ -60,7 +60,7 @@ size_t mcl_string_length(mcl_string *string);
|
|||||||
* @param string String to query
|
* @param string String to query
|
||||||
* @return Capacity (in bytes, including null terminator), or 0 if NULL
|
* @return Capacity (in bytes, including null terminator), or 0 if NULL
|
||||||
*/
|
*/
|
||||||
size_t mcl_string_capacity(mcl_string *string);
|
size_t mcl_string_capacity(mcl_string_s *string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get a pointer to a null-terminated C-string representing the content.
|
* @brief Get a pointer to a null-terminated C-string representing the content.
|
||||||
@@ -71,6 +71,6 @@ size_t mcl_string_capacity(mcl_string *string);
|
|||||||
* @note The returned pointer is valid until the next call to mcl_string_cstr()
|
* @note The returned pointer is valid until the next call to mcl_string_cstr()
|
||||||
* in the same thread or until the thread exits. **Do NOT free** the returned pointer.
|
* in the same thread or until the thread exits. **Do NOT free** the returned pointer.
|
||||||
*/
|
*/
|
||||||
char *mcl_string_cstr(mcl_string *string);
|
char *mcl_string_cstr(mcl_string_s *string);
|
||||||
|
|
||||||
#endif /* MYCLIB_STRING_H */
|
#endif /* MYCLIB_STRING_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user