refactor(string): remove mcl namespace
This commit is contained in:
@@ -43,12 +43,12 @@ static size_t next_power_two(size_t len) {
|
||||
return p;
|
||||
}
|
||||
|
||||
mcl_string_s *mcl_string_new(const char *text, size_t initial_capacity) {
|
||||
string_s *string_new(const char *text, size_t initial_capacity) {
|
||||
if (text == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mcl_string_s *str = malloc(sizeof(mcl_string_s));
|
||||
string_s *str = malloc(sizeof(string_s));
|
||||
if (str == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -93,7 +93,7 @@ mcl_string_s *mcl_string_new(const char *text, size_t initial_capacity) {
|
||||
return str;
|
||||
}
|
||||
|
||||
int mcl_string_append(mcl_string_s *string, const char *text) {
|
||||
int string_append(string_s *string, const char *text) {
|
||||
if (string == NULL || text == NULL) {
|
||||
return -1;
|
||||
}
|
||||
@@ -137,7 +137,7 @@ 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) {
|
||||
int string_extend(string_s *destination, string_s *source) {
|
||||
if (destination == NULL || source == NULL) {
|
||||
return -1;
|
||||
}
|
||||
@@ -176,7 +176,7 @@ int mcl_string_extend(mcl_string_s *destination, mcl_string_s *source) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mcl_string_free(mcl_string_s *string) {
|
||||
void string_free(string_s *string) {
|
||||
if (string == NULL) {
|
||||
return;
|
||||
}
|
||||
@@ -190,7 +190,7 @@ void mcl_string_free(mcl_string_s *string) {
|
||||
free(string);
|
||||
}
|
||||
|
||||
size_t mcl_string_length(mcl_string_s *string) {
|
||||
size_t string_length(string_s *string) {
|
||||
if (string == NULL) {
|
||||
return 0;
|
||||
}
|
||||
@@ -206,7 +206,7 @@ size_t mcl_string_length(mcl_string_s *string) {
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t mcl_string_capacity(mcl_string_s *string) {
|
||||
size_t string_capacity(string_s *string) {
|
||||
if (string == NULL) {
|
||||
return 0;
|
||||
}
|
||||
@@ -222,7 +222,7 @@ size_t mcl_string_capacity(mcl_string_s *string) {
|
||||
return cap;
|
||||
}
|
||||
|
||||
char *mcl_string_cstr(mcl_string_s *string) {
|
||||
char *string_cstr(string_s *string) {
|
||||
if (string == NULL || string->data == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -283,7 +283,7 @@ char *mcl_string_cstr(mcl_string_s *string) {
|
||||
return tb->buf;
|
||||
}
|
||||
|
||||
int mcl_string_compare(mcl_string_s *s1, mcl_string_s *s2) {
|
||||
int string_compare(string_s *s1, string_s *s2) {
|
||||
if (s1 == NULL || s2 == NULL) {
|
||||
return -123;
|
||||
}
|
||||
@@ -306,7 +306,7 @@ int mcl_string_compare(mcl_string_s *s1, mcl_string_s *s2) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
void mcl_string_clear(mcl_string_s *string) {
|
||||
void string_clear(string_s *string) {
|
||||
if (string == NULL) {
|
||||
return;
|
||||
}
|
||||
@@ -321,7 +321,7 @@ void mcl_string_clear(mcl_string_s *string) {
|
||||
mtx_unlock(&string->lock);
|
||||
}
|
||||
|
||||
void mcl_string_toupper(mcl_string_s *string) {
|
||||
void string_toupper(string_s *string) {
|
||||
if (string == NULL) {
|
||||
return;
|
||||
}
|
||||
@@ -337,7 +337,7 @@ void mcl_string_toupper(mcl_string_s *string) {
|
||||
mtx_unlock(&string->lock);
|
||||
}
|
||||
|
||||
void mcl_string_tolower(mcl_string_s *string) {
|
||||
void string_tolower(string_s *string) {
|
||||
if (string == NULL) {
|
||||
return;
|
||||
}
|
||||
@@ -371,7 +371,7 @@ static void build_lsp(int *lps, const char *substring, size_t sub_len) {
|
||||
}
|
||||
}
|
||||
|
||||
int mcl_string_find(mcl_string_s *string, const char *substring) {
|
||||
int string_find(string_s *string, const char *substring) {
|
||||
if (string == NULL || substring == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -7,12 +7,12 @@
|
||||
/**
|
||||
* @brief Thread-safe dynamic string structure.
|
||||
*/
|
||||
typedef struct mcl_string {
|
||||
typedef struct string {
|
||||
char *data; /**< Pointer to null-terminated string data */
|
||||
size_t size; /**< Current length (excluding null terminator) */
|
||||
size_t capacity; /**< Allocated capacity including null terminator */
|
||||
mtx_t lock; /**< Mutex for thread safety */
|
||||
} mcl_string_s;
|
||||
} string_s;
|
||||
|
||||
/**
|
||||
* @brief Create a new string initialized with the given text.
|
||||
@@ -21,14 +21,14 @@ typedef struct mcl_string {
|
||||
* @param initial_capacity Initial buffer capacity (including null terminator). Pass 0 to auto-calculate.
|
||||
* @return Pointer to the new string, or NULL on failure.
|
||||
*/
|
||||
mcl_string_s *mcl_string_new(const char *text, size_t initial_capacity);
|
||||
string_s *string_new(const char *text, size_t initial_capacity);
|
||||
|
||||
/**
|
||||
* @brief Free the string and its resources.
|
||||
*
|
||||
* @param string String to free (safe to call with NULL).
|
||||
*/
|
||||
void mcl_string_free(mcl_string_s *string);
|
||||
void string_free(string_s *string);
|
||||
|
||||
/**
|
||||
* @brief Append text to the string.
|
||||
@@ -37,7 +37,7 @@ void mcl_string_free(mcl_string_s *string);
|
||||
* @param text Text to append.
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
int mcl_string_append(mcl_string_s *string, const char *text);
|
||||
int string_append(string_s *string, const char *text);
|
||||
|
||||
/**
|
||||
* @brief Extend by adding another string.
|
||||
@@ -46,14 +46,14 @@ int mcl_string_append(mcl_string_s *string, const char *text);
|
||||
* @param source Source string.
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
int mcl_string_extend(mcl_string_s *destination, mcl_string_s *source);
|
||||
int string_extend(string_s *destination, string_s *source);
|
||||
|
||||
/**
|
||||
* @brief Clear the string content without freeing the memory.
|
||||
*
|
||||
* @param string String to clear.
|
||||
*/
|
||||
void mcl_string_clear(mcl_string_s *string);
|
||||
void string_clear(string_s *string);
|
||||
|
||||
/**
|
||||
* @brief Get the current length of the string.
|
||||
@@ -61,7 +61,7 @@ void mcl_string_clear(mcl_string_s *string);
|
||||
* @param string String to query.
|
||||
* @return Length excluding null terminator, or 0 if NULL.
|
||||
*/
|
||||
size_t mcl_string_length(mcl_string_s *string);
|
||||
size_t string_len(string_s *string);
|
||||
|
||||
/**
|
||||
* @brief Get the total allocated capacity of the string buffer.
|
||||
@@ -69,7 +69,7 @@ size_t mcl_string_length(mcl_string_s *string);
|
||||
* @param string String to query.
|
||||
* @return Capacity in bytes (including null terminator), or 0 if NULL.
|
||||
*/
|
||||
size_t mcl_string_capacity(mcl_string_s *string);
|
||||
size_t string_cap(string_s *string);
|
||||
|
||||
/**
|
||||
* @brief Get a pointer to a null-terminated C-string.
|
||||
@@ -80,7 +80,7 @@ size_t mcl_string_capacity(mcl_string_s *string);
|
||||
* @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);
|
||||
char *string_cstr(string_s *string);
|
||||
|
||||
/**
|
||||
* @brief Compare two strings.
|
||||
@@ -89,21 +89,21 @@ char *mcl_string_cstr(mcl_string_s *string);
|
||||
* @param s2 Second string.
|
||||
* @return -123 on failure or same as strcmp().
|
||||
*/
|
||||
int mcl_string_compare(mcl_string_s *s1, mcl_string_s *s2);
|
||||
int string_compare(string_s *s1, string_s *s2);
|
||||
|
||||
/**
|
||||
* @brief Convert the string to uppercase.
|
||||
*
|
||||
* @param string String to modify.
|
||||
*/
|
||||
void mcl_string_toupper(mcl_string_s *string);
|
||||
void string_toupper(string_s *string);
|
||||
|
||||
/**
|
||||
* @brief Convert the string to lowercase.
|
||||
*
|
||||
* @param string String to modify.
|
||||
*/
|
||||
void mcl_string_tolower(mcl_string_s *string);
|
||||
void string_tolower(string_s *string);
|
||||
|
||||
/**
|
||||
* @brief Find a substring inside a string.
|
||||
@@ -112,6 +112,6 @@ void mcl_string_tolower(mcl_string_s *string);
|
||||
* @param substring Substring to search.
|
||||
* @return Index of the first occurrence, -1 on failure.
|
||||
*/
|
||||
int mcl_string_find(mcl_string_s *string, const char *substring);
|
||||
int string_find(string_s *string, const char *substring);
|
||||
|
||||
#endif /* MYCLIB_STRING_H */
|
||||
|
||||
@@ -11,27 +11,27 @@ void test_str1(void) {
|
||||
char *c_str;
|
||||
|
||||
/* Allocate a new dynamic string with an initial capacity */
|
||||
mcl_string_s *str = mcl_string_new("Hello, world!", 512);
|
||||
string_s *str = string_new("Hello, world!", 512);
|
||||
assert(str != NULL);
|
||||
|
||||
/* Retrieve a C str from string with mcl_string_cstr() */
|
||||
c_str = mcl_string_cstr(str);
|
||||
length = mcl_string_length(str);
|
||||
capacity = mcl_string_capacity(str);
|
||||
c_str = string_cstr(str);
|
||||
length = string_len(str);
|
||||
capacity = string_cap(str);
|
||||
assert(strcmp(c_str, "Hello, world!") == 0);
|
||||
assert(length == 13);
|
||||
assert(capacity == 512);
|
||||
|
||||
/* Append text to a mcl_string */
|
||||
assert(mcl_string_append(str, " How are you?") == 0);
|
||||
assert(string_append(str, " How are you?") == 0);
|
||||
|
||||
c_str = mcl_string_cstr(str);
|
||||
length = mcl_string_length(str);
|
||||
capacity = mcl_string_capacity(str);
|
||||
c_str = string_cstr(str);
|
||||
length = string_len(str);
|
||||
capacity = string_cap(str);
|
||||
assert(strcmp(c_str, "Hello, world! How are you?") == 0);
|
||||
assert(length == 26);
|
||||
assert(capacity == 512);
|
||||
|
||||
/* Always deallocate memory */
|
||||
mcl_string_free(str);
|
||||
string_free(str);
|
||||
}
|
||||
|
||||
@@ -4,35 +4,35 @@
|
||||
#include "../../string/mystring.h"
|
||||
|
||||
void test_str2(void) {
|
||||
mcl_string_s *s1 = mcl_string_new("Hello, world!", 0);
|
||||
string_s *s1 = string_new("Hello, world!", 0);
|
||||
assert(s1 != NULL);
|
||||
mcl_string_s *s2 = mcl_string_new("Hello, world!", 0);
|
||||
string_s *s2 = string_new("Hello, world!", 0);
|
||||
assert(s2 != NULL);
|
||||
/* Don't call mcl_string_cstr() more than once in the same printf function */
|
||||
|
||||
int ret = mcl_string_compare(s1, s2);
|
||||
int ret = string_compare(s1, s2);
|
||||
assert(ret == 0);
|
||||
|
||||
mcl_string_clear(s1);
|
||||
ret = mcl_string_compare(s1, s2);
|
||||
string_clear(s1);
|
||||
ret = string_compare(s1, s2);
|
||||
assert(ret != 0);
|
||||
|
||||
mcl_string_tolower(s1);
|
||||
mcl_string_toupper(s2);
|
||||
assert(strcmp(mcl_string_cstr(s1), "") == 0);
|
||||
assert(strcmp(mcl_string_cstr(s2), "HELLO, WORLD!") == 0);
|
||||
string_tolower(s1);
|
||||
string_toupper(s2);
|
||||
assert(strcmp(string_cstr(s1), "") == 0);
|
||||
assert(strcmp(string_cstr(s2), "HELLO, WORLD!") == 0);
|
||||
|
||||
/* Extend a string */
|
||||
mcl_string_s *extend_me = mcl_string_new("This string is suuuuuuuuuuuuuuuuuuuuuper extended!", 0);
|
||||
mcl_string_extend(s1, extend_me);
|
||||
assert(mcl_string_length(s1) == 50);
|
||||
assert(mcl_string_capacity(s1) == 64);
|
||||
string_s *extend_me = string_new("This string is suuuuuuuuuuuuuuuuuuuuuper extended!", 0);
|
||||
string_extend(s1, extend_me);
|
||||
assert(string_len(s1) == 50);
|
||||
assert(string_cap(s1) == 64);
|
||||
|
||||
/* Find substring in string */
|
||||
int pos = mcl_string_find(s1, "is");
|
||||
assert(pos == 2);
|
||||
int pos = string_find(s1, " is ");
|
||||
assert(pos == 11);
|
||||
|
||||
mcl_string_free(s1);
|
||||
mcl_string_free(s2);
|
||||
mcl_string_free(extend_me);
|
||||
string_free(s1);
|
||||
string_free(s2);
|
||||
string_free(extend_me);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user