refactor: improve string methods
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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