aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/container.h35
-rw-r--r--include/dynstring.h1
-rw-r--r--include/stringbuilder.h114
3 files changed, 143 insertions, 7 deletions
diff --git a/include/container.h b/include/container.h
index 5f40e4a..0234abe 100644
--- a/include/container.h
+++ b/include/container.h
@@ -1,6 +1,12 @@
+#ifndef CONTAINER_DISABLE_ALL
#ifndef JUSTANOTHERCATGIRL_HEADERS_CONTAINER
#define JUSTANOTHERCATGIRL_HEADERS_CONTAINER
+#if defined(CONTAINER_DISABLE_ARRAY) || defined(CONTAINER_DISABLE_ARRAY) || defined(CONTAINER_DISABLE_HASH)
+# define CONTAINER_DISABLE_HMAP
+# define CONTAINER_DISABLE_HSET
+#endif
+
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
@@ -10,6 +16,7 @@
/* ----------------------------------------------------------------- */
/* -----------------UTILITY HEADER---------------------------------- */
/* ----------------------------------------------------------------- */
+#ifndef CONTAINER_DISABLE_UTILITY
#define stringify(val) _stringify_helper(val)
#define _stringify_helper(val) #val
#define string_concat_separator(first, ...) stringify(first) ";" string_concat_separator(__VA_ARGS__)
@@ -68,7 +75,7 @@ typedef void*(*memcpy_t)(void* restrict, const void*, size_t);
#else
# define _CONTAINER_STATIC static
#endif /* CONTAINER_EXPOSE_HELPERS */
-
+#endif /* CONTAINER_DISABLE_UTILITY */
/* ----------------------------------------------------------------- */
/* -------------------------ARRAY HEADER---------------------------- */
@@ -87,7 +94,7 @@ typedef void*(*memcpy_t)(void* restrict, const void*, size_t);
/* TODO: implement in operations that change the array size */
/* #define SHINK_RESIZING_ARRAY */
-
+#ifndef CONTAINER_DISABLE_ARRAY
/* size of the array header. Should not be used directly, unless you know what you are doing */
#define DYNARRAY_HEADER_SIZE sizeof(struct _dynarray_header)
@@ -154,11 +161,13 @@ void* array_copy(void* old);
/* Returns bool. Assumes that the array is sorted. */
char array_binary_search(void* array, void* element, qsort_cmp_t cmp);
struct linked_list array_to_ll(void* array);
+#endif /* CONTAINER_DISABLE_ARRAY */
/* ----------------------------------------------------------------- */
/* ----------------------LINKED LIST HEADER------------------------- */
/* ----------------------------------------------------------------- */
+#ifndef CONTAINER_DISABLE_LINKED_LIST
typedef void(*free_t)(void*);
struct __linked_list_meta {
size_t element_size;
@@ -292,11 +301,13 @@ void ll_set_free(struct linked_list* list, free_t new_free);
/* Creates a deep copy of the list, meaning that it must be free'd as any other list */
/* Uses `cpy` function to copy data to new location. If NULL, memcpy is used. */
struct linked_list ll_deep_copy(const struct linked_list* list, memcpy_t cpy);
+#endif /* CONTAINER_DISABLE_LINKED_LIST */
/* ----------------------------------------------------------------- */
/* ------------------------HASH MAP HEADER-------------------------- */
/* ----------------------------------------------------------------- */
+#ifndef CONTAINER_DISABLE_HMAP
#ifndef HMAP_MAX_BUCKET_SIZE
# define HMAP_MAX_BUCKET_SIZE 8
#endif
@@ -378,11 +389,13 @@ char hmapi_le(const struct hash_map_iter *a, const struct hash_map_iter* b);
char hmapi_ne(const struct hash_map_iter *a, const struct hash_map_iter* b);
/* 1 if `iter` is an end iterator, 0 otherwise */
char hmapi_end(const struct hash_map_iter* iter);
+#endif /* CONTAINER_DISABLE_HMAP */
/* ----------------------------------------------------------------- */
/* ------------------------HASH SET HEADER-------------------------- */
/* ----------------------------------------------------------------- */
+#ifndef CONTAINER_DISABLE_HSET
#ifndef HSET_MAX_BUCKET_SIZE
# define HSET_MAX_BUCKET_SIZE 8
#endif
@@ -454,12 +467,14 @@ char hseti_le(const struct hash_set_iter *a, const struct hash_set_iter* b);
char hseti_ne(const struct hash_set_iter *a, const struct hash_set_iter* b);
/* 1 if `iter` is an end iterator, 0 otherwise */
char hseti_end(const struct hash_set_iter* iter);
+#endif /* CONTAINER_DISABLE_HSET */
/* ------------------------------------------------------------------------- */
/* ------From now on, the rest of the header is implementation details------ */
/* -------------------the API and documentation end here-------------------- */
/* ------------------------------------------------------------------------- */
+#ifndef CONTAINER_DISABLE_ARRAY
enum _dynarray_header_idx {
_dah_idx_member_size = 0,
_dah_idx_capacity = 1,
@@ -481,6 +496,7 @@ void *_memshrink_array(void *dynarray);
void *_insert_to_index_dynarray(void *const dynarray, const void *const element, size_t el_size, size_t index);
void* _array_extend(void* array, void* buffer, size_t len);
void* _array_pop_at(void* array, size_t idx);
+#endif /* CONTAINER_DISABLE_ARRAY */
/* uncomment in dev mode so that LSP highlights the code */
/* #define CONTAINER_IMPLEMENTATION */
@@ -490,6 +506,7 @@ void* _array_pop_at(void* array, size_t idx);
/* ---------------------UTILITY IMPLEMENTATION---------------------- */
/* ----------------------------------------------------------------- */
+#ifndef CONTAINER_DISABLE_UTILITY
#ifndef __GNUC__
unsigned long __bit_scan_32(int32_t number) {
# ifdef _MSC_VER
@@ -572,12 +589,13 @@ const qsort_cmp_t __qsort_cmps[64] = {
0, 0, 0, __default_long_long_cmp,
};
#endif /* __GNUC__ */
-
+#endif /* CONTAINER_DISABLE_UTILITY */
/* ----------------------------------------------------------------- */
/* ----------------------ARRAY IMPLEMENTATION----------------------- */
/* ----------------------------------------------------------------- */
+#ifndef CONTAINER_DISABLE_ARRAY
void *_alloc_dynarray(size_t el_size, size_t len)
{
byte *data = (byte *)malloc(el_size * len + DYNARRAY_HEADER_SIZE);
@@ -699,10 +717,12 @@ struct linked_list array_to_ll(void* array) {
struct linked_list ret = ll_create_from_buffer(array_element_size(array), array, array_size(array));
return ret;
}
+#endif /* CONTAINER_DISABLE_ARRAY */
/* ----------------------------------------------------------------- */
/* ------------------LINKED LIST IMPLEMENTATION--------------------- */
/* ----------------------------------------------------------------- */
+#ifndef CONTAINER_DISABLE_LINKED_LIST
struct linked_list ll_create(size_t memb_size) {
struct __linked_list_meta meta = {
.assumed_size = 0,
@@ -1057,12 +1077,13 @@ struct linked_list ll_deep_copy(const struct linked_list* list, memcpy_t cpy) {
}
return ret;
}
-
+#endif /* CONTAINER_DISABLE_LINKED_LIST */
/* ----------------------------------------------------------------- */
/* ---------------------HASH MAP IMPLEMENTATION--------------------- */
/* ----------------------------------------------------------------- */
+#ifndef CONTAINER_DISABLE_HMAP
void __hmap_ll_custom_free(void* data) {
struct hmap_pair *pair = data;
free(pair->key);
@@ -1252,12 +1273,13 @@ char hmapi_ne(const struct hash_map_iter *a, const struct hash_map_iter* b) {
char hmapi_end(const struct hash_map_iter* iter) {
return iter->current_node == NULL || iter->bucket_pos == SIZE_MAX;
}
+#endif /* CONTAINER_DISABLE_HMAP */
/* ----------------------------------------------------------------- */
/* ---------------------HASH SET IMPLEMENTATION--------------------- */
/* ----------------------------------------------------------------- */
-
+#ifndef CONTAINER_DISABLE_HSET
struct hash_set hset_new(const size_t el_size, hset_equal_fn eq, hset_hash_fn hash) {
struct hash_set ret = {
.buckets = array_new(struct linked_list, HMAP_INIT_SIZE),
@@ -1416,8 +1438,9 @@ char hseti_ne(const struct hash_set_iter *a, const struct hash_set_iter* b) {
char hseti_end(const struct hash_set_iter* iter) {
return iter->current_node == NULL || iter->bucket_pos == SIZE_MAX;
}
+#endif /* CONTAINER_DISABLE_HSET */
#endif /* CONTAINER_IMPLEMENTATION */
#endif /* JUSTANOTHERCATGIRL_HEADERS_CONTAINER */
-
+#endif /* CONTAINER_DISABLE_ALL */
/* vim: set ts=8 noet: */
diff --git a/include/dynstring.h b/include/dynstring.h
deleted file mode 100644
index b3b23a0..0000000
--- a/include/dynstring.h
+++ /dev/null
@@ -1 +0,0 @@
-/* vim: set ts=8 noet: */
diff --git a/include/stringbuilder.h b/include/stringbuilder.h
new file mode 100644
index 0000000..e695082
--- /dev/null
+++ b/include/stringbuilder.h
@@ -0,0 +1,114 @@
+#ifndef JAC_STRINGBUILDER
+#define JAC_STRINGBUILDER
+
+#include <stddef.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct jac_sb {
+ char *data;
+ size_t size;
+ size_t cap;
+} jac_sb;
+
+jac_sb jac_sb_empty(void);
+jac_sb jac_sb_new(size_t size);
+jac_sb jac_sb_from_buf(const char* buf);
+jac_sb jac_sb_from_buf_n(const char* buf, size_t size);
+
+void jac_sb_append_buf(jac_sb *sb, const char *c);
+void jac_sb_append_buf_n(jac_sb *sb, const char *c, size_t n);
+void jac_sb_putc(jac_sb *sb, char c);
+void jac_sb_snprintf(jac_sb *sb, const char* fmt, ...);
+void jac_sb_vsnprintf(jac_sb *sb, const char* fmt, va_list list);
+
+void jac_sb_free(jac_sb sb);
+
+#define STRINGBUILDER_IMPLEMENTATION
+#ifdef STRINGBUILDER_IMPLEMENTATION
+
+#define __SB_REALLOC(sb, n) \
+do { \
+ if (sb->size + n >= sb->cap) { \
+ size_t rsz = 1UL << \
+ (64 - __builtin_clzl((unsigned long)(sb->size+n))); \
+ sb->data = realloc(sb->data, rsz + 1); \
+ sb->cap = rsz; \
+ } \
+ sb->size += n; \
+} while(0) \
+
+jac_sb jac_sb_empty(void) {
+ return (jac_sb){.data = calloc(1, 1), .size = 0, .cap = 0};
+}
+jac_sb jac_sb_new(size_t size) {
+ return (jac_sb){.data = calloc(size + 1, 1), .size = 0, .cap = size};
+}
+jac_sb jac_sb_from_buf(const char* buf) {
+ size_t ln = strlen(buf);
+ jac_sb ret = {.data = malloc(ln + 1), .size = ln, .cap = ln};
+ memcpy(ret.data, buf, ln);
+ ret.data[ret.size] = '\0';
+ return ret;
+}
+jac_sb jac_sb_from_buf_n(const char* buf, size_t size) {
+ jac_sb ret = {.data = malloc(size + 1), .size = size, .cap = size};
+ memcpy(ret.data, buf, size);
+ ret.data[size] = '\0';
+ return ret;
+}
+
+void jac_sb_append_buf(jac_sb *sb, const char *c) {
+ jac_sb_append_buf_n(sb, c, strlen(c));
+}
+void jac_sb_append_buf_n(jac_sb *sb, const char *c, size_t n) {
+ __SB_REALLOC(sb, n);
+ memcpy(sb->data + sb->size - n, c, n);
+ sb->data[sb->size] = '\0';
+}
+void jac_sb_putc(jac_sb *sb, char c) {
+ if (sb->size + 1 >= sb->cap) {
+ sb->cap <<= 1;
+ sb->data = realloc(sb->data, sb->cap + 1);
+ }
+ sb->data[sb->size++] = c;
+ sb->data[sb->size] = '\0';
+}
+
+void jac_sb_snprintf(jac_sb *sb, const char* fmt, ...) {
+ va_list l;
+ va_start(l, fmt);
+ jac_sb_vsnprintf(sb, fmt, l);
+}
+void jac_sb_vsnprintf(jac_sb *sb, const char* fmt, va_list list) {
+ va_list ls1;
+ size_t ln;
+try_write:
+ /*cap: 4, size: 2
+ * | a | b | \0 | \0 | \0 |
+ */
+ va_copy(ls1, list);
+ ln = vsnprintf(sb->data + sb->size, sb->cap - sb->size + 1, fmt, ls1);
+ va_end(ls1);
+ if (ln >= sb->cap - sb->size + 1) {
+ sb->cap <<= 1;
+ sb->data = realloc(sb->data, sb->cap + 1);
+ goto try_write;
+ }
+ va_end(list);
+ sb->size += ln;
+}
+
+void jac_sb_free(jac_sb sb) {
+ free(sb.data);
+}
+
+
+#undef __SB_REALLOC
+#endif /* STRINGBUILDER_IMPLEMENTATION */
+
+#endif /* JAC_STRINGBUILDER */
+
+/* vim: set ts=8 noet: */