This commit is contained in:
2026-06-28 23:00:17 +03:00
parent 47d9c3fbe6
commit 0b9cfab8ff
7 changed files with 199 additions and 91 deletions

View File

@@ -1,17 +1,49 @@
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "DynString.h"
#include "log.h"
short DynString_alloc(DynString* dstr, const char* str, const size_t len) {
dstr->data = (char*) malloc(sizeof(char) * len + 1);
if (dstr->data == NULL) {
return -1;
DynString DynString_alloc(const char* str, const size_t len) {
DynString dstr = {
.data = (char*) malloc(sizeof(char) * len),
.len = len
};
if (dstr.data == NULL) {
LOG_FATAL("Ran out of memory");
}
dstr->data = memcpy(dstr->data, str, len);
dstr->data[len] = '\0';
dstr->len = len;
dstr.data = memcpy(dstr.data, str, len);
return 1;
return dstr;
}
void DynString_free(DynString* dstr) {
if (dstr->data != NULL) {
free(dstr->data);
}
dstr->len = 0;
}
bool DynString_is_null(const DynString* dstr) {
return dstr->data == NULL;
}
bool DynString_equal(const DynString* s1, const DynString* s2) {
if (s1->len != s2->len) {
return false;
}
if (s1->data == NULL || s2->data == NULL) {
return false;
}
for (size_t i = 0; i < s1->len; i++) {
if (s1->data[i] != s2->data[i]) {
return false;
}
}
return true;
}