progress
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user