forked from 131/lab0.1_letscontinue
15
This commit is contained in:
18
str.c
18
str.c
@@ -159,35 +159,41 @@ int str_replace(str *s, char *substr, char *replacement)
|
|||||||
assert(s && substr && replacement);
|
assert(s && substr && replacement);
|
||||||
int substr_len = strlen(substr);
|
int substr_len = strlen(substr);
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
str temp;
|
str temp;
|
||||||
str_init(&temp);
|
str_init(&temp);
|
||||||
|
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
int found_pos;
|
int found_pos;
|
||||||
|
|
||||||
found_pos = str_find(s, substr);
|
found_pos = str_find(s, substr);
|
||||||
|
//poziciya iscomogo
|
||||||
|
|
||||||
while (found_pos != -1) {
|
while (found_pos != -1) {
|
||||||
str_append_n(&temp, s->ptr + pos, found_pos - pos);
|
str_append_n(&temp, s->ptr + pos, found_pos - pos);
|
||||||
|
//dobavlyaet str mezhdu proshlym i sled vhozhdenyem
|
||||||
str_append(&temp, replacement);
|
str_append(&temp, replacement);
|
||||||
|
//dobavlyaem zamenu
|
||||||
|
|
||||||
pos = found_pos + substr_len;
|
pos = found_pos + substr_len;
|
||||||
count++;
|
count++;
|
||||||
|
//perehodim na pos posle zamenennoi str
|
||||||
|
|
||||||
str search_str;
|
str search_str;
|
||||||
str_init(&search_str);
|
str_init(&search_str);
|
||||||
|
//esche odna vremennaya stroka
|
||||||
str_append_n(&search_str, s->ptr + pos, s->len - pos);
|
str_append_n(&search_str, s->ptr + pos, s->len - pos);
|
||||||
|
//copiruem v neye vse posle pos
|
||||||
found_pos = str_find(&search_str, substr);
|
found_pos = str_find(&search_str, substr);
|
||||||
|
//ichem v nei novoe vhozhdeniye
|
||||||
if (found_pos != -1) {
|
if (found_pos != -1) {
|
||||||
found_pos += pos;
|
found_pos += pos;
|
||||||
}
|
}
|
||||||
|
//nahodim poziciyu v ishodnoy str
|
||||||
str_deinit(&search_str);
|
str_deinit(&search_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pos < s->len) {
|
if (pos < s->len) {
|
||||||
str_append_n(&temp, s->ptr + pos, s->len - pos);
|
str_append_n(&temp, s->ptr + pos, s->len - pos);
|
||||||
}
|
}
|
||||||
|
//dobavlyaem ostatok str
|
||||||
|
|
||||||
str_set(s, str_data(&temp));
|
str_set(s, str_data(&temp));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user