68 lines
952 B
C
68 lines
952 B
C
#include "str.h"
|
|
|
|
void str_init(str_t* str)
|
|
{
|
|
assert(str);
|
|
str->data = NULL;
|
|
str->size = 0;
|
|
str->cap = 0;
|
|
}
|
|
|
|
void str_free(str_t* str)
|
|
{
|
|
assert(str);
|
|
|
|
free(str->data);
|
|
str->data = NULL;
|
|
str->size = 0;
|
|
str->cap = 0;
|
|
}
|
|
|
|
void str_resize(str_t* str)
|
|
{
|
|
assert(str);
|
|
|
|
if (str->data == NULL)
|
|
{
|
|
str->cap = 2;
|
|
str->data = calloc(str->cap + 1, sizeof(char));
|
|
}
|
|
|
|
if (str->size >= str->cap)
|
|
{
|
|
str->cap *= 2;
|
|
str->data = realloc(str->data, sizeof(char) * (str->cap + 1));
|
|
}
|
|
}
|
|
|
|
void str_push(str_t* str, char c)
|
|
{
|
|
assert(str);
|
|
|
|
str_resize(str);
|
|
str->data[str->size] = c;
|
|
|
|
str_resize(str);
|
|
str->data[str->size + 1] = '\0';
|
|
str->size++;
|
|
}
|
|
|
|
void str_append(str_t* str, char const* src)
|
|
{
|
|
assert(str);
|
|
assert(src);
|
|
|
|
size_t len = strlen(src);
|
|
|
|
for (size_t i=0; i<len; i++)
|
|
{
|
|
str_push(str, src[i]);
|
|
}
|
|
}
|
|
|
|
int str_empty(str_t* str)
|
|
{
|
|
assert(str);
|
|
return str->size == 0;
|
|
}
|