Switch to using dstring as a submodule
This commit is contained in:
parent
621e50ee24
commit
ba813ea5bf
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
[submodule "dstring"]
|
||||
path = dstring
|
||||
url = git@git.thewizardapprentice.com:abdelrahman/dstring
|
1
dstring
Submodule
1
dstring
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit c1ef7afcac21d7bc9536ca2dc309dd9dd57169d2
|
@ -1,22 +0,0 @@
|
||||
#ifndef DSTRING_H
|
||||
#define DSTRING_H
|
||||
|
||||
#include "aliases.h"
|
||||
|
||||
typedef struct dstring dstr_t;
|
||||
|
||||
dstr_t *dstr_with_capacity(u64 capacity);
|
||||
dstr_t *dstr_from_string(const char *str);
|
||||
void dstr_update(dstr_t **dst, const char *src);
|
||||
void dstr_free(dstr_t **str);
|
||||
void dstr_concat(dstr_t **dst, const char *src);
|
||||
void dstr_append(dstr_t **dst, char c);
|
||||
void dstr_resize(dstr_t **str);
|
||||
void dstr_clear(dstr_t *str);
|
||||
void dstr_print(const dstr_t *str);
|
||||
i64 dstr_find(const dstr_t *str, const char *substr);
|
||||
u64 dstr_length(const dstr_t *str);
|
||||
u64 dstr_capacity(const dstr_t *str);
|
||||
const char *dstr_to_cstr(const dstr_t *str);
|
||||
|
||||
#endif // !DSTRING_H
|
1
include/dstring/dstring.h
Symbolic link
1
include/dstring/dstring.h
Symbolic link
@ -0,0 +1 @@
|
||||
../../dstring/include/dstring.h
|
@ -1,211 +0,0 @@
|
||||
#include "dstring.h"
|
||||
#include "aliases.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Use this scalar to allocate extra memory in order to avoid having to
|
||||
// constantly reallocate
|
||||
#define CAPACITY_SCALAR 8
|
||||
|
||||
struct dstring {
|
||||
u64 capacity;
|
||||
u64 size;
|
||||
char buf[];
|
||||
};
|
||||
|
||||
dstr_t *dstr_with_capacity(u64 capacity) {
|
||||
dstr_t *out = (dstr_t *)malloc(sizeof(dstr_t) + capacity + 1);
|
||||
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->capacity = capacity;
|
||||
out->size = 0;
|
||||
memset(out->buf, 0, capacity + 1);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
dstr_t *dstr_from_string(const char *str) {
|
||||
u64 length = strlen(str);
|
||||
|
||||
u64 capacity = length * CAPACITY_SCALAR;
|
||||
|
||||
dstr_t *out = dstr_with_capacity(capacity);
|
||||
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->size = length;
|
||||
strncpy(out->buf, str, length);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void dstr_update(dstr_t **dst, const char *src) {
|
||||
if (!(*dst)) {
|
||||
return;
|
||||
}
|
||||
|
||||
u64 length = strlen(src);
|
||||
|
||||
dstr_t *str = *dst;
|
||||
|
||||
if (length <= str->capacity) {
|
||||
memset(str->buf, 0, str->capacity);
|
||||
|
||||
str->size = length;
|
||||
|
||||
strncpy(str->buf, src, length);
|
||||
} else {
|
||||
u64 capacity = length * CAPACITY_SCALAR;
|
||||
|
||||
dstr_t *tmp = (dstr_t *)realloc(*dst, sizeof(dstr_t) + capacity + 1);
|
||||
|
||||
if (!tmp) {
|
||||
return;
|
||||
}
|
||||
|
||||
tmp->capacity = capacity;
|
||||
tmp->size = length;
|
||||
strncpy(tmp->buf, src, length);
|
||||
|
||||
*dst = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
void dstr_free(dstr_t **str) {
|
||||
if (!(*str)) {
|
||||
return;
|
||||
}
|
||||
|
||||
free(*str);
|
||||
*str = NULL;
|
||||
}
|
||||
|
||||
void dstr_concat(dstr_t **dst, const char *src) {
|
||||
if (!(*dst)) {
|
||||
return;
|
||||
}
|
||||
|
||||
u64 src_length = strlen(src);
|
||||
|
||||
if (src_length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
u64 new_length = (*dst)->size + src_length;
|
||||
|
||||
char str[new_length + 1];
|
||||
memset(str, 0, new_length + 1);
|
||||
|
||||
strncpy(str, (*dst)->buf, (*dst)->size);
|
||||
strncat(str, src, src_length);
|
||||
|
||||
dstr_update(dst, str);
|
||||
}
|
||||
|
||||
void dstr_append(dstr_t **dst, char c) {
|
||||
if (!(*dst)) {
|
||||
return;
|
||||
}
|
||||
|
||||
u64 new_length = (*dst)->size + 1;
|
||||
|
||||
char str[new_length + 1];
|
||||
memset(str, 0, new_length + 1);
|
||||
|
||||
strncpy(str, (*dst)->buf, (*dst)->size);
|
||||
str[(*dst)->size] = c;
|
||||
|
||||
dstr_update(dst, str);
|
||||
}
|
||||
|
||||
void dstr_resize(dstr_t **str) {
|
||||
u64 capacity = (*str)->size;
|
||||
|
||||
dstr_t *tmp = (dstr_t *)realloc(*str, sizeof(dstr_t) + capacity + 1);
|
||||
|
||||
if (!tmp) {
|
||||
return;
|
||||
}
|
||||
|
||||
tmp->capacity = capacity;
|
||||
|
||||
*str = tmp;
|
||||
}
|
||||
|
||||
void dstr_clear(dstr_t *str) {
|
||||
if (!str || str->size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
memset(str->buf, 0, str->capacity);
|
||||
str->size = 0;
|
||||
}
|
||||
|
||||
void dstr_print(const dstr_t *str) {
|
||||
if (!str) {
|
||||
return;
|
||||
}
|
||||
|
||||
printf("%s\n", str->buf);
|
||||
}
|
||||
|
||||
i64 dstr_find(const dstr_t *str, const char *substr) {
|
||||
if (!str || !substr) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
u64 substr_length = strlen(substr);
|
||||
|
||||
if (substr_length == 0 || substr_length > str->size) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
char buf[substr_length + 1];
|
||||
memset(buf, 0, substr_length + 1);
|
||||
|
||||
for (i64 i = 0; i < str->size; ++i) {
|
||||
if (i + substr_length >= str->size) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (u64 j = 0; j < substr_length; ++j) {
|
||||
buf[j] = str->buf[i + j];
|
||||
}
|
||||
|
||||
if (strcmp(buf, substr) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
u64 dstr_length(const dstr_t *str) {
|
||||
if (!str) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return str->size;
|
||||
}
|
||||
|
||||
u64 dstr_capacity(const dstr_t *str) {
|
||||
if (!str) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return str->capacity;
|
||||
}
|
||||
|
||||
const char *dstr_to_cstr(const dstr_t *str) {
|
||||
if (!str) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return str->buf;
|
||||
}
|
1
src/dstring/dstring.c
Symbolic link
1
src/dstring/dstring.c
Symbolic link
@ -0,0 +1 @@
|
||||
../../dstring/src/dstring.c
|
Loading…
Reference in New Issue
Block a user