Compare commits
30 Commits
0c07437ef2
...
growing_ar
| Author | SHA1 | Date | |
|---|---|---|---|
| b59aedad89 | |||
| 3f5e3558b9 | |||
| bb2db0d30d | |||
| e75846a507 | |||
| 05e56d67ea | |||
| 7164156c35 | |||
| e67e1df9a5 | |||
| e206b4f4a6 | |||
| 05bfa73509 | |||
| d4313fb8e4 | |||
| 92db9206cc | |||
| 6195b521f5 | |||
| be64571b0e | |||
| 645686ae22 | |||
| 3f9a908860 | |||
| 2e93bd794a | |||
| 39c88505bd | |||
| 970c588d66 | |||
|
|
efe8a104de | ||
|
|
6f799c4330 | ||
|
|
8a58a1cc48 | ||
|
|
03d2a59948 | ||
|
|
5b8e9f8be6 | ||
|
|
9ec123c41b | ||
|
|
7039b5437a | ||
|
|
ca36e0dc35 | ||
|
|
3fdd897291 | ||
|
|
08703b465c | ||
|
|
49ce26a6c2 | ||
| feada0b31c |
11
compile
11
compile
@@ -2,17 +2,12 @@
|
|||||||
|
|
||||||
CC=clang
|
CC=clang
|
||||||
INCLUDE="\
|
INCLUDE="\
|
||||||
-Ialiases \
|
$(find src -type d | xargs -I{} echo -n "-I{} ") \
|
||||||
-Icpath/include \
|
|
||||||
-Idstr/include \
|
|
||||||
$(find mem/include -type d | xargs -I{} echo -n "-I{} ") \
|
|
||||||
"
|
"
|
||||||
SRC="\
|
SRC="\
|
||||||
cpath/src/*.c \
|
$(find src -type f -name *.c | xargs -I{} echo -n "{} ") \
|
||||||
dstr/src/*.c \
|
|
||||||
mem/src/*/*.c \
|
|
||||||
"
|
"
|
||||||
CFLAGS="-shared -fPIC -Wall -Werror -pedantic"
|
CFLAGS="-O3 -shared -fPIC -Wall -Werror -pedantic"
|
||||||
OUT="libwapp.so"
|
OUT="libwapp.so"
|
||||||
|
|
||||||
(set -x ; $CC $CFLAGS $INCLUDE $SRC -o $OUT)
|
(set -x ; $CC $CFLAGS $INCLUDE $SRC -o $OUT)
|
||||||
|
|||||||
219
dstr/src/dstr.c
219
dstr/src/dstr.c
@@ -1,219 +0,0 @@
|
|||||||
#include "dstr.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 dstr {
|
|
||||||
u64 capacity;
|
|
||||||
u64 size;
|
|
||||||
char buf[];
|
|
||||||
};
|
|
||||||
|
|
||||||
String *wapp_dstr_with_capacity(u64 capacity) {
|
|
||||||
String *out = (String *)malloc(sizeof(String) + capacity + 1);
|
|
||||||
|
|
||||||
if (!out) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
out->capacity = capacity;
|
|
||||||
out->size = 0;
|
|
||||||
memset(out->buf, 0, capacity + 1);
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
String *wapp_dstr_from_string(const char *str) {
|
|
||||||
if (!str) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 length = strlen(str);
|
|
||||||
|
|
||||||
u64 capacity = length * CAPACITY_SCALAR;
|
|
||||||
|
|
||||||
String *out = wapp_dstr_with_capacity(capacity);
|
|
||||||
|
|
||||||
if (!out) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
out->size = length;
|
|
||||||
strncpy(out->buf, str, length);
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_dstr_update(String **dst, const char *src) {
|
|
||||||
if (!dst || !(*dst)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 length = strlen(src);
|
|
||||||
|
|
||||||
String *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;
|
|
||||||
|
|
||||||
String *tmp = (String *)realloc(*dst, sizeof(String) + capacity + 1);
|
|
||||||
|
|
||||||
if (!tmp) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp->capacity = capacity;
|
|
||||||
tmp->size = length;
|
|
||||||
strncpy(tmp->buf, src, length);
|
|
||||||
|
|
||||||
*dst = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_dstr_free(String **str) {
|
|
||||||
if (!str || !(*str)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(*str);
|
|
||||||
*str = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_dstr_concat(String **dst, const char *src) {
|
|
||||||
if (!dst || !(*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);
|
|
||||||
|
|
||||||
wapp_dstr_update(dst, str);
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_dstr_append(String **dst, char c) {
|
|
||||||
if (!dst || !(*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;
|
|
||||||
|
|
||||||
wapp_dstr_update(dst, str);
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_dstr_resize(String **str) {
|
|
||||||
if (!str || !(*str)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 capacity = (*str)->size;
|
|
||||||
|
|
||||||
String *tmp = (String *)realloc(*str, sizeof(String) + capacity + 1);
|
|
||||||
|
|
||||||
if (!tmp) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp->capacity = capacity;
|
|
||||||
|
|
||||||
*str = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_dstr_clear(String *str) {
|
|
||||||
if (!str || str->size == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(str->buf, 0, str->capacity);
|
|
||||||
str->size = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_dstr_print(const String *str) {
|
|
||||||
if (!str) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("%s\n", str->buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
i64 wapp_dstr_find(const String *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 (u64 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 wapp_dstr_length(const String *str) {
|
|
||||||
if (!str) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return str->size;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 wapp_dstr_capacity(const String *str) {
|
|
||||||
if (!str) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return str->capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *wapp_dstr_to_cstr(const String *str) {
|
|
||||||
if (!str) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
return str->buf;
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
#ifndef MEM_CTX_H
|
|
||||||
#define MEM_CTX_H
|
|
||||||
|
|
||||||
#include "mem_arena.h"
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
CTX_DEST_BUFFER_MAIN,
|
|
||||||
CTX_DEST_BUFFER_TEMP,
|
|
||||||
|
|
||||||
COUNT_CTX_DEST_BUFFER,
|
|
||||||
} CTXDestBuffer;
|
|
||||||
|
|
||||||
void wapp_mem_ctx_init(u64 main_buf_capacity, u64 temp_buf_capacity);
|
|
||||||
void *wapp_mem_ctx_alloc(CTXDestBuffer buffer, u64 size);
|
|
||||||
void *wapp_mem_ctx_alloc_aligned(CTXDestBuffer buffer, u64 size, u64 alignment);
|
|
||||||
void wapp_mem_ctx_clear(CTXDestBuffer buffer);
|
|
||||||
void wapp_mem_ctx_free(void);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
#endif // !MEM_CTX_H
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
#include "mem_ctx.h"
|
|
||||||
#include "aliases.h"
|
|
||||||
#include "mem_arena.h"
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
typedef struct mem_ctx MemCTX;
|
|
||||||
struct mem_ctx {
|
|
||||||
Arena *main;
|
|
||||||
Arena *temp;
|
|
||||||
bool main_initialised;
|
|
||||||
bool temp_initialised;
|
|
||||||
};
|
|
||||||
|
|
||||||
internal MemCTX g_context = {0};
|
|
||||||
|
|
||||||
internal Arena *get_arena(CTXDestBuffer buffer);
|
|
||||||
|
|
||||||
void wapp_mem_ctx_init(u64 main_buf_capacity, u64 temp_buf_capacity) {
|
|
||||||
g_context.main_initialised =
|
|
||||||
wapp_mem_arena_init(&g_context.main, main_buf_capacity);
|
|
||||||
g_context.temp_initialised =
|
|
||||||
wapp_mem_arena_init(&g_context.temp, temp_buf_capacity);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *wapp_mem_ctx_alloc(CTXDestBuffer buffer, u64 size) {
|
|
||||||
Arena *arena = get_arena(buffer);
|
|
||||||
if (!arena) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return wapp_mem_arena_alloc(arena, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *wapp_mem_ctx_alloc_aligned(CTXDestBuffer buffer, u64 size,
|
|
||||||
u64 alignment) {
|
|
||||||
Arena *arena = get_arena(buffer);
|
|
||||||
if (!arena) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return wapp_mem_arena_alloc_aligned(arena, size, alignment);
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_mem_ctx_clear(CTXDestBuffer buffer) {
|
|
||||||
Arena *arena = get_arena(buffer);
|
|
||||||
if (!arena) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
wapp_mem_arena_clear(arena);
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_mem_ctx_free(void) {
|
|
||||||
wapp_mem_arena_free(&(g_context.main));
|
|
||||||
g_context.main_initialised = false;
|
|
||||||
|
|
||||||
wapp_mem_arena_free(&(g_context.temp));
|
|
||||||
g_context.temp_initialised = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal Arena *get_arena(CTXDestBuffer buffer) {
|
|
||||||
Arena *output = NULL;
|
|
||||||
|
|
||||||
switch (buffer) {
|
|
||||||
case CTX_DEST_BUFFER_MAIN:
|
|
||||||
if (g_context.main_initialised) {
|
|
||||||
output = g_context.main;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case CTX_DEST_BUFFER_TEMP:
|
|
||||||
if (g_context.temp_initialised) {
|
|
||||||
output = g_context.temp;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
assert(false && "Not all context destination buffers are handled");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
@@ -11,6 +11,8 @@
|
|||||||
#define DEFAULT_ALIGNMENT (2 * sizeof(void *))
|
#define DEFAULT_ALIGNMENT (2 * sizeof(void *))
|
||||||
#endif /* ifndef DEFAULT_ALIGNMENT */
|
#endif /* ifndef DEFAULT_ALIGNMENT */
|
||||||
|
|
||||||
|
#define ARENA_MINIMUM_CAPACITY 1024
|
||||||
|
|
||||||
typedef struct base_arena BaseArena;
|
typedef struct base_arena BaseArena;
|
||||||
struct base_arena {
|
struct base_arena {
|
||||||
u8 *buf;
|
u8 *buf;
|
||||||
@@ -30,7 +32,7 @@ internal bool base_arena_init(BaseArena *arena, u64 capacity);
|
|||||||
internal void *base_arena_alloc_aligned(BaseArena *arena, u64 size,
|
internal void *base_arena_alloc_aligned(BaseArena *arena, u64 size,
|
||||||
u64 alignment);
|
u64 alignment);
|
||||||
internal void base_arena_clear(BaseArena *arena);
|
internal void base_arena_clear(BaseArena *arena);
|
||||||
internal void base_arena_free(BaseArena *arena);
|
internal void base_arena_destroy(BaseArena *arena);
|
||||||
|
|
||||||
// PUBLIC API
|
// PUBLIC API
|
||||||
|
|
||||||
@@ -39,24 +41,20 @@ bool wapp_mem_arena_init(Arena **arena, u64 base_capacity) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
*arena = (Arena *)malloc(sizeof(Arena));
|
*arena = (Arena *)calloc(1, sizeof(Arena));
|
||||||
Arena *arena_ptr = *arena;
|
Arena *arena_ptr = *arena;
|
||||||
if (!arena_ptr) {
|
if (!arena_ptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(arena_ptr, 0, sizeof(Arena));
|
arena_ptr->active_arena = (BaseArena *)calloc(1, sizeof(BaseArena));
|
||||||
|
|
||||||
arena_ptr->active_arena = (BaseArena *)malloc(sizeof(BaseArena));
|
|
||||||
if (!(arena_ptr->active_arena)) {
|
if (!(arena_ptr->active_arena)) {
|
||||||
wapp_mem_arena_free(arena);
|
wapp_mem_arena_destroy(arena);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(arena_ptr->active_arena, 0, sizeof(BaseArena));
|
|
||||||
|
|
||||||
if (!base_arena_init(arena_ptr->active_arena, base_capacity)) {
|
if (!base_arena_init(arena_ptr->active_arena, base_capacity)) {
|
||||||
wapp_mem_arena_free(arena);
|
wapp_mem_arena_destroy(arena);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,13 +78,11 @@ void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
|
|||||||
if (arena->active_arena->next) {
|
if (arena->active_arena->next) {
|
||||||
arena->active_arena = arena->active_arena->next;
|
arena->active_arena = arena->active_arena->next;
|
||||||
} else {
|
} else {
|
||||||
arena->active_arena->next = (BaseArena *)malloc(sizeof(BaseArena));
|
arena->active_arena->next = (BaseArena *)calloc(1, sizeof(BaseArena));
|
||||||
if (!(arena->active_arena->next)) {
|
if (!(arena->active_arena->next)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(arena->active_arena->next, 0, sizeof(BaseArena));
|
|
||||||
|
|
||||||
if (!base_arena_init(arena->active_arena->next,
|
if (!base_arena_init(arena->active_arena->next,
|
||||||
arena->initial_capacity)) {
|
arena->initial_capacity)) {
|
||||||
free(arena->active_arena->next);
|
free(arena->active_arena->next);
|
||||||
@@ -127,7 +123,7 @@ void wapp_mem_arena_clear(Arena *arena) {
|
|||||||
arena->active_arena = last_active;
|
arena->active_arena = last_active;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_mem_arena_free(Arena **arena) {
|
void wapp_mem_arena_destroy(Arena **arena) {
|
||||||
if (!arena) {
|
if (!arena) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -145,7 +141,7 @@ void wapp_mem_arena_free(Arena **arena) {
|
|||||||
while (current) {
|
while (current) {
|
||||||
next = current->next;
|
next = current->next;
|
||||||
|
|
||||||
base_arena_free(current);
|
base_arena_destroy(current);
|
||||||
free(current);
|
free(current);
|
||||||
|
|
||||||
current = next;
|
current = next;
|
||||||
@@ -155,13 +151,13 @@ void wapp_mem_arena_free(Arena **arena) {
|
|||||||
while (current) {
|
while (current) {
|
||||||
prev = current->prev;
|
prev = current->prev;
|
||||||
|
|
||||||
base_arena_free(current);
|
base_arena_destroy(current);
|
||||||
free(current);
|
free(current);
|
||||||
|
|
||||||
current = prev;
|
current = prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
base_arena_free(arena_ptr->active_arena);
|
base_arena_destroy(arena_ptr->active_arena);
|
||||||
|
|
||||||
free(arena_ptr->active_arena);
|
free(arena_ptr->active_arena);
|
||||||
arena_ptr->active_arena = NULL;
|
arena_ptr->active_arena = NULL;
|
||||||
@@ -180,15 +176,15 @@ internal bool base_arena_init(BaseArena *arena, u64 capacity) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 alloc_size = sizeof(u8) * capacity;
|
u64 arena_capacity =
|
||||||
|
capacity >= ARENA_MINIMUM_CAPACITY ? capacity : ARENA_MINIMUM_CAPACITY;
|
||||||
|
|
||||||
arena->buf = (u8 *)malloc(alloc_size);
|
arena->buf = (u8 *)calloc(arena_capacity, sizeof(u8));
|
||||||
if (!(arena->buf)) {
|
if (!(arena->buf)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(arena->buf, 0, alloc_size);
|
arena->capacity = arena_capacity;
|
||||||
arena->capacity = capacity;
|
|
||||||
arena->offset = arena->buf;
|
arena->offset = arena->buf;
|
||||||
arena->prev = arena->next = NULL;
|
arena->prev = arena->next = NULL;
|
||||||
|
|
||||||
@@ -201,12 +197,14 @@ internal void *base_arena_alloc_aligned(BaseArena *arena, u64 size,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 *output = wapp_mem_util_align_forward((void *)(arena->offset), alignment);
|
u8 *alloc_start = arena->offset;
|
||||||
|
|
||||||
|
u8 *output = wapp_mem_util_align_forward((void *)alloc_start, alignment);
|
||||||
if (output + size >= arena->buf + arena->capacity) {
|
if (output + size >= arena->buf + arena->capacity) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
arena->offset += size;
|
arena->offset = output + size;
|
||||||
|
|
||||||
return (void *)output;
|
return (void *)output;
|
||||||
}
|
}
|
||||||
@@ -220,7 +218,7 @@ internal void base_arena_clear(BaseArena *arena) {
|
|||||||
arena->offset = arena->buf;
|
arena->offset = arena->buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void base_arena_free(BaseArena *arena) {
|
internal void base_arena_destroy(BaseArena *arena) {
|
||||||
if (!arena) {
|
if (!arena) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ bool wapp_mem_arena_init(Arena **arena, u64 base_capacity);
|
|||||||
void *wapp_mem_arena_alloc(Arena *arena, u64 size);
|
void *wapp_mem_arena_alloc(Arena *arena, u64 size);
|
||||||
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment);
|
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment);
|
||||||
void wapp_mem_arena_clear(Arena *arena);
|
void wapp_mem_arena_clear(Arena *arena);
|
||||||
void wapp_mem_arena_free(Arena **arena);
|
void wapp_mem_arena_destroy(Arena **arena);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
34
src/strings/basic_strings/basic_strings.h
Normal file
34
src/strings/basic_strings/basic_strings.h
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#ifndef BASIC_STRING_H
|
||||||
|
#define BASIC_STRING_H
|
||||||
|
|
||||||
|
#include "aliases.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
typedef struct bstr BasicString;
|
||||||
|
struct bstr {
|
||||||
|
u64 size;
|
||||||
|
const char *buf;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct strvw StringView;
|
||||||
|
struct strvw {
|
||||||
|
const u64 size;
|
||||||
|
const char *buf;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define new_string(STR) \
|
||||||
|
{ .size = strlen(STR), .buf = STR }
|
||||||
|
#define wapp_bstr_new(STR) ((BasicString)new_string(STR))
|
||||||
|
#define wapp_strvw_new(STR) ((StringView)new_string(STR))
|
||||||
|
#define wapp_string_print(STR) (printf("%.*s\n", (i32)STR.size, STR.buf))
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // !BASIC_STRING_H
|
||||||
174
src/strings/dstr/dstr.c
Normal file
174
src/strings/dstr/dstr.c
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
#include "dstr.h"
|
||||||
|
#include "aliases.h"
|
||||||
|
#include "mem_arena.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
|
||||||
|
#define MINIMUM_DSTR_CAPACITY 1024
|
||||||
|
|
||||||
|
struct dstr {
|
||||||
|
u64 capacity;
|
||||||
|
u64 size;
|
||||||
|
char buf[];
|
||||||
|
};
|
||||||
|
|
||||||
|
String *wapp_dstr_with_capacity(u64 capacity, Arena *arena) {
|
||||||
|
if (!arena) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
String *out =
|
||||||
|
(String *)wapp_mem_arena_alloc(arena, sizeof(String) + capacity + 1);
|
||||||
|
if (!out) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
out->capacity = capacity;
|
||||||
|
out->size = 0;
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
String *wapp_dstr_from_string(const char *str, Arena *arena) {
|
||||||
|
if (!str || !arena) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 length = strlen(str);
|
||||||
|
u64 capacity = length * CAPACITY_SCALAR;
|
||||||
|
capacity =
|
||||||
|
capacity >= MINIMUM_DSTR_CAPACITY ? capacity : MINIMUM_DSTR_CAPACITY;
|
||||||
|
|
||||||
|
String *out = wapp_dstr_with_capacity(capacity, arena);
|
||||||
|
if (!out) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
out->size = length;
|
||||||
|
strncpy(out->buf, str, length + 1);
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringUpdate wapp_dstr_update(String **dst, const char *src, Arena *arena) {
|
||||||
|
if (!dst || !(*dst)) {
|
||||||
|
return (StringUpdate){.updated = false, .str = *dst};
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 length = strlen(src);
|
||||||
|
|
||||||
|
String *str = *dst;
|
||||||
|
|
||||||
|
if (length >= str->capacity) {
|
||||||
|
if (!arena) {
|
||||||
|
return (StringUpdate){.updated = false, .str = *dst};
|
||||||
|
}
|
||||||
|
|
||||||
|
String *new_str = wapp_dstr_from_string(src, arena);
|
||||||
|
if (!new_str) {
|
||||||
|
return (StringUpdate){.updated = false, .str = *dst};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (StringUpdate){.updated = true, .str = new_str};
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(str->buf, 0, str->capacity);
|
||||||
|
|
||||||
|
str->size = length;
|
||||||
|
|
||||||
|
strncpy(str->buf, src, length + 1);
|
||||||
|
|
||||||
|
return (StringUpdate){.updated = true, .str = *dst};
|
||||||
|
}
|
||||||
|
|
||||||
|
StringUpdate wapp_dstr_concat(String **dst, const char *src, Arena *arena) {
|
||||||
|
if (!dst || !(*dst)) {
|
||||||
|
return (StringUpdate){.updated = false, .str = *dst};
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 src_length = strlen(src);
|
||||||
|
if (src_length == 0) {
|
||||||
|
return (StringUpdate){.updated = false, .str = *dst};
|
||||||
|
}
|
||||||
|
|
||||||
|
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, new_length + 1 - (*dst)->size);
|
||||||
|
|
||||||
|
return wapp_dstr_update(dst, str, arena);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wapp_dstr_clear(String *str) {
|
||||||
|
if (!str || str->size == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(str->buf, 0, str->capacity);
|
||||||
|
str->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wapp_dstr_print(const String *str) {
|
||||||
|
if (!str) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%.*s\n", (i32)str->size, str->buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
i64 wapp_dstr_find(const String *str, const char *substr) {
|
||||||
|
if (!str || !substr) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 substr_length = strlen(substr);
|
||||||
|
if (substr_length == 0 || substr_length > str->size) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *s1;
|
||||||
|
for (u64 i = 0; i < str->size; ++i) {
|
||||||
|
if (i + substr_length > str->size) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
s1 = &(str->buf[i]);
|
||||||
|
|
||||||
|
if (strncmp(s1, substr, substr_length) == 0) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 wapp_dstr_length(const String *str) {
|
||||||
|
if (!str) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return str->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 wapp_dstr_capacity(const String *str) {
|
||||||
|
if (!str) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return str->capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *wapp_dstr_to_cstr(const String *str) {
|
||||||
|
if (!str) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return str->buf;
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
#define DSTR_H
|
#define DSTR_H
|
||||||
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
|
#include "mem_arena.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -9,13 +11,16 @@ extern "C" {
|
|||||||
|
|
||||||
typedef struct dstr String;
|
typedef struct dstr String;
|
||||||
|
|
||||||
String *wapp_dstr_with_capacity(u64 capacity);
|
typedef struct string_update StringUpdate;
|
||||||
String *wapp_dstr_from_string(const char *str);
|
struct string_update {
|
||||||
void wapp_dstr_update(String **dst, const char *src);
|
bool updated;
|
||||||
void wapp_dstr_free(String **str);
|
String *str;
|
||||||
void wapp_dstr_concat(String **dst, const char *src);
|
};
|
||||||
void wapp_dstr_append(String **dst, char c);
|
|
||||||
void wapp_dstr_resize(String **str);
|
String *wapp_dstr_with_capacity(u64 capacity, Arena *arena);
|
||||||
|
String *wapp_dstr_from_string(const char *str, Arena *arena);
|
||||||
|
StringUpdate wapp_dstr_update(String **dst, const char *src, Arena *arena);
|
||||||
|
StringUpdate wapp_dstr_concat(String **dst, const char *src, Arena *arena);
|
||||||
void wapp_dstr_clear(String *str);
|
void wapp_dstr_clear(String *str);
|
||||||
void wapp_dstr_print(const String *str);
|
void wapp_dstr_print(const String *str);
|
||||||
i64 wapp_dstr_find(const String *str, const char *substr);
|
i64 wapp_dstr_find(const String *str, const char *substr);
|
||||||
Reference in New Issue
Block a user