This commit is contained in:
etrushko05
2025-11-01 06:03:19 -04:00
parent 3b7ddda0f5
commit 028ee887b0

18
str.c
View File

@@ -159,35 +159,41 @@ int str_replace(str *s, char *substr, char *replacement)
assert(s && substr && replacement);
int substr_len = strlen(substr);
int count = 0;
str temp;
str_init(&temp);
str_init(&temp);
int pos = 0;
int found_pos;
found_pos = str_find(s, substr);
//poziciya iscomogo
while (found_pos != -1) {
str_append_n(&temp, s->ptr + pos, found_pos - pos);
//dobavlyaet str mezhdu proshlym i sled vhozhdenyem
str_append(&temp, replacement);
//dobavlyaem zamenu
pos = found_pos + substr_len;
count++;
//perehodim na pos posle zamenennoi str
str search_str;
str_init(&search_str);
//esche odna vremennaya stroka
str_append_n(&search_str, s->ptr + pos, s->len - pos);
//copiruem v neye vse posle pos
found_pos = str_find(&search_str, substr);
//ichem v nei novoe vhozhdeniye
if (found_pos != -1) {
found_pos += pos;
}
//nahodim poziciyu v ishodnoy str
str_deinit(&search_str);
}
if (pos < s->len) {
str_append_n(&temp, s->ptr + pos, s->len - pos);
}
//dobavlyaem ostatok str
str_set(s, str_data(&temp));