feat(string): format, insert, replace, remove

This commit is contained in:
2025-09-09 02:51:47 +02:00
parent 26f0887c5c
commit 6b177c87c6
6 changed files with 196 additions and 16 deletions

View File

@@ -18,6 +18,7 @@ testlib = files(
'test/queue/q1.c', 'test/queue/q1.c',
'test/string/str1.c', 'test/string/str1.c',
'test/string/str2.c', 'test/string/str2.c',
'test/string/str3.c',
'test/vector/v1.c', 'test/vector/v1.c',
) )

View File

@@ -1,7 +1,9 @@
#include "mystring.h" #include "mystring.h"
#include <ctype.h> #include <ctype.h>
#include <stdarg.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <threads.h> #include <threads.h>
@@ -83,7 +85,7 @@ string_s *string_new(const char *text, size_t initial_capacity) {
str->data[str->size] = '\0'; str->data[str->size] = '\0';
/* Init mutex */ /* 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->data);
free(str); free(str);
@@ -306,28 +308,30 @@ int string_compare(string_s *s1, string_s *s2) {
return ret; return ret;
} }
void string_clear(string_s *string) { int string_clear(string_s *string) {
if (string == NULL) { if (string == NULL) {
return; return -1;
} }
if (mtx_lock(&string->lock) != thrd_success) { if (mtx_lock(&string->lock) != thrd_success) {
return; return -1;
} }
memset(string->data, 0, string->size); memset(string->data, 0, string->size);
string->size = 0; string->size = 0;
mtx_unlock(&string->lock); mtx_unlock(&string->lock);
return 0;
} }
void string_toupper(string_s *string) { int string_toupper(string_s *string) {
if (string == NULL) { if (string == NULL) {
return; return -1;
} }
if (mtx_lock(&string->lock) != thrd_success) { if (mtx_lock(&string->lock) != thrd_success) {
return; return -1;
} }
for (size_t i = 0; i < string->size; ++i) { for (size_t i = 0; i < string->size; ++i) {
@@ -335,15 +339,17 @@ void string_toupper(string_s *string) {
} }
mtx_unlock(&string->lock); mtx_unlock(&string->lock);
return 0;
} }
void string_tolower(string_s *string) { int string_tolower(string_s *string) {
if (string == NULL) { if (string == NULL) {
return; return -1;
} }
if (mtx_lock(&string->lock) != thrd_success) { if (mtx_lock(&string->lock) != thrd_success) {
return; return -1;
} }
for (size_t i = 0; i < string->size; ++i) { for (size_t i = 0; i < string->size; ++i) {
@@ -351,10 +357,12 @@ void string_tolower(string_s *string) {
} }
mtx_unlock(&string->lock); mtx_unlock(&string->lock);
return 0;
} }
/* Build Longest Prefix Suffix array */ /* 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 len = 0;
size_t i = 1; size_t i = 1;
@@ -376,15 +384,21 @@ int string_find(string_s *string, const char *substring) {
return -1; return -1;
} }
if (mtx_lock(&string->lock) != thrd_success) {
return -1;
}
/* Handle empty case */ /* Handle empty case */
if (strcmp(string->data, "") == 0) { if (strcmp(string->data, "") == 0) {
mtx_unlock(&string->lock);
return -1; return -1;
} }
size_t sub_len = strlen(substring); size_t sub_len = strlen(substring);
int lps[sub_len]; int lps[sub_len];
memset(lps, 0, sizeof(lps)); 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 i = 0; /* string iterator */
size_t j = 0; /* substring iterator */ size_t j = 0; /* substring iterator */
@@ -393,6 +407,8 @@ int string_find(string_s *string, const char *substring) {
i++; i++;
j++; j++;
if (j == sub_len) { if (j == sub_len) {
mtx_unlock(&string->lock);
return i - j; return i - j;
} }
} else { } else {
@@ -404,5 +420,115 @@ int string_find(string_s *string, const char *substring) {
} }
} }
mtx_unlock(&string->lock);
return -1; 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;
}

View File

@@ -52,8 +52,9 @@ int string_extend(string_s *destination, string_s *source);
* @brief Clear the string content without freeing the memory. * @brief Clear the string content without freeing the memory.
* *
* @param string String to clear. * @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. * @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. * @brief Convert the string to uppercase.
* *
* @param string String to modify. * @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. * @brief Convert the string to lowercase.
* *
* @param string String to modify. * @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. * @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); 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 */ #endif /* MYCLIB_STRING_H */

30
test/string/str3.c Normal file
View File

@@ -0,0 +1,30 @@
#include <assert.h>
#include <string.h>
#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);
}

View File

@@ -6,9 +6,13 @@
#include <stdio.h> #include <stdio.h>
void test_hm1(); void test_hm1();
void test_q1(); void test_q1();
void test_str1(); void test_str1();
void test_str2(); void test_str2();
void test_str3();
void test_v1(); void test_v1();
int main(void) { int main(void) {
@@ -25,6 +29,7 @@ int main(void) {
puts("==== [Running String tests] ===="); puts("==== [Running String tests] ====");
test_str1(); test_str1();
test_str2(); test_str2();
test_str3();
puts("OK!"); puts("OK!");
puts(""); puts("");

View File

@@ -15,10 +15,12 @@ static void multiply(void *elem) {
e->age = e->age * 2; e->age = e->age * 2;
} }
/* Another way to use foreach
static void print(void *elem) { static void print(void *elem) {
my_elem_s *e = (my_elem_s *)elem; my_elem_s *e = (my_elem_s *)elem;
printf("%s (%d)\n", e->name, e->age); printf("%s (%d)\n", e->name, e->age);
} }
*/
void test_v1() { void test_v1() {
/* Allocate a new vector */ /* Allocate a new vector */
@@ -58,7 +60,7 @@ void test_v1() {
/* Iterate for each element */ /* Iterate for each element */
vec_foreach(v, multiply); vec_foreach(v, multiply);
/* Print each element */ /* Print each element */
vec_foreach(v, print); // vec_foreach(v, print);
/* Deallocate the vector */ /* Deallocate the vector */
vec_free(v); vec_free(v);