This commit is contained in:
Олег Бородин
2023-09-05 11:30:10 +02:00
parent 77bd507ae8
commit e9b9df0356
15 changed files with 423 additions and 562 deletions

View File

@@ -10,15 +10,17 @@
#include <cstring.h>
#define INIT_CAPA 1
#define INIT_CAPA 128
/* String container */
void* cstring_init(cstring_t* str) {
str->data = malloc((INIT_CAPA + 1)*sizeof(char));
size_t memsize = (INIT_CAPA + 1)*sizeof(char);
str->data = malloc(memsize);
if (str->data == NULL) return NULL;
memset(str->data, '\0', INIT_CAPA + 1);
//memset(str->data, '\0', memsize);
str->capa = INIT_CAPA;
str->size = 0;
str->data[str->size] = '\0';
return str->data;
}
@@ -27,14 +29,14 @@ void* cstring_append(cstring_t* str, char* add) {
size_t addsize = strlen(add);
size_t newsize = str->size + addsize;
if (newsize > str->capa) {
size_t newcapa = str->capa + addsize;
size_t newcapa = (str->capa*24)/18 + addsize*3;
char* newstr = realloc(str->data, (newcapa + 1)*sizeof(char));
if (newstr == NULL) return NULL;
str->data = newstr;
str->capa = newcapa;
}
strcpy(&(str->data[str->size]), add);
str->data[newsize + 1] = '\0';
memcpy(&(str->data[str->size]), add, addsize);
str->data[newsize] = '\0';
str->size = newsize;
return str->data;
}
@@ -43,7 +45,6 @@ char* cstring_getref(cstring_t* str) {
return str->data;
}
void cstring_destroy(cstring_t* str) {
if (str == NULL) return;
free(str->data);