Files
lab0.1_letscontinue/str.c
etrushko05 028ee887b0 15
2025-11-01 06:03:19 -04:00

204 lines
3.6 KiB
C

#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "str.h"
#include "util.h"
#define INIT_SZ 20
//Внутренняя функция,
//позволяет убедиться что строка может вместить как минимум newsz символов
void
_str_ensure(str *s, int newsz)
{
int tmp;
newsz += 1; // добавляем единичку чтобы влез нулевой байт
if (newsz < s->capacity)
return;
tmp = s->capacity * 2;
if (tmp > newsz) {
newsz = tmp;
}
s->ptr = xrealloc(s->ptr, newsz);
s->capacity = newsz;
}
void
str_init(str *s)
{
assert(s);
s->len = 0;
s->capacity = INIT_SZ;
s->ptr = xmalloc(INIT_SZ);
s->ptr[0] = '\0';
}
void
str_deinit(str *s)
{
if (s->ptr)
free(s->ptr);
s->ptr = NULL;
s->len = 0;
s->capacity = 0;
}
void
str_init_data(str *s, const char *initial)
{
assert(s && initial);
int len = strlen(initial);
s->len = len;
s->capacity = len + 1;
s->ptr = xmalloc(s->capacity);
memcpy(s->ptr, initial, len + 1);
}
char *
str_data(str *s)
{
assert(s);
return s->ptr;
}
void
str_set(str *s, char *cstr)
{
assert(s && cstr);
int len = strlen(cstr);
_str_ensure(s, len);
memcpy(s->ptr, cstr, len + 1);
s->len = len;
}
str
str_copy(str *s)
{
assert(s);
str new_str;
str_init_data(&new_str, s->ptr);
return new_str;
}
int
str_count(str *s, char ch)
{
assert(s);
int count = 0;
for (int i = 0; i < s->len; i++) {
if (s->ptr[i] == ch) {
count++;
}
}
return count;
}
void
str_append(str *s, char *cstr)
{
assert(s && cstr);
int len = strlen(cstr);
str_append_n(s, cstr, len);
}
void
str_append_n(str *s, char *cstr, int len)
{
assert(s && cstr);
_str_ensure(s, s->len + len);
memcpy(s->ptr + s->len, cstr, len);
s->len += len;
s->ptr[s->len] = '\0';
}
void
str_shrink(str *s, int len)
{
assert(s && len >= 0);
if (len < s->len) {
s->len = len;
s->ptr[s->len] = '\0';
}
}
int
str_find(str *s, char *substr)
{
assert(s && substr);
char *p = strstr(s->ptr, substr);
if (p == NULL)
return -1;
return p - s->ptr;
}
str
str_sub(str *s, int start_idx, int length)
{
assert(s && start_idx >= 0 && length >= 0);
str s2;
str_init(&s2);
if (start_idx >= s->len) {
return s2; // пустая строка
}
int actual_length = length;
if (start_idx + length > s->len) {
actual_length = s->len - start_idx;
}
str_append_n(&s2, s->ptr + start_idx, actual_length);
return s2;
}
// Hint: you can create temporary string object!
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);
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));
str_deinit(&temp);
return count;
}