129 lines
1.7 KiB
C
129 lines
1.7 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);
|
|
// <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;
|
|
}
|
|
|
|
void
|
|
str_set(str *s, char *cstr)
|
|
{
|
|
assert(s && cstr);
|
|
// <YOURCODE>
|
|
}
|
|
|
|
str
|
|
str_copy(str *s)
|
|
{
|
|
assert(s);
|
|
// <YOURCODE>
|
|
}
|
|
|
|
int
|
|
str_count(str *s, char ch)
|
|
{
|
|
assert(s);
|
|
// <YOURCODE>
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
str_append(str *s, char *cstr)
|
|
{
|
|
assert(s && cstr);
|
|
// <YOURCODE>
|
|
}
|
|
|
|
void
|
|
str_append_n(str *s, char *cstr, int len)
|
|
{
|
|
assert(s && cstr);
|
|
// <YOURCODE>
|
|
}
|
|
|
|
void
|
|
str_shrink(str *s, int len)
|
|
{
|
|
assert(s && len >= 0);
|
|
// <YOURCODE>
|
|
}
|
|
|
|
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;
|
|
}
|