1 #include <string.h> // strlen
2 #include <stdlib.h> // malloc, realloc
4 #include "include/cstring.h"
6 cstring_t *cstring_init() {
7 cstring_t *x = malloc(sizeof(cstring_t));
9 x->size = x->alloc = 0;
10 x->expand = cstring_expand;
11 x->expand_arr = cstring_expand_arr;
12 x->reset = cstring_reset;
13 x->delete = cstring_delete;
17 void cstring_expand(cstring_t *self, char x) {
18 if(self->size + sizeof(x) + sizeof(char) > self->alloc) {
19 self->alloc += (REALLOC_ADD * sizeof(char));
20 self->text = realloc(self->text, self->alloc);
22 self->text[self->size] = x;
23 self->text[self->size+1] = '\0';
24 self->size = strlen(self->text);
27 void cstring_expand_arr(cstring_t *self, char *x) {
28 if(self->size + strlen(x) + sizeof(char) > self->alloc) {
29 self->alloc += (REALLOC_ADD * sizeof(char));
30 self->text = realloc(self->text, self->alloc);
32 self->text = strcat(self->text, x);
33 self->size = strlen(self->text);
36 void cstring_reset(cstring_t *self) {
38 self->text = (void*)0;
39 self->size = self->alloc = 0;
42 void cstring_delete(cstring_t *self) {