94 lines
2.5 KiB
C
94 lines
2.5 KiB
C
#ifndef STR8_H
|
|
#define STR8_H
|
|
|
|
#include "aliases.h"
|
|
#include "mem_allocator.h"
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
#ifdef __cplusplus
|
|
BEGIN_C_LINKAGE
|
|
#endif // !__cplusplus
|
|
|
|
typedef struct str8 Str8;
|
|
struct str8 {
|
|
u64 capacity;
|
|
u64 size;
|
|
c8 *buf;
|
|
};
|
|
|
|
typedef const Str8 Str8RO;
|
|
|
|
typedef struct str8_node Str8Node;
|
|
struct str8_node {
|
|
Str8RO *string;
|
|
Str8Node *prev;
|
|
Str8Node *next;
|
|
};
|
|
|
|
typedef struct str8_list Str8List;
|
|
struct str8_list {
|
|
Str8Node *first;
|
|
Str8Node *last;
|
|
u64 total_size;
|
|
u64 node_count;
|
|
};
|
|
|
|
/**
|
|
* Utilities to be used with printf functions
|
|
*/
|
|
#define WAPP_STR8_SPEC "%.*s"
|
|
#define wapp_str8_varg(STRING) (int)STRING.size, STRING.buf
|
|
|
|
/**
|
|
* Str8 stack buffers
|
|
*/
|
|
#define wapp_str8_buf(CAPACITY) ((Str8){.capacity = CAPACITY, .size = 0, .buf = (c8[CAPACITY]){0}})
|
|
|
|
// Utilises the fact that memcpy returns pointer to dest buffer and that getting
|
|
// address of compound literals is valid in C to create a string on the stack
|
|
#define wapp_str8_lit(STRING) ((Str8){.capacity = (sizeof(STRING) - 1) * 2, \
|
|
.size = sizeof(STRING) - 1, \
|
|
.buf = memcpy(&((c8 [sizeof(STRING) * 2]){0}), STRING, sizeof(STRING))})
|
|
#define wapp_str8_lit_ro(STRING) ((Str8RO){.capacity = sizeof(STRING) - 1, \
|
|
.size = sizeof(STRING) - 1, \
|
|
.buf = (c8 *)STRING})
|
|
|
|
/**
|
|
* Str8 allocated buffers
|
|
*/
|
|
Str8 *wapp_str8_buf_alloc(const Allocator *allocator, u64 capacity);
|
|
Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str);
|
|
Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str);
|
|
|
|
/**
|
|
* Str8 utilities
|
|
*/
|
|
c8 wapp_str8_get(Str8RO *str, u64 index);
|
|
void wapp_str8_set(Str8 *str, u64 index, c8 c);
|
|
bool wapp_str8_equal(Str8RO *s1, Str8RO *s2);
|
|
Str8RO wapp_str8_substr(Str8RO *str, u64 start, u64 end);
|
|
|
|
/**
|
|
* Str8 find functions
|
|
*/
|
|
i64 wapp_str8_find(Str8RO *str, Str8RO substr);
|
|
i64 wapp_str8_rfind(Str8RO *str, Str8RO substr);
|
|
|
|
/**
|
|
* Str8 list functions
|
|
*/
|
|
Str8Node *wapp_str8_list_get(Str8List *list, u64 index);
|
|
void wapp_str8_list_push_front(Str8List *list, Str8Node *node);
|
|
void wapp_str8_list_push_back(Str8List *list, Str8Node *node);
|
|
void wapp_str8_list_insert(Str8List *list, Str8Node *node, u64 index);
|
|
Str8Node *wapp_str8_list_pop_front(Str8List *list);
|
|
Str8Node *wapp_str8_list_pop_back(Str8List *list);
|
|
Str8Node *wapp_str8_list_remove(Str8List *list, u64 index);
|
|
|
|
#ifdef __cplusplus
|
|
END_C_LINKAGE
|
|
#endif // !__cplusplus
|
|
|
|
#endif // !STR8_H
|