diff --git a/meson.build b/meson.build index 3a2ecf7..0306485 100644 --- a/meson.build +++ b/meson.build @@ -18,6 +18,7 @@ testlib = files( 'test/queue/q1.c', 'test/string/str1.c', 'test/string/str2.c', + 'test/string/str3.c', 'test/vector/v1.c', ) diff --git a/string/mystring.c b/string/mystring.c index 181acb2..f9d24a4 100644 --- a/string/mystring.c +++ b/string/mystring.c @@ -1,7 +1,9 @@ #include "mystring.h" #include +#include #include +#include #include #include #include @@ -83,7 +85,7 @@ string_s *string_new(const char *text, size_t initial_capacity) { str->data[str->size] = '\0'; /* Init mutex */ - if (mtx_init(&str->lock, mtx_plain) != thrd_success) { + if (mtx_init(&str->lock, mtx_recursive) != thrd_success) { free(str->data); free(str); @@ -306,28 +308,30 @@ int string_compare(string_s *s1, string_s *s2) { return ret; } -void string_clear(string_s *string) { +int string_clear(string_s *string) { if (string == NULL) { - return; + return -1; } if (mtx_lock(&string->lock) != thrd_success) { - return; + return -1; } memset(string->data, 0, string->size); string->size = 0; mtx_unlock(&string->lock); + + return 0; } -void string_toupper(string_s *string) { +int string_toupper(string_s *string) { if (string == NULL) { - return; + return -1; } if (mtx_lock(&string->lock) != thrd_success) { - return; + return -1; } for (size_t i = 0; i < string->size; ++i) { @@ -335,15 +339,17 @@ void string_toupper(string_s *string) { } mtx_unlock(&string->lock); + + return 0; } -void string_tolower(string_s *string) { +int string_tolower(string_s *string) { if (string == NULL) { - return; + return -1; } if (mtx_lock(&string->lock) != thrd_success) { - return; + return -1; } for (size_t i = 0; i < string->size; ++i) { @@ -351,10 +357,12 @@ void string_tolower(string_s *string) { } mtx_unlock(&string->lock); + + return 0; } /* Build Longest Prefix Suffix array */ -static void build_lsp(int *lps, const char *substring, size_t sub_len) { +static void build_lps(int *lps, const char *substring, size_t sub_len) { size_t len = 0; size_t i = 1; @@ -376,15 +384,21 @@ int string_find(string_s *string, const char *substring) { return -1; } + if (mtx_lock(&string->lock) != thrd_success) { + return -1; + } + /* Handle empty case */ if (strcmp(string->data, "") == 0) { + mtx_unlock(&string->lock); + return -1; } size_t sub_len = strlen(substring); int lps[sub_len]; memset(lps, 0, sizeof(lps)); - build_lsp(lps, substring, sub_len); + build_lps(lps, substring, sub_len); size_t i = 0; /* string iterator */ size_t j = 0; /* substring iterator */ @@ -393,6 +407,8 @@ int string_find(string_s *string, const char *substring) { i++; j++; if (j == sub_len) { + mtx_unlock(&string->lock); + return i - j; } } else { @@ -404,5 +420,115 @@ int string_find(string_s *string, const char *substring) { } } + mtx_unlock(&string->lock); + return -1; } + +string_s *string_format(const char *fmt, ...) { + if (fmt == NULL) { + return NULL; + } + + va_list args; + va_start(args, fmt); + size_t len = vsnprintf(NULL, 0, fmt, args) + 1; + va_end(args); + + string_s *str = string_new("", len); + + va_start(args, fmt); + vsnprintf(str->data, str->capacity, fmt, args); + va_end(args); + + return str; +} + +int string_insert(string_s *string, size_t index, const char *text) { + if (string == NULL || text == NULL) { + return -1; + } + + if (index > string->size) { + return -1; + } + + if (mtx_lock(&string->lock) != thrd_success) { + return -1; + } + + size_t text_len = strlen(text); + size_t new_size = string->size + text_len; + + if (new_size + 1 > string->capacity) { + /* Reallocate buffer */ + size_t new_cap = next_power_two(new_size + 1); + void *tmp = realloc(string->data, new_cap); + if (tmp == NULL) { + mtx_unlock(&string->lock); + + return -1; + } + string->data = tmp; + } + + /* Shift bytes */ + memmove((char *)string->data + ((index + text_len) * sizeof(char)), (char *)string->data + (index * sizeof(char)), string->size - index + 1); + /* Insert new text */ + memcpy((char *)string->data + (index * sizeof(char)), text, text_len); + string->size = new_size; + /* Ensure null termination */ + string->data[string->size] = '\0'; + + mtx_unlock(&string->lock); + + return 0; +} + +int string_remove(string_s *string, size_t index, size_t length) { + if (string == NULL) { + return -1; + } + + if (index + length > string->size) { + return -1; + } + + if (mtx_lock(&string->lock) != thrd_success) { + return -1; + } + + memmove((char *)string->data + (index * sizeof(char)), (char *)string->data + ((index + length) * sizeof(char)), string->size - length); + string->size -= length; + string->data[string->size] = '\0'; + + mtx_unlock(&string->lock); + + return 0; +} + +int string_replace(string_s *string, const char *old_text, const char *new_text) { + if (string == NULL || old_text == NULL || new_text == NULL) { + return -1; + } + + if (mtx_lock(&string->lock) != thrd_success) { + return -1; + } + + int pos; + size_t old_len = strlen(old_text); + + while (1) { + pos = string_find(string, old_text); + if (pos == -1) { + break; + } + string_remove(string, pos, old_len); + string_insert(string, pos, new_text); + } + + mtx_unlock(&string->lock); + + return 0; +} diff --git a/string/mystring.h b/string/mystring.h index 0ec6c75..849a5d5 100644 --- a/string/mystring.h +++ b/string/mystring.h @@ -52,8 +52,9 @@ int string_extend(string_s *destination, string_s *source); * @brief Clear the string content without freeing the memory. * * @param string String to clear. + * @return 0 on success, -1 on failure. */ -void string_clear(string_s *string); +int string_clear(string_s *string); /** * @brief Get the current length of the string. @@ -95,15 +96,17 @@ int string_compare(string_s *s1, string_s *s2); * @brief Convert the string to uppercase. * * @param string String to modify. + * @return 0 on success, -1 on failure. */ -void string_toupper(string_s *string); +int string_toupper(string_s *string); /** * @brief Convert the string to lowercase. * * @param string String to modify. + * @return 0 on success, -1 on failure. */ -void string_tolower(string_s *string); +int string_tolower(string_s *string); /** * @brief Find a substring inside a string. @@ -114,4 +117,17 @@ void string_tolower(string_s *string); */ int string_find(string_s *string, const char *substring); +string_s *string_format(const char *fmt, ...); + +int string_insert(string_s *string, size_t index, const char *text); + +int string_replace(string_s *string, const char *old_text, const char *new_text); + +int string_remove(string_s *string, size_t index, size_t length); + +// TODO +// string_trim(string_s *string) +// string_reverse(string_s *string) +// string_split(string_s *string, const char *delim, string_s ***out, size_t *count) ?? + #endif /* MYCLIB_STRING_H */ diff --git a/test/string/str3.c b/test/string/str3.c new file mode 100644 index 0000000..4a23a79 --- /dev/null +++ b/test/string/str3.c @@ -0,0 +1,30 @@ +#include +#include + +#include "../../string/mystring.h" + +void test_str3(void) { + /* Make a new string from format */ + string_s *s = string_format("My name is %s (%d)", "John", 21); + assert(strcmp(s->data, "My name is John (21)") == 0); + + /* Test insert */ + string_s *ms = string_new("Heo mate!", 0); + assert(ms != NULL); + string_insert(ms, 2, "ll"); + assert(strcmp(ms->data, "Hello mate!") == 0); + + /* Test delete */ + string_remove(ms, 5, 5); + assert(strcmp(ms->data, "Hello!") == 0); + + /* Test replace */ + string_s *replace_me = string_new("My car doesn't bark!", 0); + assert(strcmp(replace_me->data, "My car doesn't bark!") == 0); + string_replace(replace_me, "car", "dog"); + assert(strcmp(replace_me->data, "My dog doesn't bark!") == 0); + + string_free(ms); + string_free(s); + string_free(replace_me); +} diff --git a/test/test.c b/test/test.c index f2396f7..e0f3b50 100644 --- a/test/test.c +++ b/test/test.c @@ -6,9 +6,13 @@ #include void test_hm1(); + void test_q1(); + void test_str1(); void test_str2(); +void test_str3(); + void test_v1(); int main(void) { @@ -25,6 +29,7 @@ int main(void) { puts("==== [Running String tests] ===="); test_str1(); test_str2(); + test_str3(); puts("OK!"); puts(""); diff --git a/test/vector/v1.c b/test/vector/v1.c index 2fe707e..39be8b5 100644 --- a/test/vector/v1.c +++ b/test/vector/v1.c @@ -15,10 +15,12 @@ static void multiply(void *elem) { e->age = e->age * 2; } +/* Another way to use foreach static void print(void *elem) { my_elem_s *e = (my_elem_s *)elem; printf("%s (%d)\n", e->name, e->age); } +*/ void test_v1() { /* Allocate a new vector */ @@ -58,7 +60,7 @@ void test_v1() { /* Iterate for each element */ vec_foreach(v, multiply); /* Print each element */ - vec_foreach(v, print); + // vec_foreach(v, print); /* Deallocate the vector */ vec_free(v);