Files
lab0.1_letscontinue/str.c

129 lines
1.7 KiB
C
Raw Permalink Normal View History

2025-09-27 03:54:38 +03:00
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "str.h"
#include "util.h"
#define INIT_SZ 20
2025-10-03 22:58:30 +03:00
//Внутренняя функция,
//позволяет убедиться что строка может вместить как минимум 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;
}
2025-09-27 03:54:38 +03:00
void
str_init(str *s)
{
assert(s);
// <YOURCODE>
}
void
str_deinit(str *s)
{
if (s->ptr)
free(s->ptr);
s->ptr = NULL;
}
void
str_init_data(str *s, const char *initial)
{
assert(s && initial);
// <YOURCODE>
}
char *
str_data(str *s)
{
assert(s);
// <YOURCODE>
return NULL;
}
2025-10-03 22:58:30 +03:00
void
str_set(str *s, char *cstr)
{
assert(s && cstr);
// <YOURCODE>
}
2025-09-27 03:54:38 +03:00
str
str_copy(str *s)
{
assert(s);
// <YOURCODE>
}
2025-10-03 22:58:30 +03:00
int
str_count(str *s, char ch)
{
assert(s);
// <YOURCODE>
return 0;
}
void
str_append(str *s, char *cstr)
{
assert(s && cstr);
// <YOURCODE>
}
2025-09-27 03:54:38 +03:00
void
2025-10-03 22:58:30 +03:00
str_append_n(str *s, char *cstr, int len)
2025-09-27 03:54:38 +03:00
{
2025-10-03 22:58:30 +03:00
assert(s && cstr);
2025-09-27 03:54:38 +03:00
// <YOURCODE>
}
void
2025-10-03 22:58:30 +03:00
str_shrink(str *s, int len)
2025-09-27 03:54:38 +03:00
{
2025-10-03 22:58:30 +03:00
assert(s && len >= 0);
2025-09-27 03:54:38 +03:00
// <YOURCODE>
2025-10-03 22:58:30 +03:00
}
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);
// <YOURCODE>
return s2;
}
// Hint: you can create temporary string object!
int
str_replace(str *s, char *substr, char *replacement)
{
assert(s && substr && replacement);
// <YOURCODE>
return 0;
}