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

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>
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("");

View File

@@ -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);