50 lines
962 B
C
50 lines
962 B
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
#include "DynString.h"
|
|
#include "log.h"
|
|
|
|
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);
|
|
|
|
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;
|
|
}
|
|
|