Init core
This commit is contained in:
30
.gitignore
vendored
Normal file
30
.gitignore
vendored
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
*.exe
|
||||||
|
*.exp
|
||||||
|
*.pdb
|
||||||
|
*.obj
|
||||||
|
*.json
|
||||||
|
*.ilk
|
||||||
|
*.lib
|
||||||
|
*.pdf
|
||||||
|
*.png
|
||||||
|
*.10x
|
||||||
|
*.sublime-project
|
||||||
|
*.sublime-workspace
|
||||||
|
backup.*
|
||||||
|
|
||||||
|
imgui.ini
|
||||||
|
|
||||||
|
__pycache__/
|
||||||
|
venv/
|
||||||
|
build/
|
||||||
|
vendor/
|
||||||
|
assets/
|
||||||
|
krzlinks/
|
||||||
|
misc/
|
||||||
|
generated/
|
||||||
|
html/
|
||||||
|
backup/
|
||||||
|
notes/
|
||||||
|
odin/
|
||||||
|
prototyping/
|
||||||
|
text_editor/
|
||||||
626
arena.h
Normal file
626
arena.h
Normal file
@@ -0,0 +1,626 @@
|
|||||||
|
/*
|
||||||
|
## MA_Arena version 2
|
||||||
|
A public domain, single-header-file library that provides easy to use
|
||||||
|
arena data structure.
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- linear allocator (MA_Arena)
|
||||||
|
- thread local scratch allocator (MA_Scratch) *optional*
|
||||||
|
- allocator abstraction (M_Allocator) *overridable*
|
||||||
|
- virtual memory abstraction (MV_Memory)
|
||||||
|
|
||||||
|
|
||||||
|
### Usage, do this in *one* C or C++ file:
|
||||||
|
```
|
||||||
|
#define MA_IMPLEMENTATION
|
||||||
|
#include "arena.h"
|
||||||
|
```
|
||||||
|
|
||||||
|
*/
|
||||||
|
#ifndef MA_HEADER
|
||||||
|
#define MA_HEADER
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#define MA_KIB(x) ((x##ull) * 1024ull)
|
||||||
|
#define MA_MIB(x) (MA_KIB(x) * 1024ull)
|
||||||
|
#define MA_GIB(x) (MA_MIB(x) * 1024ull)
|
||||||
|
#define MA_TIB(x) (MA_GIB(x) * 1024ull)
|
||||||
|
|
||||||
|
#ifndef MA_DEFAULT_RESERVE_SIZE
|
||||||
|
#define MA_DEFAULT_RESERVE_SIZE MA_GIB(1)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MA_DEFAULT_ALIGNMENT
|
||||||
|
#define MA_DEFAULT_ALIGNMENT 8
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MA_COMMIT_ADD_SIZE
|
||||||
|
#define MA_COMMIT_ADD_SIZE MA_MIB(4)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MA_INHERIT_HOOK
|
||||||
|
#define MA_INHERIT_HOOK
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MA_C_INHERIT_HOOK
|
||||||
|
#define MA_C_INHERIT_HOOK M_Allocator allocator;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MA_ZERO_IS_INITIALIZATION
|
||||||
|
#define MA_ZERO_IS_INITIALIZATION 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MA_INIT_HOOK
|
||||||
|
#define MA_INIT_HOOK(arena) \
|
||||||
|
arena->allocator.obj = (void *)arena; \
|
||||||
|
arena->allocator.p = (M_AllocatorProc *)MA_AllocatorProc;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MA_API
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#define MA_API extern "C"
|
||||||
|
#else
|
||||||
|
#define MA_API
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MA_THREAD_LOCAL
|
||||||
|
#if defined(__cplusplus) && __cplusplus >= 201103L
|
||||||
|
#define MA_THREAD_LOCAL thread_local
|
||||||
|
#elif defined(__GNUC__)
|
||||||
|
#define MA_THREAD_LOCAL __thread
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
#define MA_THREAD_LOCAL __declspec(thread)
|
||||||
|
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
|
||||||
|
#define MA_THREAD_LOCAL _Thread_local
|
||||||
|
#elif defined(__TINYC__)
|
||||||
|
#define MA_THREAD_LOCAL _Thread_local
|
||||||
|
#else
|
||||||
|
#error Couldnt figure out thread local, needs to be provided manually
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef enum M_AllocatorOp {
|
||||||
|
M_AllocatorOp_Invalid,
|
||||||
|
M_AllocatorOp_Allocate,
|
||||||
|
M_AllocatorOp_Deallocate,
|
||||||
|
} M_AllocatorOp;
|
||||||
|
|
||||||
|
typedef struct MV_Memory MV_Memory;
|
||||||
|
typedef struct MA_Checkpoint MA_Checkpoint;
|
||||||
|
typedef struct MA_Arena MA_Arena;
|
||||||
|
typedef struct M_Allocator M_Allocator;
|
||||||
|
typedef void *M_AllocatorProc(void *allocator, M_AllocatorOp kind, void *p, size_t size);
|
||||||
|
|
||||||
|
struct M_Allocator {
|
||||||
|
void *obj;
|
||||||
|
void *(*p)(void *allocator, M_AllocatorOp kind, void *p, size_t size);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct MV_Memory {
|
||||||
|
size_t commit;
|
||||||
|
size_t reserve;
|
||||||
|
uint8_t *data;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct MA_Arena MA_INHERIT_HOOK {
|
||||||
|
MA_C_INHERIT_HOOK
|
||||||
|
MV_Memory memory;
|
||||||
|
int alignment;
|
||||||
|
int saved_alignment;
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
size_t packed_array_element_size;
|
||||||
|
size_t packed_array_begin;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct MA_Checkpoint {
|
||||||
|
MA_Arena *arena;
|
||||||
|
size_t pos;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define MA_PushArrayNonZeroed(a, T, c) (T *)MA_PushSizeNonZeroed(a, sizeof(T) * (c))
|
||||||
|
#define MA_PushStructNonZeroed(a, T) (T *)MA_PushSizeNonZeroed(a, sizeof(T))
|
||||||
|
#define MA_PushStruct(a, T) (T *)MA_PushSize(a, sizeof(T))
|
||||||
|
#define MA_PushArray(a, T, c) (T *)MA_PushSize(a, sizeof(T) * (c))
|
||||||
|
#define MA_PushStructCopy(a, T, p) (T *)MA_PushCopy(a, (p), sizeof(T))
|
||||||
|
#define MA_BeginPackedArray(arena, T) (T *)MA__BeginPackedArray(arena, sizeof(T))
|
||||||
|
#define MA_CheckpointScope(name, InArena) for (MA_Checkpoint name = MA_Save(InArena); name.arena; (MA_Load(name), name.arena = 0))
|
||||||
|
|
||||||
|
#define M_AllocStruct(a, T) (T *)M_Alloc((a), sizeof(T))
|
||||||
|
#define M_AllocArray(a, T, c) (T *)M_Alloc((a), sizeof(T) * (c))
|
||||||
|
#define M_AllocStructCopy(a, T, p) (T *)M_PushCopy(a, (p), sizeof(T))
|
||||||
|
|
||||||
|
#define MA_IS_POW2(x) (((x) & ((x)-1)) == 0)
|
||||||
|
#define MA_MIN(x, y) ((x) <= (y) ? (x) : (y))
|
||||||
|
#define MA_MAX(x, y) ((x) >= (y) ? (x) : (y))
|
||||||
|
#define MA_LENGTHOF(x) ((int64_t)((sizeof(x) / sizeof((x)[0]))))
|
||||||
|
|
||||||
|
#define MA_CLAMP_TOP(x, max) ((x) >= (max) ? (max) : (x))
|
||||||
|
#define MA_CLAMP_BOT(x, min) ((x) <= (min) ? (min) : (x))
|
||||||
|
#define MA_CLAMP(x, min, max) ((x) >= (max) ? (max) : (x) <= (min) ? (min) \
|
||||||
|
: (x))
|
||||||
|
// clang-format off
|
||||||
|
MA_API void MA_MemoryZero(void *p, size_t size);
|
||||||
|
MA_API void MA_MemoryCopy(void *dst, void *src, size_t size);
|
||||||
|
MA_API size_t MA_GetAlignOffset(size_t size, size_t align);
|
||||||
|
MA_API size_t MA_AlignUp(size_t size, size_t align);
|
||||||
|
MA_API size_t MA_AlignDown(size_t size, size_t align);
|
||||||
|
MA_API void MA_DeallocateStub(MA_Arena *arena, void *p);
|
||||||
|
MA_API void MA_PopToPos(MA_Arena *arena, size_t pos);
|
||||||
|
MA_API void * MA_PopSize(MA_Arena *arena, size_t size);
|
||||||
|
MA_API void MA_DeallocateArena(MA_Arena *arena);
|
||||||
|
MA_API void MA_Reset(MA_Arena *arena);
|
||||||
|
MA_API void * MA__BeginPackedArray(MA_Arena *arena, size_t element_size);
|
||||||
|
MA_API int MA_EndPackedArray(MA_Arena *arena);
|
||||||
|
MA_API void MA_SetAlignment(MA_Arena *arena, int alignment);
|
||||||
|
MA_API uint8_t * MA_GetTop(MA_Arena *a);
|
||||||
|
MA_API void * MA_PushSizeNonZeroed(MA_Arena *a, size_t size);
|
||||||
|
MA_API void * MA_PushSize(MA_Arena *arena, size_t size);
|
||||||
|
MA_API void MA_InitEx(MA_Arena *a, size_t reserve);
|
||||||
|
MA_API void MA_Init(MA_Arena *a);
|
||||||
|
MA_API void MA_MakeSureInitialized(MA_Arena *a);
|
||||||
|
MA_API MA_Arena * MA_Bootstrap(void);
|
||||||
|
MA_API void MA_InitFromBuffer(MA_Arena *arena, void *buffer, size_t size);
|
||||||
|
MA_API MA_Arena MA_MakeFromBuffer(void *buffer, size_t size);
|
||||||
|
MA_API char * MA_PushStringCopy(MA_Arena *arena, char *p, size_t size);
|
||||||
|
MA_API void * MA_PushCopy(MA_Arena *arena, void *p, size_t size);
|
||||||
|
MA_API bool MA_IsPointerInside(MA_Arena *arena, void *p);
|
||||||
|
MA_API MA_Arena MA_PushArena(MA_Arena *arena, size_t size);
|
||||||
|
MA_API MA_Checkpoint MA_Save(MA_Arena *arena);
|
||||||
|
MA_API void MA_Load(MA_Checkpoint checkpoint);
|
||||||
|
MA_API MA_Checkpoint MA_GetScratchEx(MA_Arena **conflicts, int conflict_count);
|
||||||
|
MA_API MA_Checkpoint MA_GetScratch(void);
|
||||||
|
MA_API MA_Checkpoint MA_GetScratch1(MA_Arena *conflict);
|
||||||
|
MA_API void * MA_AllocatorProc(M_Allocator allocator, M_AllocatorOp kind, void *p, size_t size);
|
||||||
|
|
||||||
|
MA_API MV_Memory MV_Reserve(size_t size);
|
||||||
|
MA_API bool MV_Commit(MV_Memory *m, size_t commit);
|
||||||
|
MA_API void MV_Deallocate(MV_Memory *m);
|
||||||
|
MA_API bool MV_DecommitPos(MV_Memory *m, size_t pos);
|
||||||
|
|
||||||
|
MA_API void * M_AllocNonZeroed(M_Allocator allocator, size_t size);
|
||||||
|
MA_API void * M_Alloc(M_Allocator allocator, size_t size);
|
||||||
|
MA_API void * M_AllocCopy(M_Allocator allocator, void *p, size_t size);
|
||||||
|
MA_API void M_Dealloc(M_Allocator allocator, void *p);
|
||||||
|
MA_API M_Allocator M_GetSystemAllocator(void);
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
#ifndef MA_DISABLE_SCRATCH
|
||||||
|
extern MA_THREAD_LOCAL MA_Arena MA_ScratchArenaPool[];
|
||||||
|
#define MA_ScratchScope(x) for (MA_Checkpoint x = MA_GetScratch(); x.arena; (MA_ReleaseScratch(x), x.arena = 0))
|
||||||
|
#define MA_ReleaseScratch MA_Load
|
||||||
|
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
struct MA_Scratch {
|
||||||
|
MA_Checkpoint checkpoint;
|
||||||
|
MA_Scratch() { this->checkpoint = MA_GetScratch(); }
|
||||||
|
~MA_Scratch() { MA_Load(checkpoint); }
|
||||||
|
operator MA_Arena *() { return checkpoint.arena; }
|
||||||
|
|
||||||
|
private: // @Note: Disable copy constructors, cause its error prone
|
||||||
|
MA_Scratch(MA_Scratch &arena);
|
||||||
|
MA_Scratch(MA_Scratch &arena, MA_Scratch &a2);
|
||||||
|
};
|
||||||
|
#endif // __cplusplus
|
||||||
|
#endif // MA_DISABLE_SCRATCH
|
||||||
|
#endif // MA_HEADER
|
||||||
|
|
||||||
|
#ifdef MA_IMPLEMENTATION
|
||||||
|
|
||||||
|
#ifndef MA_ASSERT
|
||||||
|
#include <assert.h>
|
||||||
|
#define MA_ASSERT(x) assert(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MA_MemoryZero
|
||||||
|
#include <string.h>
|
||||||
|
MA_API void MA_MemoryZero(void *p, size_t size) {
|
||||||
|
memset(p, 0, size);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MA_MemoryCopy
|
||||||
|
#include <string.h>
|
||||||
|
MA_API void MA_MemoryCopy(void *dst, void *src, size_t size) {
|
||||||
|
memcpy(dst, src, size);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MA_CMalloc
|
||||||
|
#include <stdlib.h>
|
||||||
|
#define MA_CMalloc(x) malloc(x)
|
||||||
|
#define MA_CFree(x) free(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MA_FN
|
||||||
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
|
#define MA_FN __attribute__((unused)) static
|
||||||
|
#else
|
||||||
|
#define MA_FN static
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
MA_API size_t MA_GetAlignOffset(size_t size, size_t align) {
|
||||||
|
size_t mask = align - 1;
|
||||||
|
size_t val = size & mask;
|
||||||
|
if (val) {
|
||||||
|
val = align - val;
|
||||||
|
}
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API size_t MA_AlignUp(size_t size, size_t align) {
|
||||||
|
size_t result = size + MA_GetAlignOffset(size, align);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API size_t MA_AlignDown(size_t size, size_t align) {
|
||||||
|
size += 1; // Make sure when align is 8 doesn't get rounded down to 0
|
||||||
|
size_t result = size - (align - MA_GetAlignOffset(size, align));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_FN uint8_t *MV__AdvanceCommit(MV_Memory *m, size_t *commit_size, size_t page_size) {
|
||||||
|
size_t aligned_up_commit = MA_AlignUp(*commit_size, page_size);
|
||||||
|
size_t to_be_total_commited_size = aligned_up_commit + m->commit;
|
||||||
|
size_t to_be_total_commited_size_clamped_to_reserve = MA_CLAMP_TOP(to_be_total_commited_size, m->reserve);
|
||||||
|
size_t adjusted_to_boundary_commit = to_be_total_commited_size_clamped_to_reserve - m->commit;
|
||||||
|
MA_ASSERT(adjusted_to_boundary_commit && "Reached the virtual memory reserved boundary");
|
||||||
|
*commit_size = adjusted_to_boundary_commit;
|
||||||
|
|
||||||
|
if (adjusted_to_boundary_commit == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint8_t *result = m->data + m->commit;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void MA_DeallocateStub(MA_Arena *arena, void *p) {}
|
||||||
|
|
||||||
|
MA_API void MA_PopToPos(MA_Arena *arena, size_t pos) {
|
||||||
|
pos = MA_CLAMP_TOP(pos, arena->len);
|
||||||
|
arena->len = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void *MA_PopSize(MA_Arena *arena, size_t size) {
|
||||||
|
size = MA_CLAMP_TOP(size, arena->len);
|
||||||
|
arena->len -= size;
|
||||||
|
return arena->memory.data + arena->len;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void MA_DeallocateArena(MA_Arena *arena) {
|
||||||
|
MV_Deallocate(&arena->memory);
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void MA_Reset(MA_Arena *arena) {
|
||||||
|
MA_PopToPos(arena, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_FN size_t MA__AlignLen(MA_Arena *a) {
|
||||||
|
size_t align_offset = a->alignment ? MA_GetAlignOffset((uintptr_t)a->memory.data + (uintptr_t)a->len, a->alignment) : 0;
|
||||||
|
size_t aligned = a->len + align_offset;
|
||||||
|
return aligned;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void *MA__BeginPackedArray(MA_Arena *arena, size_t element_size) {
|
||||||
|
MA_ASSERT(arena->memory.data);
|
||||||
|
arena->len = MA__AlignLen(arena);
|
||||||
|
arena->saved_alignment = arena->alignment;
|
||||||
|
arena->alignment = 0;
|
||||||
|
arena->packed_array_begin = arena->len;
|
||||||
|
arena->packed_array_element_size = element_size;
|
||||||
|
void *result = arena->memory.data + arena->len;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API int MA_EndPackedArray(MA_Arena *arena) {
|
||||||
|
arena->alignment = arena->saved_alignment;
|
||||||
|
size_t different = (arena->len - arena->packed_array_begin);
|
||||||
|
int result = (int)((arena->len - arena->packed_array_begin) / arena->packed_array_element_size);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void MA_SetAlignment(MA_Arena *arena, int alignment) {
|
||||||
|
arena->alignment = alignment;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API uint8_t *MA_GetTop(MA_Arena *a) {
|
||||||
|
MA_ASSERT(a->memory.data);
|
||||||
|
return a->memory.data + a->len;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void *MA_PushSizeNonZeroed(MA_Arena *a, size_t size) {
|
||||||
|
size_t align_offset = a->alignment ? MA_GetAlignOffset((uintptr_t)a->memory.data + (uintptr_t)a->len, a->alignment) : 0;
|
||||||
|
size_t aligned_len = a->len + align_offset;
|
||||||
|
size_t size_with_alignment = size + align_offset;
|
||||||
|
|
||||||
|
if (a->len + size_with_alignment > a->memory.commit) {
|
||||||
|
if (a->memory.reserve == 0) {
|
||||||
|
#if MA_ZERO_IS_INITIALIZATION
|
||||||
|
MA_Init(a);
|
||||||
|
#else
|
||||||
|
MA_ASSERT("Pushing on uninitialized arena");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
bool result = MV_Commit(&a->memory, size_with_alignment + MA_COMMIT_ADD_SIZE);
|
||||||
|
MA_ASSERT(result && "Failed to commit memory");
|
||||||
|
(void)result;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t *result = a->memory.data + aligned_len;
|
||||||
|
a->len += size_with_alignment;
|
||||||
|
MA_ASSERT(a->len <= a->memory.commit);
|
||||||
|
return (void *)result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void *MA_PushSize(MA_Arena *arena, size_t size) {
|
||||||
|
void *result = MA_PushSizeNonZeroed(arena, size);
|
||||||
|
MA_MemoryZero(result, size);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void MA_InitEx(MA_Arena *a, size_t reserve) {
|
||||||
|
a->memory = MV_Reserve(reserve);
|
||||||
|
a->alignment = MA_DEFAULT_ALIGNMENT;
|
||||||
|
MA_INIT_HOOK(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void MA_Init(MA_Arena *a) {
|
||||||
|
MA_InitEx(a, MA_DEFAULT_RESERVE_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void MA_MakeSureInitialized(MA_Arena *a) {
|
||||||
|
if (a->memory.data == 0) {
|
||||||
|
MA_Init(a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API MA_Arena *MA_Bootstrap(void) {
|
||||||
|
MA_Arena bootstrap_arena = {0};
|
||||||
|
MA_Arena *arena = MA_PushStruct(&bootstrap_arena, MA_Arena);
|
||||||
|
*arena = bootstrap_arena;
|
||||||
|
arena->allocator.obj = arena;
|
||||||
|
return arena;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void MA_InitFromBuffer(MA_Arena *arena, void *buffer, size_t size) {
|
||||||
|
arena->memory.data = (uint8_t *)buffer;
|
||||||
|
arena->memory.commit = size;
|
||||||
|
arena->memory.reserve = size;
|
||||||
|
arena->alignment = MA_DEFAULT_ALIGNMENT;
|
||||||
|
MA_INIT_HOOK(arena);
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API MA_Arena MA_MakeFromBuffer(void *buffer, size_t size) {
|
||||||
|
MA_Arena arena;
|
||||||
|
MA_MemoryZero(&arena, sizeof(arena));
|
||||||
|
MA_InitFromBuffer(&arena, buffer, size);
|
||||||
|
return arena;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API char *MA_PushStringCopy(MA_Arena *arena, char *p, size_t size) {
|
||||||
|
char *copy_buffer = (char *)MA_PushSizeNonZeroed(arena, size + 1);
|
||||||
|
MA_MemoryCopy(copy_buffer, p, size);
|
||||||
|
copy_buffer[size] = 0;
|
||||||
|
return copy_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void *MA_PushCopy(MA_Arena *arena, void *p, size_t size) {
|
||||||
|
void *copy_buffer = MA_PushSizeNonZeroed(arena, size);
|
||||||
|
MA_MemoryCopy(copy_buffer, p, size);
|
||||||
|
return copy_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API bool MA_IsPointerInside(MA_Arena *arena, void *p) {
|
||||||
|
uintptr_t pointer = (uintptr_t)p;
|
||||||
|
uintptr_t start = (uintptr_t)arena->memory.data;
|
||||||
|
uintptr_t stop = start + (uintptr_t)arena->len;
|
||||||
|
bool result = pointer >= start && pointer < stop;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API MA_Arena MA_PushArena(MA_Arena *arena, size_t size) {
|
||||||
|
MA_Arena result;
|
||||||
|
MA_MemoryZero(&result, sizeof(result));
|
||||||
|
result.memory.data = MA_PushArrayNonZeroed(arena, uint8_t, size);
|
||||||
|
result.memory.commit = size;
|
||||||
|
result.memory.reserve = size;
|
||||||
|
result.alignment = arena->alignment;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API MA_Checkpoint MA_Save(MA_Arena *arena) {
|
||||||
|
MA_Checkpoint result;
|
||||||
|
result.pos = arena->len;
|
||||||
|
result.arena = arena;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void MA_Load(MA_Checkpoint checkpoint) {
|
||||||
|
MA_PopToPos(checkpoint.arena, checkpoint.pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void *M_AllocNonZeroed(M_Allocator allocator, size_t size) {
|
||||||
|
void *p = allocator.p(allocator.obj, M_AllocatorOp_Allocate, NULL, size);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void *M_Alloc(M_Allocator allocator, size_t size) {
|
||||||
|
void *p = allocator.p(allocator.obj, M_AllocatorOp_Allocate, NULL, size);
|
||||||
|
MA_MemoryZero(p, size);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void *M_AllocCopy(M_Allocator allocator, void *p, size_t size) {
|
||||||
|
void *copy_buffer = M_AllocNonZeroed(allocator, size);
|
||||||
|
MA_MemoryCopy(copy_buffer, p, size);
|
||||||
|
return copy_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void M_Dealloc(M_Allocator allocator, void *p) {
|
||||||
|
allocator.p(allocator.obj, M_AllocatorOp_Deallocate, p, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_FN void *M_ClibAllocatorProc(void *allocator, M_AllocatorOp kind, void *p, size_t size) {
|
||||||
|
if (kind == M_AllocatorOp_Allocate) {
|
||||||
|
return MA_CMalloc(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (kind == M_AllocatorOp_Deallocate) {
|
||||||
|
MA_CFree(p);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_ASSERT("MA_Arena invalid codepath");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void *MA_AllocatorProc(M_Allocator allocator, M_AllocatorOp kind, void *p, size_t size) {
|
||||||
|
if (kind == M_AllocatorOp_Allocate) {
|
||||||
|
return MA_PushSizeNonZeroed((MA_Arena *)allocator.obj, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (kind == M_AllocatorOp_Deallocate) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_ASSERT("MA_Arena invalid codepath");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API M_Allocator M_GetSystemAllocator(void) {
|
||||||
|
M_Allocator allocator;
|
||||||
|
allocator.obj = 0;
|
||||||
|
allocator.p = M_ClibAllocatorProc;
|
||||||
|
return allocator;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef MA_DISABLE_SCRATCH
|
||||||
|
MA_THREAD_LOCAL MA_Arena MA_ScratchArenaPool[4];
|
||||||
|
|
||||||
|
MA_API MA_Checkpoint MA_GetScratchEx(MA_Arena **conflicts, int conflict_count) {
|
||||||
|
MA_Arena *unoccupied = 0;
|
||||||
|
for (int i = 0; i < MA_LENGTHOF(MA_ScratchArenaPool); i += 1) {
|
||||||
|
MA_Arena *from_pool = MA_ScratchArenaPool + i;
|
||||||
|
unoccupied = from_pool;
|
||||||
|
for (int conflict_i = 0; conflict_i < conflict_count; conflict_i += 1) {
|
||||||
|
MA_Arena *from_conflict = conflicts[conflict_i];
|
||||||
|
if (from_pool == from_conflict) {
|
||||||
|
unoccupied = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (unoccupied) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_ASSERT(unoccupied);
|
||||||
|
MA_Checkpoint result = MA_Save(unoccupied);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API MA_Checkpoint MA_GetScratch(void) {
|
||||||
|
MA_Checkpoint result = MA_Save(MA_ScratchArenaPool + 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API MA_Checkpoint MA_GetScratch1(MA_Arena *conflict) {
|
||||||
|
MA_Arena *conflicts[] = {conflict};
|
||||||
|
return MA_GetScratchEx(conflicts, 1);
|
||||||
|
}
|
||||||
|
#endif // MA_DISABLE_SCRATCH
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#ifndef NOMINMAX
|
||||||
|
#define NOMINMAX
|
||||||
|
#endif
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#endif
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
const size_t MV__WIN32_PAGE_SIZE = 4096;
|
||||||
|
|
||||||
|
MA_API MV_Memory MV_Reserve(size_t size) {
|
||||||
|
MV_Memory result;
|
||||||
|
MA_MemoryZero(&result, sizeof(result));
|
||||||
|
size_t adjusted_size = MA_AlignUp(size, MV__WIN32_PAGE_SIZE);
|
||||||
|
result.data = (uint8_t *)VirtualAlloc(0, adjusted_size, MEM_RESERVE, PAGE_READWRITE);
|
||||||
|
MA_ASSERT(result.data && "Failed to reserve virtual memory");
|
||||||
|
result.reserve = adjusted_size;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API bool MV_Commit(MV_Memory *m, size_t commit) {
|
||||||
|
uint8_t *pointer = MV__AdvanceCommit(m, &commit, MV__WIN32_PAGE_SIZE);
|
||||||
|
if (pointer) {
|
||||||
|
void *result = VirtualAlloc(pointer, commit, MEM_COMMIT, PAGE_READWRITE);
|
||||||
|
MA_ASSERT(result && "Failed to commit more memory");
|
||||||
|
if (result) {
|
||||||
|
m->commit += commit;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void MV_Deallocate(MV_Memory *m) {
|
||||||
|
BOOL result = VirtualFree(m->data, 0, MEM_RELEASE);
|
||||||
|
MA_ASSERT(result != 0 && "Failed to release MV_Memory");
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API bool MV_DecommitPos(MV_Memory *m, size_t pos) {
|
||||||
|
size_t aligned = MA_AlignDown(pos, MV__WIN32_PAGE_SIZE);
|
||||||
|
size_t adjusted_pos = MA_CLAMP_TOP(aligned, m->commit);
|
||||||
|
size_t size_to_decommit = m->commit - adjusted_pos;
|
||||||
|
if (size_to_decommit) {
|
||||||
|
uint8_t *base_address = m->data + adjusted_pos;
|
||||||
|
BOOL result = VirtualFree(base_address, size_to_decommit, MEM_DECOMMIT);
|
||||||
|
if (result) {
|
||||||
|
m->commit -= size_to_decommit;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif __unix__ // _WIN32
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#define MV__UNIX_PAGE_SIZE 4096
|
||||||
|
MA_API MV_Memory MV_Reserve(size_t size) {
|
||||||
|
MV_Memory result = {};
|
||||||
|
size_t size_aligned = MA_AlignUp(size, MV__UNIX_PAGE_SIZE);
|
||||||
|
result.data = (uint8_t *)mmap(0, size_aligned, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||||
|
MA_ASSERT(result.data && "Failed to reserve memory using mmap!!");
|
||||||
|
if (result.data) {
|
||||||
|
result.reserve = size_aligned;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API bool MV_Commit(MV_Memory *m, size_t commit) {
|
||||||
|
uint8_t *pointer = MV__AdvanceCommit(m, &commit, MV__UNIX_PAGE_SIZE);
|
||||||
|
if (pointer) {
|
||||||
|
int mprotect_result = mprotect(pointer, commit, PROT_READ | PROT_WRITE);
|
||||||
|
MA_ASSERT(mprotect_result == 0 && "Failed to commit more memory using mmap");
|
||||||
|
if (mprotect_result == 0) {
|
||||||
|
m->commit += commit;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_API void MV_Deallocate(MV_Memory *m) {
|
||||||
|
int result = munmap(m->data, m->reserve);
|
||||||
|
MA_ASSERT(result == 0 && "Failed to release virtual memory using munmap");
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#error "unhandled arena platform"
|
||||||
|
#endif // __unix__
|
||||||
|
#endif // MA_IMPL
|
||||||
315
array.hpp
Normal file
315
array.hpp
Normal file
@@ -0,0 +1,315 @@
|
|||||||
|
// #define ARRAY_ALLOCATOR_TYPE Allocator
|
||||||
|
|
||||||
|
#ifndef ARRAY_PRIVATE_FUNCTION
|
||||||
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
|
#define ARRAY_PRIVATE_FUNCTION __attribute__((unused)) static
|
||||||
|
#else
|
||||||
|
#define ARRAY_PRIVATE_FUNCTION static
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef ARRAY_ALLOCATE
|
||||||
|
#include <stdlib.h>
|
||||||
|
#define ARRAY_ALLOCATE(allocator, size) malloc(size)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef ARRAY_DEALLOCATE
|
||||||
|
#include <stdlib.h>
|
||||||
|
#define ARRAY_DEALLOCATE(allocator, p) free(p)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef ARRAY_ASSERT
|
||||||
|
#include <assert.h>
|
||||||
|
#define ARRAY_ASSERT(x) assert(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef ARRAY_MemoryMove
|
||||||
|
#include <string.h>
|
||||||
|
#define ARRAY_MemoryMove(dst, src, size) memmove(dst, src, size)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef ARRAY_SET_DEFAULT_ALLOCATOR
|
||||||
|
#define ARRAY_SET_DEFAULT_ALLOCATOR
|
||||||
|
// Example:
|
||||||
|
// #define ARRAY_SET_DEFAULT_ALLOCATOR if (!allocator) allocator = global_heap;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef DEFER_HEADER
|
||||||
|
#define ForArrayRemovable(a) for (int __i = 0; __i < (a).len; __i += 1)
|
||||||
|
#define ForArrayRemovablePrepare(a) \
|
||||||
|
auto &it = (a)[__i]; \
|
||||||
|
bool remove_it = false; \
|
||||||
|
defer { \
|
||||||
|
if (remove_it) { \
|
||||||
|
(a).ordered_remove(it); \
|
||||||
|
__i -= 1; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
#define ForArrayRemovableDeclare() (remove_it = true)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(ARRAY_ALLOCATOR_CODE)
|
||||||
|
#if defined(ARRAY_ALLOCATOR_TYPE)
|
||||||
|
#define ARRAY_ALLOCATOR_CODE(x) x
|
||||||
|
#define ARRAY_ALLOCATOR_PARAM ARRAY_ALLOCATOR_TYPE allocator,
|
||||||
|
#else
|
||||||
|
#define ARRAY_ALLOCATOR_CODE(x)
|
||||||
|
#define ARRAY_ALLOCATOR_PARAM
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(For)
|
||||||
|
#define For2(array,it) for(auto &it : (array))
|
||||||
|
#define For(array) For2(array,it)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
struct Array {
|
||||||
|
ARRAY_ALLOCATOR_CODE(ARRAY_ALLOCATOR_TYPE allocator;)
|
||||||
|
T *data;
|
||||||
|
int cap, len;
|
||||||
|
|
||||||
|
T &operator[](int index) {
|
||||||
|
ARRAY_ASSERT(index >= 0 && index < len);
|
||||||
|
return data[index];
|
||||||
|
}
|
||||||
|
T &operator[](long long index) {
|
||||||
|
ARRAY_ASSERT(index >= 0 && index < len);
|
||||||
|
return data[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_first(T &item) { return &item == first(); }
|
||||||
|
bool is_last(T &item) { return &item == last(); }
|
||||||
|
|
||||||
|
bool contains(T *item) {
|
||||||
|
bool result = item >= data && item < data + len;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_index(T &item) {
|
||||||
|
ARRAY_ASSERT((data <= &item) && ((data + len) > &item));
|
||||||
|
size_t offset = &item - data;
|
||||||
|
return (int)offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
void add(T item) {
|
||||||
|
try_growing();
|
||||||
|
data[len++] = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Struct needs to have 'value_to_sort_by' field
|
||||||
|
void sorted_insert_decreasing(T item) {
|
||||||
|
int insert_index = -1;
|
||||||
|
For(*this) {
|
||||||
|
if (it.value_to_sort_by <= item.value_to_sort_by) {
|
||||||
|
insert_index = get_index(it);
|
||||||
|
insert(item, insert_index);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (insert_index == -1) {
|
||||||
|
add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bounded_add(T item) {
|
||||||
|
ARRAY_ASSERT(len + 1 <= cap);
|
||||||
|
try_growing(); // in case of error
|
||||||
|
data[len++] = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
T *alloc(const T &item) {
|
||||||
|
try_growing();
|
||||||
|
T *ref = data + len++;
|
||||||
|
*ref = item;
|
||||||
|
return ref;
|
||||||
|
}
|
||||||
|
|
||||||
|
T *alloc() {
|
||||||
|
try_growing();
|
||||||
|
T *ref = data + len++;
|
||||||
|
*ref = {};
|
||||||
|
return ref;
|
||||||
|
}
|
||||||
|
|
||||||
|
T *alloc_multiple(int size) {
|
||||||
|
try_growing_to_fit_item_count(size);
|
||||||
|
T *result = data + len;
|
||||||
|
len += size;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_array(T *items, int item_count) {
|
||||||
|
for (int i = 0; i < item_count; i += 1) {
|
||||||
|
add(items[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_array(Array<T> items) {
|
||||||
|
add_array(items.data, items.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void reserve(int size) {
|
||||||
|
if (size > cap) {
|
||||||
|
ARRAY_SET_DEFAULT_ALLOCATOR;
|
||||||
|
|
||||||
|
void *p = ARRAY_ALLOCATE(allocator, size * sizeof(T));
|
||||||
|
ARRAY_ASSERT(p);
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
ARRAY_MemoryMove(p, data, len * sizeof(T));
|
||||||
|
ARRAY_DEALLOCATE(allocator, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
data = (T *)p;
|
||||||
|
cap = size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void init(ARRAY_ALLOCATOR_PARAM int size) {
|
||||||
|
len = 0;
|
||||||
|
cap = 0;
|
||||||
|
data = 0;
|
||||||
|
ARRAY_ALLOCATOR_CODE(this->allocator = allocator;)
|
||||||
|
reserve(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
T pop() {
|
||||||
|
ARRAY_ASSERT(len > 0);
|
||||||
|
return data[--len];
|
||||||
|
}
|
||||||
|
|
||||||
|
void unordered_remove(T &item) { // DONT USE IN LOOPS !!!!
|
||||||
|
ARRAY_ASSERT(len > 0);
|
||||||
|
ARRAY_ASSERT(&item >= begin() && &item < end());
|
||||||
|
item = data[--len];
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_index(const T &item) {
|
||||||
|
ptrdiff_t index = (ptrdiff_t)(&item - data);
|
||||||
|
ARRAY_ASSERT(index >= 0 && index < len);
|
||||||
|
// ARRAY_ASSERT(index > INT_MIN && index < INT_MAX);
|
||||||
|
return (int)index;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ordered_remove(T &item) { // DONT USE IN LOOPS !!!
|
||||||
|
ARRAY_ASSERT(len > 0);
|
||||||
|
ARRAY_ASSERT(&item >= begin() && &item < end());
|
||||||
|
int index = get_index(item);
|
||||||
|
ARRAY_ASSERT(index >= 0 && index < len);
|
||||||
|
|
||||||
|
int right_len = len - index - 1;
|
||||||
|
ARRAY_MemoryMove(data + index, data + index + 1, right_len * sizeof(T));
|
||||||
|
len -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void insert(T item, int index) {
|
||||||
|
if (index == len) {
|
||||||
|
add(item);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ARRAY_ASSERT(index < len);
|
||||||
|
ARRAY_ASSERT(index >= 0);
|
||||||
|
|
||||||
|
try_growing();
|
||||||
|
int right_len = len - index;
|
||||||
|
ARRAY_MemoryMove(data + index + 1, data + index, sizeof(T) * right_len);
|
||||||
|
data[index] = item;
|
||||||
|
len += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void dealloc() {
|
||||||
|
if (data) ARRAY_DEALLOCATE(allocator, data);
|
||||||
|
data = 0;
|
||||||
|
len = cap = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Array<T> exact_copy(ARRAY_ALLOCATOR_CODE(ARRAY_ALLOCATOR_TYPE *allocator)) {
|
||||||
|
Array result = {};
|
||||||
|
ARRAY_ALLOCATOR_CODE(result.allocator = allocator;)
|
||||||
|
result.reserve(cap);
|
||||||
|
|
||||||
|
ARRAY_MemoryMove(result.data, data, sizeof(T) * len);
|
||||||
|
result.len = len;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Array<T> tight_copy(ARRAY_ALLOCATOR_CODE(ARRAY_ALLOCATOR_TYPE *allocator)) {
|
||||||
|
Array result = {};
|
||||||
|
ARRAY_ALLOCATOR_CODE(result.allocator = allocator;)
|
||||||
|
result.reserve(len);
|
||||||
|
|
||||||
|
ARRAY_MemoryMove(result.data, data, sizeof(T) * len);
|
||||||
|
result.len = len;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
T *first() {
|
||||||
|
ARRAY_ASSERT(len > 0);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
T *last() {
|
||||||
|
ARRAY_ASSERT(len > 0);
|
||||||
|
return data + len - 1;
|
||||||
|
}
|
||||||
|
T *front() {
|
||||||
|
ARRAY_ASSERT(len > 0);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
T *back() {
|
||||||
|
ARRAY_ASSERT(len > 0);
|
||||||
|
return data + len - 1;
|
||||||
|
}
|
||||||
|
T *begin() { return data; }
|
||||||
|
T *end() { return data + len; }
|
||||||
|
|
||||||
|
// for (auto it = integers.begin(), end = integers.end(); it != end; ++it)
|
||||||
|
struct Reverse_Iter {
|
||||||
|
T *data;
|
||||||
|
Array<T> *arr;
|
||||||
|
|
||||||
|
Reverse_Iter operator++(int) {
|
||||||
|
Reverse_Iter ret = *this;
|
||||||
|
data -= 1;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
Reverse_Iter &operator++() {
|
||||||
|
data -= 1;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
T &operator*() { return data[0]; }
|
||||||
|
T *operator->() { return data; }
|
||||||
|
|
||||||
|
friend bool operator==(const Reverse_Iter &a, const Reverse_Iter &b) { return a.data == b.data; };
|
||||||
|
friend bool operator!=(const Reverse_Iter &a, const Reverse_Iter &b) { return a.data != b.data; };
|
||||||
|
|
||||||
|
Reverse_Iter begin() { return Reverse_Iter{arr->end() - 1, arr}; }
|
||||||
|
Reverse_Iter end() { return Reverse_Iter{arr->begin() - 1, arr}; }
|
||||||
|
};
|
||||||
|
|
||||||
|
Reverse_Iter reverse() { return {end() - 1, this}; }
|
||||||
|
|
||||||
|
void try_growing() {
|
||||||
|
if (len + 1 > cap) {
|
||||||
|
int new_size = cap * 2;
|
||||||
|
if (new_size < 16) new_size = 16;
|
||||||
|
|
||||||
|
reserve(new_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void try_growing_to_fit_item_count(int item_count) {
|
||||||
|
if (len + item_count > cap) {
|
||||||
|
int new_size = (cap + item_count) * 2;
|
||||||
|
if (new_size < 16) new_size = 16;
|
||||||
|
reserve(new_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
34
core.c
Normal file
34
core.c
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#include "core.h"
|
||||||
|
#define S8_IMPLEMENTATION
|
||||||
|
|
||||||
|
#define IO_IMPLEMENTATION
|
||||||
|
#define IO_VSNPRINTF stbsp_vsnprintf
|
||||||
|
#define IO_SNPRINTF stbsp_snprintf
|
||||||
|
#include "io.h"
|
||||||
|
|
||||||
|
#define STB_SPRINTF_IMPLEMENTATION
|
||||||
|
#include "stb_sprintf.h"
|
||||||
|
|
||||||
|
#define MA_IMPLEMENTATION
|
||||||
|
#define MA_ASSERT(x) IO_Assert(x)
|
||||||
|
#include "arena.h"
|
||||||
|
|
||||||
|
#define RE_IMPLEMENTATION
|
||||||
|
#define RE_ASSERT(x) IO_Assert(x)
|
||||||
|
#include "regex.h"
|
||||||
|
|
||||||
|
#define UTF_IMPLEMENTATION
|
||||||
|
#include "unicode.h"
|
||||||
|
|
||||||
|
#define S8_VSNPRINTF stbsp_vsnprintf
|
||||||
|
#define S8_ALLOCATE(allocator, size) MA_PushSize(allocator, size)
|
||||||
|
#define S8_ASSERT(x) IO_Assert(x)
|
||||||
|
#define S8_MemoryCopy MA_MemoryCopy
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
#define MU_IMPLEMENTATION
|
||||||
|
#define MU_ASSERT IO_Assert
|
||||||
|
#include "multimedia.h"
|
||||||
|
#include "hash.c"
|
||||||
|
#include "load_library.c"
|
||||||
|
#include "filesystem.c"
|
||||||
26
core.h
Normal file
26
core.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "preproc_env.h"
|
||||||
|
#include "stb_sprintf.h"
|
||||||
|
#include "io.h"
|
||||||
|
#include "arena.h"
|
||||||
|
#include "string.h"
|
||||||
|
#include "unicode.h"
|
||||||
|
#include "hash.h"
|
||||||
|
#include "linked_list.h"
|
||||||
|
#include "regex.h"
|
||||||
|
#include "multimedia.h"
|
||||||
|
#include "load_library.h"
|
||||||
|
#include "filesystem.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#include "defer.hpp"
|
||||||
|
#define TABLE_ASSERT IO_Assert
|
||||||
|
#define TABLE_SET_DEFAULT_ALLOCATOR \
|
||||||
|
if (!allocator.p) allocator = M_GetSystemAllocator();
|
||||||
|
#include "table.hpp"
|
||||||
|
#define ARRAY_ASSERT IO_Assert
|
||||||
|
#define ARRAY_ALLOCATOR_TYPE M_Allocator
|
||||||
|
#define ARRAY_SET_DEFAULT_ALLOCATOR \
|
||||||
|
if (!allocator.p) allocator = M_GetSystemAllocator();
|
||||||
|
#include "array.hpp"
|
||||||
|
#endif
|
||||||
21
defer.hpp
Normal file
21
defer.hpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#define DEFER_HEADER
|
||||||
|
template <typename T>
|
||||||
|
struct DEFER_ExitScope {
|
||||||
|
T lambda;
|
||||||
|
DEFER_ExitScope(T lambda) : lambda(lambda) {}
|
||||||
|
~DEFER_ExitScope() { lambda(); }
|
||||||
|
DEFER_ExitScope(const DEFER_ExitScope &i) : lambda(i.lambda){};
|
||||||
|
|
||||||
|
private:
|
||||||
|
DEFER_ExitScope &operator=(const DEFER_ExitScope &);
|
||||||
|
};
|
||||||
|
|
||||||
|
class DEFER_ExitScopeHelp {
|
||||||
|
public:
|
||||||
|
template <typename T>
|
||||||
|
DEFER_ExitScope<T> operator+(T t) { return t; }
|
||||||
|
};
|
||||||
|
|
||||||
|
#define DEFER_CONCAT_INTERNAL(x, y) x##y
|
||||||
|
#define DEFER_CONCAT(x, y) DEFER_CONCAT_INTERNAL(x, y)
|
||||||
|
#define defer const auto DEFER_CONCAT(defer__, __LINE__) = DEFER_ExitScopeHelp() + [&]()
|
||||||
513
filesystem.c
Normal file
513
filesystem.c
Normal file
@@ -0,0 +1,513 @@
|
|||||||
|
#include "filesystem.h"
|
||||||
|
|
||||||
|
#define OS_LENGTHOF(x) ((int64_t)((sizeof(x) / sizeof((x)[0]))))
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#ifndef NOMINMAX
|
||||||
|
#define NOMINMAX
|
||||||
|
#endif
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#endif
|
||||||
|
#include <windows.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
OS_API bool WIN_EnableTerminalColors(void) {
|
||||||
|
// Enable color terminal output
|
||||||
|
{
|
||||||
|
// Set output mode to handle virtual terminal sequences
|
||||||
|
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
if (hOut != INVALID_HANDLE_VALUE) {
|
||||||
|
DWORD dwMode = 0;
|
||||||
|
if (GetConsoleMode(hOut, &dwMode)) {
|
||||||
|
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
||||||
|
if (SetConsoleMode(hOut, dwMode)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
IO_Printf("Failed to enable colored terminal output C\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
IO_Printf("Failed to enable colored terminal output B\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
IO_Printf("Failed to enable colored terminal output A\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API void DEV_SetWorkingDir(void) {
|
||||||
|
// Enable color terminal output
|
||||||
|
{
|
||||||
|
// Set output mode to handle virtual terminal sequences
|
||||||
|
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
if (hOut != INVALID_HANDLE_VALUE) {
|
||||||
|
DWORD dwMode = 0;
|
||||||
|
if (GetConsoleMode(hOut, &dwMode)) {
|
||||||
|
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
||||||
|
if (SetConsoleMode(hOut, dwMode)) {
|
||||||
|
// COLOR CODES ENABLED
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
IO_Printf("Failed to enable colored terminal output C\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
IO_Printf("Failed to enable colored terminal output B\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
IO_Printf("Failed to enable colored terminal output A\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_Checkpoint scratch = MA_GetScratch();
|
||||||
|
S8_String working_dir = OS_GetWorkingDir(scratch.arena);
|
||||||
|
if (S8_EndsWith(working_dir, S8_Lit("build/"), S8_IGNORE_CASE)) {
|
||||||
|
OS_SetWorkingDir(S8_Lit(".."));
|
||||||
|
}
|
||||||
|
MA_ReleaseScratch(scratch);
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API bool OS_IsAbsolute(S8_String path) {
|
||||||
|
bool result = path.len > 3 && CHAR_IsAlphabetic(path.str[0]) && path.str[1] == ':' && path.str[2] == '/';
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API S8_String OS_GetExePath(MA_Arena *arena) {
|
||||||
|
wchar_t wbuffer[1024];
|
||||||
|
DWORD wsize = GetModuleFileNameW(0, wbuffer, OS_LENGTHOF(wbuffer));
|
||||||
|
IO_Assert(wsize != 0);
|
||||||
|
|
||||||
|
S8_String path = UTF_CreateStringFromWidechar(arena, wbuffer, wsize);
|
||||||
|
S8_NormalizePath(path);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API S8_String OS_GetExeDir(MA_Arena *arena) {
|
||||||
|
S8_String path = OS_GetExePath(arena);
|
||||||
|
path = S8_ChopLastSlash(path);
|
||||||
|
path.str[path.len] = 0;
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API S8_String OS_GetWorkingDir(MA_Arena *arena) {
|
||||||
|
wchar_t wbuffer[1024];
|
||||||
|
DWORD wsize = GetCurrentDirectoryW(OS_LENGTHOF(wbuffer), wbuffer);
|
||||||
|
IO_Assert(wsize != 0);
|
||||||
|
IO_Assert(wsize < 1022);
|
||||||
|
wbuffer[wsize++] = '/';
|
||||||
|
wbuffer[wsize] = 0;
|
||||||
|
|
||||||
|
S8_String path = UTF_CreateStringFromWidechar(arena, wbuffer, wsize);
|
||||||
|
S8_NormalizePath(path);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API void OS_SetWorkingDir(S8_String path) {
|
||||||
|
wchar_t wpath[1024];
|
||||||
|
UTF_CreateWidecharFromChar(wpath, OS_LENGTHOF(wpath), path.str, path.len);
|
||||||
|
SetCurrentDirectoryW(wpath);
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API S8_String OS_GetAbsolutePath(MA_Arena *arena, S8_String relative) {
|
||||||
|
wchar_t wpath[1024];
|
||||||
|
UTF_CreateWidecharFromChar(wpath, OS_LENGTHOF(wpath), relative.str, relative.len);
|
||||||
|
wchar_t wpath_abs[1024];
|
||||||
|
DWORD written = GetFullPathNameW((wchar_t *)wpath, OS_LENGTHOF(wpath_abs), wpath_abs, 0);
|
||||||
|
if (written == 0)
|
||||||
|
return S8_MakeEmpty();
|
||||||
|
S8_String path = UTF_CreateStringFromWidechar(arena, wpath_abs, written);
|
||||||
|
S8_NormalizePath(path);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API bool OS_FileExists(S8_String path) {
|
||||||
|
wchar_t wbuff[1024];
|
||||||
|
UTF_CreateWidecharFromChar(wbuff, OS_LENGTHOF(wbuff), path.str, path.len);
|
||||||
|
DWORD attribs = GetFileAttributesW(wbuff);
|
||||||
|
bool result = attribs == INVALID_FILE_ATTRIBUTES ? false : true;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API bool OS_IsDir(S8_String path) {
|
||||||
|
wchar_t wbuff[1024];
|
||||||
|
UTF_CreateWidecharFromChar(wbuff, OS_LENGTHOF(wbuff), path.str, path.len);
|
||||||
|
DWORD dwAttrib = GetFileAttributesW(wbuff);
|
||||||
|
return dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API bool OS_IsFile(S8_String path) {
|
||||||
|
wchar_t wbuff[1024];
|
||||||
|
UTF_CreateWidecharFromChar(wbuff, OS_LENGTHOF(wbuff), path.str, path.len);
|
||||||
|
DWORD dwAttrib = GetFileAttributesW(wbuff);
|
||||||
|
bool is_file = (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) == 0;
|
||||||
|
return dwAttrib != INVALID_FILE_ATTRIBUTES && is_file;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API double OS_GetTime(void) {
|
||||||
|
static int64_t counts_per_second;
|
||||||
|
if (counts_per_second == 0) {
|
||||||
|
LARGE_INTEGER freq;
|
||||||
|
QueryPerformanceFrequency(&freq);
|
||||||
|
counts_per_second = freq.QuadPart;
|
||||||
|
}
|
||||||
|
|
||||||
|
LARGE_INTEGER time;
|
||||||
|
QueryPerformanceCounter(&time);
|
||||||
|
double result = (double)time.QuadPart / (double)counts_per_second;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @todo: I think we want a list with both relative + absolute + other things
|
||||||
|
//
|
||||||
|
// Returns directories with slash at the end '/'
|
||||||
|
// By default returns absolute paths
|
||||||
|
OS_API S8_List OS_ListDir(MA_Arena *arena, S8_String path, unsigned flags) {
|
||||||
|
MA_Checkpoint scratch = MA_GetScratch1(arena);
|
||||||
|
S8_List dirs_to_read = S8_MakeEmptyList();
|
||||||
|
S8_List result = S8_MakeEmptyList();
|
||||||
|
S8_AddNode(scratch.arena, &dirs_to_read, path);
|
||||||
|
|
||||||
|
for (S8_Node *it = dirs_to_read.first; it; it = it->next) {
|
||||||
|
wchar_t wbuff[1024];
|
||||||
|
S8_String modified_path = S8_Format(scratch.arena, "%.*s\\*", (int)it->string.len, it->string.str);
|
||||||
|
IO_Assert(modified_path.len < OS_LENGTHOF(wbuff));
|
||||||
|
int64_t wsize = UTF_CreateWidecharFromChar(wbuff, OS_LENGTHOF(wbuff), modified_path.str, modified_path.len);
|
||||||
|
IO_Assert(wsize);
|
||||||
|
|
||||||
|
WIN32_FIND_DATAW ffd;
|
||||||
|
HANDLE handle = FindFirstFileW(wbuff, &ffd);
|
||||||
|
if (handle == INVALID_HANDLE_VALUE)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
do {
|
||||||
|
|
||||||
|
//
|
||||||
|
// Skip '.' and '..'
|
||||||
|
//
|
||||||
|
if (ffd.cFileName[0] == '.') {
|
||||||
|
if (ffd.cFileName[1] == '.') {
|
||||||
|
if (ffd.cFileName[2] == 0)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ffd.cFileName[1] == 0)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool dir = ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
|
||||||
|
S8_String filename = UTF_CreateStringFromWidechar(scratch.arena, ffd.cFileName, S8_WideLength(ffd.cFileName));
|
||||||
|
S8_String rel_abs_path = S8_Format(scratch.arena, "%Q/%Q%Q", it->string, filename, dir ? S8_Lit("/") : S8_Lit(""));
|
||||||
|
if (flags & OS_RELATIVE_PATHS) {
|
||||||
|
S8_AddNode(arena, &result, rel_abs_path);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
S8_String abs_path = OS_GetAbsolutePath(arena, rel_abs_path);
|
||||||
|
S8_AddNode(arena, &result, abs_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dir && flags & OS_RECURSIVE) {
|
||||||
|
S8_AddNode(scratch.arena, &dirs_to_read, rel_abs_path);
|
||||||
|
}
|
||||||
|
} while (FindNextFileW(handle, &ffd) != 0);
|
||||||
|
|
||||||
|
DWORD error = GetLastError();
|
||||||
|
if (error != ERROR_NO_MORE_FILES) {
|
||||||
|
// Not sure what to do here hmmm
|
||||||
|
}
|
||||||
|
FindClose(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
MA_ReleaseScratch(scratch);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API OS_Result OS_MakeDir(S8_String path) {
|
||||||
|
wchar_t wpath[1024];
|
||||||
|
UTF_CreateWidecharFromChar(wpath, OS_LENGTHOF(wpath), path.str, path.len);
|
||||||
|
BOOL success = CreateDirectoryW(wpath, NULL);
|
||||||
|
OS_Result result = OS_SUCCESS;
|
||||||
|
if (success == 0) {
|
||||||
|
DWORD error = GetLastError();
|
||||||
|
if (error == ERROR_ALREADY_EXISTS) {
|
||||||
|
result = OS_ALREADY_EXISTS;
|
||||||
|
}
|
||||||
|
else if (error == ERROR_PATH_NOT_FOUND) {
|
||||||
|
result = OS_PATH_NOT_FOUND;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
IO_Assert(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API OS_Result OS_CopyFile(S8_String from, S8_String to, bool overwrite) {
|
||||||
|
wchar_t wfrom[1024];
|
||||||
|
UTF_CreateWidecharFromChar(wfrom, OS_LENGTHOF(wfrom), from.str, from.len);
|
||||||
|
|
||||||
|
wchar_t wto[1024];
|
||||||
|
UTF_CreateWidecharFromChar(wto, OS_LENGTHOF(wto), to.str, to.len);
|
||||||
|
|
||||||
|
BOOL fail_if_exists = !overwrite;
|
||||||
|
BOOL success = CopyFileW(wfrom, wto, fail_if_exists);
|
||||||
|
|
||||||
|
OS_Result result = OS_SUCCESS;
|
||||||
|
if (success == FALSE)
|
||||||
|
result = OS_FAILURE;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API OS_Result OS_DeleteFile(S8_String path) {
|
||||||
|
wchar_t wpath[1024];
|
||||||
|
UTF_CreateWidecharFromChar(wpath, OS_LENGTHOF(wpath), path.str, path.len);
|
||||||
|
BOOL success = DeleteFileW(wpath);
|
||||||
|
OS_Result result = OS_SUCCESS;
|
||||||
|
if (success == 0)
|
||||||
|
result = OS_PATH_NOT_FOUND;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API OS_Result OS_DeleteDir(S8_String path, unsigned flags) {
|
||||||
|
if (flags & OS_RECURSIVE) {
|
||||||
|
MA_Checkpoint scratch = MA_GetScratch();
|
||||||
|
S8_List list = OS_ListDir(scratch.arena, path, OS_RECURSIVE);
|
||||||
|
S8_Node *dirs_to_remove = 0;
|
||||||
|
for (S8_Node *it = list.first; it; it = it->next) {
|
||||||
|
if (!S8_EndsWith(it->string, S8_Lit("/"), S8_IGNORE_CASE)) {
|
||||||
|
OS_DeleteFile(it->string);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
S8_Node *node = S8_CreateNode(scratch.arena, it->string);
|
||||||
|
SLL_STACK_ADD(dirs_to_remove, node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (S8_Node *it = dirs_to_remove; it; it = it->next) {
|
||||||
|
OS_DeleteDir(it->string, OS_NO_FLAGS);
|
||||||
|
}
|
||||||
|
OS_Result result = OS_DeleteDir(path, OS_NO_FLAGS);
|
||||||
|
MA_ReleaseScratch(scratch);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
wchar_t wpath[1024];
|
||||||
|
UTF_CreateWidecharFromChar(wpath, OS_LENGTHOF(wpath), path.str, path.len);
|
||||||
|
BOOL success = RemoveDirectoryW(wpath);
|
||||||
|
OS_Result result = OS_SUCCESS;
|
||||||
|
if (success == 0)
|
||||||
|
result = OS_PATH_NOT_FOUND;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static OS_Result OS__WriteFile(S8_String path, S8_String data, bool append) {
|
||||||
|
wchar_t wpath[1024];
|
||||||
|
UTF_CreateWidecharFromChar(wpath, OS_LENGTHOF(wpath), path.str, path.len);
|
||||||
|
OS_Result result = OS_FAILURE;
|
||||||
|
|
||||||
|
DWORD access = GENERIC_WRITE;
|
||||||
|
DWORD creation_disposition = CREATE_ALWAYS;
|
||||||
|
if (append) {
|
||||||
|
access = FILE_APPEND_DATA;
|
||||||
|
creation_disposition = OPEN_ALWAYS;
|
||||||
|
}
|
||||||
|
|
||||||
|
HANDLE handle = CreateFileW(wpath, access, 0, NULL, creation_disposition, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||||
|
if (handle != INVALID_HANDLE_VALUE) {
|
||||||
|
DWORD bytes_written = 0;
|
||||||
|
IO_Assert(data.len == (DWORD)data.len); // @Todo: can only read 32 byte size files?
|
||||||
|
BOOL error = WriteFile(handle, data.str, (DWORD)data.len, &bytes_written, NULL);
|
||||||
|
if (error == TRUE) {
|
||||||
|
if (bytes_written != data.len) {
|
||||||
|
result = OS_SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CloseHandle(handle);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
result = OS_PATH_NOT_FOUND;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API OS_Result OS_AppendFile(S8_String path, S8_String string) {
|
||||||
|
return OS__WriteFile(path, string, true);
|
||||||
|
} // @untested
|
||||||
|
|
||||||
|
OS_API OS_Result OS_WriteFile(S8_String path, S8_String string) {
|
||||||
|
return OS__WriteFile(path, string, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API S8_String OS_ReadFile(MA_Arena *arena, S8_String path) {
|
||||||
|
bool success = false;
|
||||||
|
S8_String result = S8_MakeEmpty();
|
||||||
|
MA_Checkpoint checkpoint = MA_Save(arena);
|
||||||
|
|
||||||
|
wchar_t wpath[1024];
|
||||||
|
UTF_CreateWidecharFromChar(wpath, OS_LENGTHOF(wpath), path.str, path.len);
|
||||||
|
HANDLE handle = CreateFileW(wpath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||||
|
if (handle != INVALID_HANDLE_VALUE) {
|
||||||
|
LARGE_INTEGER file_size;
|
||||||
|
if (GetFileSizeEx(handle, &file_size)) {
|
||||||
|
if (file_size.QuadPart != 0) {
|
||||||
|
result.len = (int64_t)file_size.QuadPart;
|
||||||
|
result.str = (char *)MA_PushSizeNonZeroed(arena, result.len + 1);
|
||||||
|
DWORD read;
|
||||||
|
if (ReadFile(handle, result.str, (DWORD)result.len, &read, NULL)) { // @todo: can only read 32 byte size files?
|
||||||
|
if (read == result.len) {
|
||||||
|
success = true;
|
||||||
|
result.str[result.len] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CloseHandle(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!success) {
|
||||||
|
result = S8_MakeEmpty();
|
||||||
|
MA_Load(checkpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API int OS_SystemF(const char *string, ...) {
|
||||||
|
MA_Checkpoint scratch = MA_GetScratch();
|
||||||
|
S8_FORMAT(scratch.arena, string, result);
|
||||||
|
IO_Printf("Executing: %s\n", result.str);
|
||||||
|
fflush(stdout);
|
||||||
|
int error_code = system(result.str);
|
||||||
|
MA_ReleaseScratch(scratch);
|
||||||
|
return error_code;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API int64_t OS_GetFileModTime(S8_String file) {
|
||||||
|
FILETIME time = {0};
|
||||||
|
WIN32_FIND_DATAW data;
|
||||||
|
|
||||||
|
wchar_t wpath[1024];
|
||||||
|
UTF_CreateWidecharFromChar(wpath, 1024, file.str, file.len);
|
||||||
|
HANDLE handle = FindFirstFileW(wpath, &data);
|
||||||
|
if (handle != INVALID_HANDLE_VALUE) {
|
||||||
|
FindClose(handle);
|
||||||
|
time = data.ftLastWriteTime;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int64_t result = (int64_t)time.dwHighDateTime << 32 | time.dwLowDateTime;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API OS_Date OS_GetDate(void) {
|
||||||
|
SYSTEMTIME local;
|
||||||
|
GetLocalTime(&local);
|
||||||
|
|
||||||
|
OS_Date result = {0};
|
||||||
|
result.year = local.wYear;
|
||||||
|
result.month = local.wMonth;
|
||||||
|
result.day = local.wDay;
|
||||||
|
result.hour = local.wHour;
|
||||||
|
result.second = local.wSecond;
|
||||||
|
result.milliseconds = local.wMilliseconds;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
OS_API S8_String UTF_CreateStringFromWidechar(MA_Arena *arena, wchar_t *wstr, int64_t wsize) {
|
||||||
|
int64_t buffer_size = (wsize + 1) * 2;
|
||||||
|
char *buffer = (char *)MA_PushSizeNonZeroed(arena, buffer_size);
|
||||||
|
int64_t size = UTF_CreateCharFromWidechar(buffer, buffer_size, wstr, wsize);
|
||||||
|
IO_Assert(size < buffer_size);
|
||||||
|
return S8_Make(buffer, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API S8_List S8_SplitOnRegex(MA_Arena *arena, S8_String string, S8_String regex, unsigned flags) {
|
||||||
|
S8_List result = S8_MakeEmptyList();
|
||||||
|
int64_t index = 0;
|
||||||
|
|
||||||
|
char buff[4096];
|
||||||
|
RE_Regex *re = RE2_Parse(buff, sizeof(buff), regex.str, regex.len);
|
||||||
|
for (RE_Match match = RE3_Find(re, string.str, string.len); match.pos != -1; match = RE3_Find(re, string.str, string.len)) {
|
||||||
|
S8_String before_match = S8_Make(string.str, match.pos);
|
||||||
|
S8_String the_match = S8_Make(string.str + match.pos, match.size);
|
||||||
|
if (before_match.len) S8_AddNode(arena, &result, before_match);
|
||||||
|
if (flags & S8_SPLIT_INCLUSIVE) {
|
||||||
|
if (the_match.len) S8_AddNode(arena, &result, the_match);
|
||||||
|
}
|
||||||
|
string = S8_Skip(string, match.pos + match.size);
|
||||||
|
}
|
||||||
|
S8_AddNode(arena, &result, string);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API S8_List OS_ListDirRegex(MA_Arena *arena, S8_String path, unsigned flags, char *regex) {
|
||||||
|
S8_List result = S8_MakeEmptyList();
|
||||||
|
|
||||||
|
char buff[4096];
|
||||||
|
RE_Regex *re = RE1_Parse(buff, sizeof(buff), regex);
|
||||||
|
S8_List files = OS_ListDir(arena, path, flags);
|
||||||
|
for (S8_Node *it = files.first; it; it = it->next) {
|
||||||
|
if (RE3_AreEqual(re, it->string.str, it->string.len)) {
|
||||||
|
S8_AddNode(arena, &result, it->string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API S8_String OS_ListDirRegexAsString(MA_Arena *arena, S8_String path, unsigned flags, char *regex) {
|
||||||
|
S8_List files = OS_ListDirRegex(arena, path, flags, regex);
|
||||||
|
S8_String files_str = S8_MergeWithSeparator(arena, files, S8_Lit(" "));
|
||||||
|
return files_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API bool OS_ExpandIncludesList(MA_Arena *arena, S8_List *out, S8_String filepath) {
|
||||||
|
S8_String c = OS_ReadFile(arena, filepath);
|
||||||
|
if (c.str == 0) return false;
|
||||||
|
S8_String path = S8_ChopLastSlash(filepath);
|
||||||
|
S8_String include = S8_Lit("#include \"");
|
||||||
|
for (;;) {
|
||||||
|
int64_t idx = -1;
|
||||||
|
if (S8_Find(c, include, 0, &idx)) {
|
||||||
|
S8_String str_to_add = S8_GetPrefix(c, idx);
|
||||||
|
S8_AddNode(arena, out, str_to_add);
|
||||||
|
S8_String save = c;
|
||||||
|
c = S8_Skip(c, idx + include.len);
|
||||||
|
|
||||||
|
S8_String filename = c;
|
||||||
|
filename.len = 0;
|
||||||
|
while (filename.str[filename.len] != '"' && filename.len < c.len) {
|
||||||
|
filename.len += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
c = S8_Skip(c, filename.len + 1);
|
||||||
|
S8_String inc_path = S8_Format(arena, "%.*s/%.*s", S8_Expand(path), S8_Expand(filename));
|
||||||
|
if (!OS_ExpandIncludesList(arena, out, inc_path)) {
|
||||||
|
S8_String s = S8_GetPrefix(save, save.len - c.len);
|
||||||
|
S8_AddNode(arena, out, s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
S8_AddNode(arena, out, c);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_API S8_String OS_ExpandIncludes(MA_Arena *arena, S8_String filepath) {
|
||||||
|
S8_List out = {0};
|
||||||
|
S8_String result = {0};
|
||||||
|
MA_ScratchScope(s) {
|
||||||
|
OS_ExpandIncludesList(s.arena, &out, filepath);
|
||||||
|
result = S8_Merge(arena, out);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
67
filesystem.h
Normal file
67
filesystem.h
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
#ifndef OS_FS_HEADER
|
||||||
|
#define OS_FS_HEADER
|
||||||
|
|
||||||
|
#ifndef OS_API
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#define OS_API extern "C"
|
||||||
|
#else
|
||||||
|
#define OS_API
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef enum OS_Result OS_Result;
|
||||||
|
typedef struct OS_Date OS_Date;
|
||||||
|
|
||||||
|
enum OS_Result {
|
||||||
|
OS_SUCCESS,
|
||||||
|
OS_ALREADY_EXISTS,
|
||||||
|
OS_PATH_NOT_FOUND,
|
||||||
|
OS_FAILURE,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
OS_NO_FLAGS = 0,
|
||||||
|
OS_RECURSIVE = 1,
|
||||||
|
OS_RELATIVE_PATHS = 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct OS_Date {
|
||||||
|
uint32_t year;
|
||||||
|
uint32_t month;
|
||||||
|
uint32_t day;
|
||||||
|
uint32_t hour;
|
||||||
|
uint32_t minute;
|
||||||
|
uint32_t second;
|
||||||
|
uint32_t milliseconds;
|
||||||
|
};
|
||||||
|
|
||||||
|
OS_API void DEV_SetWorkingDir(void);
|
||||||
|
OS_API bool OS_IsAbsolute(S8_String path);
|
||||||
|
OS_API S8_String OS_GetExePath(MA_Arena *arena);
|
||||||
|
OS_API S8_String OS_GetExeDir(MA_Arena *arena);
|
||||||
|
OS_API S8_String OS_GetWorkingDir(MA_Arena *arena);
|
||||||
|
OS_API void OS_SetWorkingDir(S8_String path);
|
||||||
|
OS_API S8_String OS_GetAbsolutePath(MA_Arena *arena, S8_String relative);
|
||||||
|
OS_API bool OS_FileExists(S8_String path);
|
||||||
|
OS_API bool OS_IsDir(S8_String path);
|
||||||
|
OS_API bool OS_IsFile(S8_String path);
|
||||||
|
OS_API double OS_GetTime(void);
|
||||||
|
OS_API S8_List OS_ListDir(MA_Arena *arena, S8_String path, unsigned flags);
|
||||||
|
OS_API OS_Result OS_MakeDir(S8_String path);
|
||||||
|
OS_API OS_Result OS_CopyFile(S8_String from, S8_String to, bool overwrite);
|
||||||
|
OS_API OS_Result OS_DeleteFile(S8_String path);
|
||||||
|
OS_API OS_Result OS_DeleteDir(S8_String path, unsigned flags);
|
||||||
|
OS_API OS_Result OS_AppendFile(S8_String path, S8_String string);
|
||||||
|
OS_API OS_Result OS_WriteFile(S8_String path, S8_String string);
|
||||||
|
OS_API S8_String OS_ReadFile(MA_Arena *arena, S8_String path);
|
||||||
|
OS_API int OS_SystemF(const char *string, ...);
|
||||||
|
OS_API int64_t OS_GetFileModTime(S8_String file);
|
||||||
|
OS_API OS_Date OS_GetDate(void);
|
||||||
|
OS_API S8_String UTF_CreateStringFromWidechar(MA_Arena *arena, wchar_t *wstr, int64_t wsize);
|
||||||
|
OS_API S8_List S8_SplitOnRegex(MA_Arena *arena, S8_String string, S8_String regex, unsigned flags);
|
||||||
|
OS_API S8_List OS_ListDirRegex(MA_Arena *arena, S8_String path, unsigned flags, char *regex);
|
||||||
|
OS_API S8_String OS_ListDirRegexAsString(MA_Arena *arena, S8_String path, unsigned flags, char *regex);
|
||||||
|
OS_API bool OS_ExpandIncludesList(MA_Arena *arena, S8_List *out, S8_String filepath);
|
||||||
|
OS_API S8_String OS_ExpandIncludes(MA_Arena *arena, S8_String filepath);
|
||||||
|
OS_API bool WIN_EnableTerminalColors(void);
|
||||||
|
#endif // OS_FS_HEADER
|
||||||
53
hash.c
Normal file
53
hash.c
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
#include "hash.h"
|
||||||
|
|
||||||
|
// FNV HASH (1a?)
|
||||||
|
HASH_API_FUNCTION uint64_t HashBytes(void *data, uint64_t size) {
|
||||||
|
uint8_t *data8 = (uint8_t *)data;
|
||||||
|
uint64_t hash = (uint64_t)14695981039346656037ULL;
|
||||||
|
for (uint64_t i = 0; i < size; i++) {
|
||||||
|
hash = hash ^ (uint64_t)(data8[i]);
|
||||||
|
hash = hash * (uint64_t)1099511628211ULL;
|
||||||
|
}
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
HASH_API_FUNCTION RandomSeed MakeRandomSeed(uint64_t value) {
|
||||||
|
RandomSeed result;
|
||||||
|
result.a = value;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
HASH_API_FUNCTION uint64_t GetRandomU64(RandomSeed *state) {
|
||||||
|
uint64_t x = state->a;
|
||||||
|
x ^= x << 13;
|
||||||
|
x ^= x >> 7;
|
||||||
|
x ^= x << 17;
|
||||||
|
return state->a = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
HASH_API_FUNCTION int GetRandomRangeI(RandomSeed *seed, int first, int last_included) {
|
||||||
|
uint64_t random = GetRandomU64(seed);
|
||||||
|
int range = (last_included - first + 1);
|
||||||
|
int mapped = random % range;
|
||||||
|
int result = mapped + first;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
HASH_API_FUNCTION double GetRandomNormal(RandomSeed *series) {
|
||||||
|
uint64_t rnd = GetRandomU64(series);
|
||||||
|
double result = (double)rnd / (double)UINT64_MAX;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
HASH_API_FUNCTION double GetRandomNormalRange(RandomSeed *seed, double min, double max) {
|
||||||
|
double value = GetRandomNormal(seed);
|
||||||
|
double result = value * (max - min) + min;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
HASH_API_FUNCTION uint64_t HashMix(uint64_t x, uint64_t y) {
|
||||||
|
x ^= y;
|
||||||
|
x *= 0xff51afd7ed558ccd;
|
||||||
|
x ^= x >> 32;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
33
hash.h
Normal file
33
hash.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef HASH_HEADER
|
||||||
|
#define HASH_HEADER
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifndef HASH_API_FUNCTION
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#define HASH_API_FUNCTION extern "C"
|
||||||
|
#else
|
||||||
|
#define HASH_API_FUNCTION
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//@begin gen_structs
|
||||||
|
typedef struct RandomSeed RandomSeed;
|
||||||
|
//@end gen_structs
|
||||||
|
|
||||||
|
struct RandomSeed {
|
||||||
|
uint64_t a;
|
||||||
|
};
|
||||||
|
|
||||||
|
//@begin gen_api_funcs
|
||||||
|
HASH_API_FUNCTION uint64_t HashBytes(void *data, uint64_t size);
|
||||||
|
HASH_API_FUNCTION RandomSeed MakeRandomSeed(uint64_t value);
|
||||||
|
HASH_API_FUNCTION uint64_t GetRandomU64(RandomSeed *state);
|
||||||
|
HASH_API_FUNCTION int GetRandomRangeI(RandomSeed *seed, int first, int last_included);
|
||||||
|
HASH_API_FUNCTION double GetRandomNormal(RandomSeed *series);
|
||||||
|
HASH_API_FUNCTION double GetRandomNormalRange(RandomSeed *seed, double min, double max);
|
||||||
|
HASH_API_FUNCTION uint64_t HashMix(uint64_t x, uint64_t y);
|
||||||
|
//@end gen_api_funcs
|
||||||
|
|
||||||
|
#define WRAP_AROUND_POWER_OF_2(x, pow2) (((x) & ((pow2)-1llu)))
|
||||||
|
static inline float GetRandomNormalF(RandomSeed *series) { return (float)GetRandomNormal(series); }
|
||||||
|
#endif
|
||||||
298
io.h
Normal file
298
io.h
Normal file
@@ -0,0 +1,298 @@
|
|||||||
|
#ifndef IO_HEADER
|
||||||
|
#define IO_HEADER
|
||||||
|
#include <stdbool.h>
|
||||||
|
typedef enum IO_ErrorResult IO_ErrorResult;
|
||||||
|
#ifndef IO_API
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#define IO_API extern "C"
|
||||||
|
#else
|
||||||
|
#define IO_API
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum IO_ErrorResult {
|
||||||
|
IO_ErrorResult_Continue,
|
||||||
|
IO_ErrorResult_Break,
|
||||||
|
IO_ErrorResult_Exit,
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define IO_DebugBreak() (__debugbreak(), 0)
|
||||||
|
#else
|
||||||
|
#define IO_DebugBreak() (IO_Exit(1), 0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extern void (*IO_User_OutputMessage)(char *str, int len);
|
||||||
|
|
||||||
|
#define IO__STRINGIFY(x) #x
|
||||||
|
#define IO__TOSTRING(x) IO__STRINGIFY(x)
|
||||||
|
#define IO_LINE IO__TOSTRING(__LINE__)
|
||||||
|
|
||||||
|
#define IO_Assert(x) !(x) && IO__FatalError((char *)(__FILE__ "(" IO_LINE "): " \
|
||||||
|
"error: " #x "\n")) && \
|
||||||
|
IO_DebugBreak()
|
||||||
|
#define IO_FatalErrorf(...) \
|
||||||
|
do { \
|
||||||
|
bool result = IO__FatalErrorf(__FILE__, __LINE__, __VA_ARGS__); \
|
||||||
|
if (result) IO_DebugBreak(); \
|
||||||
|
} while (0)
|
||||||
|
#define IO_FatalError(...) \
|
||||||
|
do { \
|
||||||
|
bool result = IO__FatalError(__FILE__ "(" IO_LINE "): error - " __VA_ARGS__); \
|
||||||
|
if (result) IO_DebugBreak(); \
|
||||||
|
} while (0)
|
||||||
|
#define IO_Assertf(x, ...) \
|
||||||
|
do { \
|
||||||
|
if (!(x)) { \
|
||||||
|
bool result = IO__FatalErrorf(__FILE__, __LINE__, __VA_ARGS__); \
|
||||||
|
if (result) IO_DebugBreak(); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define IO_InvalidElseIf(c) \
|
||||||
|
else if (c) { \
|
||||||
|
IO_InvalidCodepath(); \
|
||||||
|
}
|
||||||
|
#define IO_InvalidElse() \
|
||||||
|
else { \
|
||||||
|
IO_InvalidCodepath(); \
|
||||||
|
}
|
||||||
|
#define IO_InvalidCodepath() IO_FatalError("This codepath is invalid")
|
||||||
|
#define IO_InvalidDefaultCase() \
|
||||||
|
default: { \
|
||||||
|
IO_FatalError("Entered invalid switch statement case"); \
|
||||||
|
}
|
||||||
|
#define IO_Todo() IO_FatalError("This codepath is not implemented yet")
|
||||||
|
|
||||||
|
IO_API bool IO__FatalErrorf(const char *file, int line, const char *msg, ...);
|
||||||
|
IO_API void IO_Printf(const char *msg, ...);
|
||||||
|
IO_API bool IO__FatalError(char *msg);
|
||||||
|
IO_API void IO_Print(char *msg);
|
||||||
|
IO_API void IO_OutputMessage(char *str, int len);
|
||||||
|
IO_API IO_ErrorResult IO_OutputError(char *str, int len);
|
||||||
|
IO_API void IO_Exit(int error_code);
|
||||||
|
IO_API bool IO_IsDebuggerPresent(void);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef IO_IMPLEMENTATION
|
||||||
|
#ifndef IO_SNPRINTF
|
||||||
|
#include <stdio.h>
|
||||||
|
#define IO_SNPRINTF snprintf
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef IO_VSNPRINTF
|
||||||
|
#include <stdio.h>
|
||||||
|
#define IO_VSNPRINTF vsnprintf
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef IO_ALLOCATE
|
||||||
|
#include <stdlib.h>
|
||||||
|
#define IO_ALLOCATE(x) malloc(x)
|
||||||
|
#define IO_FREE(x) free(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef IO_FN
|
||||||
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
|
#define IO_FN __attribute__((unused)) static
|
||||||
|
#else
|
||||||
|
#define IO_FN static
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
IO_FN int IO_Strlen(char *string) {
|
||||||
|
int len = 0;
|
||||||
|
while (*string++ != 0) len++;
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
void (*IO_User_OutputMessage)(char *str, int len);
|
||||||
|
|
||||||
|
IO_API bool IO__FatalErrorf(const char *file, int line, const char *msg, ...) {
|
||||||
|
va_list args1;
|
||||||
|
va_list args2;
|
||||||
|
char buff[2048];
|
||||||
|
|
||||||
|
va_start(args1, msg);
|
||||||
|
va_copy(args2, args1);
|
||||||
|
int size = IO_VSNPRINTF(buff, sizeof(buff), msg, args2);
|
||||||
|
va_end(args2);
|
||||||
|
|
||||||
|
char *new_buffer = 0;
|
||||||
|
char *user_message = buff;
|
||||||
|
if (size >= sizeof(buff)) {
|
||||||
|
size += 4;
|
||||||
|
new_buffer = (char *)IO_ALLOCATE(size);
|
||||||
|
IO_VSNPRINTF(new_buffer, size, msg, args1);
|
||||||
|
user_message = new_buffer;
|
||||||
|
}
|
||||||
|
va_end(args1);
|
||||||
|
|
||||||
|
IO_ErrorResult ret = IO_ErrorResult_Continue;
|
||||||
|
{
|
||||||
|
char buff2[2048];
|
||||||
|
char *result = buff2;
|
||||||
|
char *b = 0;
|
||||||
|
int size = IO_SNPRINTF(buff2, sizeof(buff2), "%s(%d): error: %s \n", file, line, user_message);
|
||||||
|
if (size >= sizeof(buff2)) {
|
||||||
|
size += 4;
|
||||||
|
b = (char *)IO_ALLOCATE(size);
|
||||||
|
size = IO_SNPRINTF(b, size, "%s(%d): error: %s \n", file, line, user_message);
|
||||||
|
result = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = IO_OutputError(result, size);
|
||||||
|
if (ret == IO_ErrorResult_Exit) {
|
||||||
|
IO_Exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (b) {
|
||||||
|
IO_FREE(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (new_buffer) {
|
||||||
|
IO_FREE(new_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret == IO_ErrorResult_Break;
|
||||||
|
}
|
||||||
|
|
||||||
|
IO_API void IO_Printf(const char *msg, ...) {
|
||||||
|
va_list args1;
|
||||||
|
va_list args2;
|
||||||
|
char buff[2048];
|
||||||
|
|
||||||
|
va_start(args1, msg);
|
||||||
|
va_copy(args2, args1);
|
||||||
|
int size = IO_VSNPRINTF(buff, sizeof(buff), msg, args2);
|
||||||
|
va_end(args2);
|
||||||
|
|
||||||
|
char *new_buffer = 0;
|
||||||
|
char *result = buff;
|
||||||
|
if (size >= sizeof(buff)) {
|
||||||
|
size += 4;
|
||||||
|
new_buffer = (char *)IO_ALLOCATE(size);
|
||||||
|
IO_VSNPRINTF(new_buffer, size, msg, args1);
|
||||||
|
result = new_buffer;
|
||||||
|
}
|
||||||
|
va_end(args1);
|
||||||
|
|
||||||
|
if (IO_User_OutputMessage) {
|
||||||
|
IO_User_OutputMessage(result, size);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
IO_OutputMessage(result, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (new_buffer) {
|
||||||
|
IO_FREE(new_buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IO_API bool IO__FatalError(char *msg) {
|
||||||
|
int len = IO_Strlen(msg);
|
||||||
|
IO_ErrorResult result = IO_OutputError(msg, len);
|
||||||
|
if (result == IO_ErrorResult_Exit) {
|
||||||
|
IO_Exit(1);
|
||||||
|
}
|
||||||
|
return result == IO_ErrorResult_Break;
|
||||||
|
}
|
||||||
|
|
||||||
|
IO_API void IO_Print(char *msg) {
|
||||||
|
int len = IO_Strlen(msg);
|
||||||
|
if (IO_User_OutputMessage) {
|
||||||
|
IO_User_OutputMessage(msg, len);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
IO_OutputMessage(msg, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#ifdef _WIN32
|
||||||
|
#ifndef NOMINMAX
|
||||||
|
#define NOMINMAX
|
||||||
|
#endif
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#endif
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
#pragma comment(lib, "user32")
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
IO_API bool IO_IsDebuggerPresent(void) {
|
||||||
|
return IsDebuggerPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
IO_API void IO_OutputMessage(char *str, int len) {
|
||||||
|
if (IsDebuggerPresent()) {
|
||||||
|
OutputDebugStringA(str);
|
||||||
|
}
|
||||||
|
printf("%.*s", len, str);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
IO_API IO_ErrorResult IO_OutputError(char *str, int len) {
|
||||||
|
IO_ErrorResult result = IO_ErrorResult_Continue;
|
||||||
|
IO_OutputMessage(str, len);
|
||||||
|
|
||||||
|
char *msg = str;
|
||||||
|
if (str[len] != 0) {
|
||||||
|
msg = (char *)IO_ALLOCATE(len + 1);
|
||||||
|
for (int i = 0; i < len; i += 1) msg[i] = str[i];
|
||||||
|
msg[len] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
OutputDebugStringA(msg);
|
||||||
|
if (!IsDebuggerPresent()) {
|
||||||
|
|
||||||
|
// Limit size of error output message
|
||||||
|
char tmp = 0;
|
||||||
|
if (len > 4096) {
|
||||||
|
tmp = str[4096];
|
||||||
|
str[4096] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageBoxA(0, msg, "Error!", 0);
|
||||||
|
|
||||||
|
if (tmp != 0) {
|
||||||
|
str[4096] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = IO_ErrorResult_Exit;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result = IO_ErrorResult_Break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg != str) {
|
||||||
|
IO_FREE(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
IO_API void IO_Exit(int error_code) {
|
||||||
|
ExitProcess(error_code);
|
||||||
|
}
|
||||||
|
#else // _WIN32 else // LIBC
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
IO_API IO_ErrorResult IO_OutputError(char *str, int len) {
|
||||||
|
fprintf(stderr, "%.*s", len, str);
|
||||||
|
return IO_ErrorResult_Exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
IO_API void IO_OutputMessage(char *str, int len) {
|
||||||
|
fprintf(stdout, "%.*s", len, str);
|
||||||
|
}
|
||||||
|
|
||||||
|
IO_API void IO_Exit(int error_code) {
|
||||||
|
exit(error_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
IO_API bool IO_IsDebuggerPresent(void) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif // LIBC
|
||||||
|
#endif // IO_IMPLEMENTATION
|
||||||
129
linked_list.h
Normal file
129
linked_list.h
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
#ifndef LINKED_LIST_HEADER
|
||||||
|
#define LINKED_LIST_HEADER
|
||||||
|
|
||||||
|
#define SLL_QUEUE_ADD_MOD(f, l, n, next) \
|
||||||
|
do { \
|
||||||
|
(n)->next = 0; \
|
||||||
|
if ((f) == 0) { \
|
||||||
|
(f) = (l) = (n); \
|
||||||
|
} \
|
||||||
|
else { \
|
||||||
|
(l) = (l)->next = (n); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
#define SLL_QUEUE_ADD(f, l, n) SLL_QUEUE_ADD_MOD(f, l, n, next)
|
||||||
|
|
||||||
|
#define SLL_QUEUE_POP_FIRST_MOD(f, l, next) \
|
||||||
|
do { \
|
||||||
|
if ((f) == (l)) { \
|
||||||
|
(f) = (l) = 0; \
|
||||||
|
} \
|
||||||
|
else { \
|
||||||
|
(f) = (f)->next; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
#define SLL_QUEUE_POP_FIRST(f, l) SLL_QUEUE_POP_FIRST_MOD(f, l, next)
|
||||||
|
|
||||||
|
#define SLL_STACK_ADD_MOD(stack_base, new_stack_base, next) \
|
||||||
|
do { \
|
||||||
|
(new_stack_base)->next = (stack_base); \
|
||||||
|
(stack_base) = (new_stack_base); \
|
||||||
|
} while (0)
|
||||||
|
#define SLL_STACK_ADD(stack_base, new_stack_base) \
|
||||||
|
SLL_STACK_ADD_MOD(stack_base, new_stack_base, next)
|
||||||
|
|
||||||
|
#define SLL_STACK_POP_AND_STORE(stack_base, out_node) \
|
||||||
|
do { \
|
||||||
|
if (stack_base) { \
|
||||||
|
(out_node) = (stack_base); \
|
||||||
|
(stack_base) = (stack_base)->next; \
|
||||||
|
(out_node)->next = 0; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define DLL_QUEUE_ADD_MOD(f, l, node, next, prev) \
|
||||||
|
do { \
|
||||||
|
if ((f) == 0) { \
|
||||||
|
(f) = (l) = (node); \
|
||||||
|
(node)->prev = 0; \
|
||||||
|
(node)->next = 0; \
|
||||||
|
} \
|
||||||
|
else { \
|
||||||
|
(l)->next = (node); \
|
||||||
|
(node)->prev = (l); \
|
||||||
|
(node)->next = 0; \
|
||||||
|
(l) = (node); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
#define DLL_QUEUE_ADD(f, l, node) DLL_QUEUE_ADD_MOD(f, l, node, next, prev)
|
||||||
|
#define DLL_QUEUE_REMOVE_MOD(first, last, node, next, prev) \
|
||||||
|
do { \
|
||||||
|
if ((first) == (last)) { \
|
||||||
|
IO_Assertf((node) == (first), "Not you are trying to remove is not in the list"); \
|
||||||
|
(first) = (last) = 0; \
|
||||||
|
} \
|
||||||
|
else if ((last) == (node)) { \
|
||||||
|
(last) = (last)->prev; \
|
||||||
|
(last)->next = 0; \
|
||||||
|
} \
|
||||||
|
else if ((first) == (node)) { \
|
||||||
|
(first) = (first)->next; \
|
||||||
|
(first)->prev = 0; \
|
||||||
|
} \
|
||||||
|
else { \
|
||||||
|
(node)->prev->next = (node)->next; \
|
||||||
|
(node)->next->prev = (node)->prev; \
|
||||||
|
} \
|
||||||
|
if (node) { \
|
||||||
|
(node)->prev = 0; \
|
||||||
|
(node)->next = 0; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
#define DLL_QUEUE_REMOVE(first, last, node) DLL_QUEUE_REMOVE_MOD(first, last, node, next, prev)
|
||||||
|
|
||||||
|
#define DLL_STACK_ADD_MOD(first, node, next, prev) \
|
||||||
|
do { \
|
||||||
|
(node)->next = (first); \
|
||||||
|
if ((first)) \
|
||||||
|
(first)->prev = (node); \
|
||||||
|
(first) = (node); \
|
||||||
|
(node)->prev = 0; \
|
||||||
|
} while (0)
|
||||||
|
#define DLL_STACK_ADD(first, node) DLL_STACK_ADD_MOD(first, node, next, prev)
|
||||||
|
#define DLL_STACK_REMOVE_MOD(first, node, next, prev) \
|
||||||
|
do { \
|
||||||
|
if ((node) == (first)) { \
|
||||||
|
(first) = (first)->next; \
|
||||||
|
if ((first)) \
|
||||||
|
(first)->prev = 0; \
|
||||||
|
} \
|
||||||
|
else { \
|
||||||
|
(node)->prev->next = (node)->next; \
|
||||||
|
if ((node)->next) \
|
||||||
|
(node)->next->prev = (node)->prev; \
|
||||||
|
} \
|
||||||
|
if (node) { \
|
||||||
|
(node)->prev = 0; \
|
||||||
|
(node)->next = 0; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
#define DLL_STACK_REMOVE(first, node) DLL_STACK_REMOVE_MOD(first, node, next, prev)
|
||||||
|
|
||||||
|
#define DLL_INSERT_NEXT_MOD(base, new, next, prev) \
|
||||||
|
do { \
|
||||||
|
if ((base) == 0) { \
|
||||||
|
(base) = (new); \
|
||||||
|
(new)->next = 0; \
|
||||||
|
(new)->prev = 0; \
|
||||||
|
} \
|
||||||
|
else { \
|
||||||
|
(new)->next = (base)->next; \
|
||||||
|
(base)->next = (new); \
|
||||||
|
(new)->prev = (base); \
|
||||||
|
if ((new)->next) (new)->next->prev = (new); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
#define DLL_INSERT_NEXT(base, new) DLL_INSERT_NEXT_MOD(base, new, next, prev)
|
||||||
|
#define DLL_INSERT_PREV(base, new) DLL_INSERT_NEXT_MOD(base, new, next, prev)
|
||||||
|
|
||||||
|
#endif
|
||||||
26
load_library.c
Normal file
26
load_library.c
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#include "load_library.h"
|
||||||
|
#ifdef _WIN32
|
||||||
|
#ifndef NOMINMAX
|
||||||
|
#define NOMINMAX
|
||||||
|
#endif
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#endif
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
LIB_Library LIB_LoadLibrary(char *str) {
|
||||||
|
HMODULE module = LoadLibraryA(str);
|
||||||
|
return (LIB_Library)module;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *LIB_LoadSymbol(LIB_Library lib, char *symbol) {
|
||||||
|
void *result = GetProcAddress((HMODULE)lib, symbol);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LIB_UnloadLibrary(LIB_Library lib) {
|
||||||
|
BOOL result = FreeLibrary((HMODULE)lib);
|
||||||
|
if (result == 0) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif // _WIN32
|
||||||
14
load_library.h
Normal file
14
load_library.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef LIB_LOAD_HEADER
|
||||||
|
#define LIB_LOAD_HEADER
|
||||||
|
|
||||||
|
typedef void *LIB_Library;
|
||||||
|
|
||||||
|
LIB_Library LIB_LoadLibrary(char *str);
|
||||||
|
void *LIB_LoadSymbol(LIB_Library lib, char *symbol);
|
||||||
|
bool LIB_UnloadLibrary(LIB_Library lib);
|
||||||
|
|
||||||
|
#ifndef LIB_EXPORT
|
||||||
|
#define LIB_EXPORT __declspec(dllexport)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
2112
multimedia.h
Normal file
2112
multimedia.h
Normal file
File diff suppressed because it is too large
Load Diff
90
preproc_env.h
Normal file
90
preproc_env.h
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
#ifndef PREPROC_ENV_HEADER
|
||||||
|
#define PREPROC_ENV_HEADER
|
||||||
|
|
||||||
|
#ifndef _CRT_SECURE_NO_WARNINGS
|
||||||
|
#define _CRT_SECURE_NO_WARNINGS
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__APPLE__) && defined(__MACH__)
|
||||||
|
#define OS_MAC 1
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
#define OS_WINDOWS 1
|
||||||
|
#elif defined(__linux__)
|
||||||
|
#define OS_POSIX 1
|
||||||
|
#define OS_LINUX 1
|
||||||
|
#else
|
||||||
|
#error Unsupported platform
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
#define COMPILER_CLANG 1
|
||||||
|
#elif defined(__GNUC__) || defined(__GNUG__)
|
||||||
|
#define COMPILER_GCC 1
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
#define COMPILER_MSVC 1
|
||||||
|
#elif defined(__TINYC__)
|
||||||
|
#define COMPILER_TCC 1
|
||||||
|
#else
|
||||||
|
#error Unsupported compiler
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#define LANG_CPP 1
|
||||||
|
#else
|
||||||
|
#define LANG_C 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef OS_MAC
|
||||||
|
#define OS_MAC 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef OS_WINDOWS
|
||||||
|
#define OS_WINDOWS 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef OS_LINUX
|
||||||
|
#define OS_LINUX 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef OS_POSIX
|
||||||
|
#define OS_POSIX 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef COMPILER_MSVC
|
||||||
|
#define COMPILER_MSVC 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef COMPILER_CLANG
|
||||||
|
#define COMPILER_CLANG 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef COMPILER_GCC
|
||||||
|
#define COMPILER_GCC 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef COMPILER_TCC
|
||||||
|
#define COMPILER_TCC 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LANG_CPP
|
||||||
|
#define LANG_CPP 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LANG_C
|
||||||
|
#define LANG_C 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if COMPILER_MSVC
|
||||||
|
#define FORCE_INLINE __forceinline
|
||||||
|
#elif COMPILER_GCC || COMPILER_CLANG
|
||||||
|
#define FORCE_INLINE __attribute__((always_inline)) inline
|
||||||
|
#else
|
||||||
|
#define FORCE_INLINE inline
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// #if COMPILER_CLANG
|
||||||
|
// #pragma clang diagnostic push
|
||||||
|
// #pragma clang diagnostic ignored "-Wmicrosoft-enum-forward-reference"
|
||||||
|
// #endif
|
||||||
|
|
||||||
|
#endif // PREPROC_ENV_HEADER
|
||||||
656
regex.h
Normal file
656
regex.h
Normal file
@@ -0,0 +1,656 @@
|
|||||||
|
#ifndef RE_HEADER
|
||||||
|
#define RE_HEADER
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#ifndef RE_Int
|
||||||
|
#define RE_Int int64_t
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef RE_API
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#define RE_API extern "C"
|
||||||
|
#else
|
||||||
|
#define RE_API
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef RE_StaticFunc
|
||||||
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
|
#define RE_StaticFunc __attribute__((unused)) static
|
||||||
|
#else
|
||||||
|
#define RE_StaticFunc static
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct RE_String RE_String;
|
||||||
|
typedef struct RE_Utf32Result RE_Utf32Result;
|
||||||
|
typedef struct RE_Parser RE_Parser;
|
||||||
|
typedef enum RE_MatchKind RE_MatchKind;
|
||||||
|
typedef struct RE_Regex RE_Regex;
|
||||||
|
typedef struct RE_Match RE_Match;
|
||||||
|
|
||||||
|
/* @todo
|
||||||
|
Add \W \D \S oppsites
|
||||||
|
*/
|
||||||
|
|
||||||
|
enum RE_MatchKind {
|
||||||
|
RE_MATCH_NULL,
|
||||||
|
RE_MATCH_FRONT,
|
||||||
|
RE_MATCH_BACK,
|
||||||
|
RE_MATCH_WORD,
|
||||||
|
RE_MATCH_OR,
|
||||||
|
RE_MATCH_GROUP,
|
||||||
|
RE_MATCH_SELECTED,
|
||||||
|
RE_MATCH_NOT_SELECTED,
|
||||||
|
RE_MATCH_RANGE,
|
||||||
|
RE_MATCH_ANY,
|
||||||
|
RE_MATCH_ANY_WORD,
|
||||||
|
RE_MATCH_ANY_DIGIT,
|
||||||
|
RE_MATCH_ANY_WHITESPACE,
|
||||||
|
RE_MATCH_ONE_OR_MORE,
|
||||||
|
RE_MATCH_ZERO_OR_MORE,
|
||||||
|
RE_MATCH_ZERO_OR_ONE,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RE_Regex {
|
||||||
|
RE_MatchKind kind;
|
||||||
|
RE_Regex *next;
|
||||||
|
RE_Regex *prev;
|
||||||
|
|
||||||
|
union {
|
||||||
|
struct {
|
||||||
|
char word_min;
|
||||||
|
char word_max;
|
||||||
|
};
|
||||||
|
char word;
|
||||||
|
uint32_t word32;
|
||||||
|
RE_Regex *child;
|
||||||
|
struct {
|
||||||
|
RE_Regex *left;
|
||||||
|
RE_Regex *right;
|
||||||
|
};
|
||||||
|
struct {
|
||||||
|
RE_Regex *first;
|
||||||
|
RE_Regex *last;
|
||||||
|
} group;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RE_Match {
|
||||||
|
RE_Int pos;
|
||||||
|
RE_Int size;
|
||||||
|
};
|
||||||
|
|
||||||
|
RE_API bool RE1_AreEqual(char *regex, char *string);
|
||||||
|
RE_API bool RE2_AreEqual(RE_Regex *regex, char *string);
|
||||||
|
RE_API bool RE3_AreEqual(RE_Regex *regex, char *string, RE_Int len);
|
||||||
|
RE_API RE_Match RE1_Find(char *regex, char *string);
|
||||||
|
RE_API RE_Match RE2_Find(RE_Regex *regex, char *string);
|
||||||
|
RE_API RE_Match RE3_Find(RE_Regex *regex, char *string, RE_Int len);
|
||||||
|
RE_API RE_Match RE2_FindAgain(RE_Regex *regex, char *string, RE_Match prev_match);
|
||||||
|
RE_API RE_Match RE3_FindAgain(RE_Regex *regex, char *string, RE_Int len, RE_Match prev_match);
|
||||||
|
RE_API RE_Int RE3_MatchFront(RE_Regex *regex, char *string, RE_Int len, char *string_front);
|
||||||
|
RE_API RE_Regex *RE1_Parse(char *buff, RE_Int buffsize, char *string);
|
||||||
|
RE_API RE_Regex *RE2_Parse(char *buff, RE_Int buffsize, char *string, RE_Int len);
|
||||||
|
|
||||||
|
#endif // RE_HEADER
|
||||||
|
#ifdef RE_IMPLEMENTATION
|
||||||
|
#ifndef RE_ASSERT
|
||||||
|
#include <assert.h>
|
||||||
|
#define RE_ASSERT(x) assert(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef RE_STRICT_ASSERT
|
||||||
|
#define RE_STRICT_ASSERT RE_ASSERT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef RE_MemoryZero
|
||||||
|
#include <string.h>
|
||||||
|
#define RE_MemoryZero(p, size) memset(p, 0, size)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct RE__Arena {
|
||||||
|
char *buff;
|
||||||
|
RE_Int len;
|
||||||
|
RE_Int cap;
|
||||||
|
} RE_Arena;
|
||||||
|
|
||||||
|
struct RE_String {
|
||||||
|
char *str;
|
||||||
|
RE_Int len;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RE_Utf32Result {
|
||||||
|
uint32_t out_str;
|
||||||
|
int advance;
|
||||||
|
int error;
|
||||||
|
};
|
||||||
|
static RE_Regex RE_NullRegex;
|
||||||
|
static char RE_NullChar;
|
||||||
|
|
||||||
|
struct RE_Parser {
|
||||||
|
RE_String string;
|
||||||
|
RE_Int i;
|
||||||
|
RE_Regex *first;
|
||||||
|
RE_Regex *last;
|
||||||
|
};
|
||||||
|
RE_API RE_Regex *RE1_ParseEx(RE_Arena *arena, char *string);
|
||||||
|
RE_API RE_Regex *RE2_ParseEx(RE_Arena *arena, char *string, RE_Int len);
|
||||||
|
|
||||||
|
RE_StaticFunc void *RE_PushSize(RE_Arena *arena, RE_Int size) {
|
||||||
|
if (arena->len + size > arena->cap) {
|
||||||
|
RE_ASSERT(!"RE_Regex: Not enough memory passed for this regex");
|
||||||
|
}
|
||||||
|
void *result = arena->buff + arena->len;
|
||||||
|
arena->len += size;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_StaticFunc RE_Arena RE_ArenaFromBuffer(char *buff, RE_Int size) {
|
||||||
|
RE_Arena result;
|
||||||
|
result.len = 0;
|
||||||
|
result.cap = size;
|
||||||
|
result.buff = buff;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_StaticFunc RE_String RE_Skip(RE_String string, RE_Int len) {
|
||||||
|
if (len > string.len) len = string.len;
|
||||||
|
RE_Int remain = string.len - len;
|
||||||
|
RE_String result;
|
||||||
|
result.str = string.str + len;
|
||||||
|
result.len = remain;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_StaticFunc RE_Int RE_StringLength(char *string) {
|
||||||
|
RE_Int len = 0;
|
||||||
|
while (*string++ != 0) len++;
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_StaticFunc RE_Utf32Result RE_ConvertUTF8ToUTF32(char *c, int max_advance) {
|
||||||
|
RE_Utf32Result result;
|
||||||
|
RE_MemoryZero(&result, sizeof(result));
|
||||||
|
|
||||||
|
if ((c[0] & 0x80) == 0) { // Check if leftmost zero of first byte is unset
|
||||||
|
if (max_advance >= 1) {
|
||||||
|
result.out_str = c[0];
|
||||||
|
result.advance = 1;
|
||||||
|
}
|
||||||
|
else result.error = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if ((c[0] & 0xe0) == 0xc0) {
|
||||||
|
if ((c[1] & 0xc0) == 0x80) { // Continuation byte required
|
||||||
|
if (max_advance >= 2) {
|
||||||
|
result.out_str = (uint32_t)(c[0] & 0x1f) << 6u | (c[1] & 0x3f);
|
||||||
|
result.advance = 2;
|
||||||
|
}
|
||||||
|
else result.error = 2;
|
||||||
|
}
|
||||||
|
else result.error = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if ((c[0] & 0xf0) == 0xe0) {
|
||||||
|
if ((c[1] & 0xc0) == 0x80 && (c[2] & 0xc0) == 0x80) { // Two continuation bytes required
|
||||||
|
if (max_advance >= 3) {
|
||||||
|
result.out_str = (uint32_t)(c[0] & 0xf) << 12u | (uint32_t)(c[1] & 0x3f) << 6u | (c[2] & 0x3f);
|
||||||
|
result.advance = 3;
|
||||||
|
}
|
||||||
|
else result.error = 3;
|
||||||
|
}
|
||||||
|
else result.error = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if ((c[0] & 0xf8) == 0xf0) {
|
||||||
|
if ((c[1] & 0xc0) == 0x80 && (c[2] & 0xc0) == 0x80 && (c[3] & 0xc0) == 0x80) { // Three continuation bytes required
|
||||||
|
if (max_advance >= 4) {
|
||||||
|
result.out_str = (uint32_t)(c[0] & 0xf) << 18u | (uint32_t)(c[1] & 0x3f) << 12u | (uint32_t)(c[2] & 0x3f) << 6u | (uint32_t)(c[3] & 0x3f);
|
||||||
|
result.advance = 4;
|
||||||
|
}
|
||||||
|
else result.error = 4;
|
||||||
|
}
|
||||||
|
else result.error = 4;
|
||||||
|
}
|
||||||
|
else result.error = 4;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define RE_DLL_QUEUE_REMOVE(first, last, node) \
|
||||||
|
do { \
|
||||||
|
if ((first) == (last)) { \
|
||||||
|
(first) = (last) = 0; \
|
||||||
|
} \
|
||||||
|
else if ((last) == (node)) { \
|
||||||
|
(last) = (last)->prev; \
|
||||||
|
(last)->next = 0; \
|
||||||
|
} \
|
||||||
|
else if ((first) == (node)) { \
|
||||||
|
(first) = (first)->next; \
|
||||||
|
(first)->prev = 0; \
|
||||||
|
} \
|
||||||
|
else { \
|
||||||
|
(node)->prev->next = (node)->next; \
|
||||||
|
(node)->next->prev = (node)->prev; \
|
||||||
|
} \
|
||||||
|
if (node) (node)->prev = 0; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define RE_DLL_QUEUE_ADD(f, l, node) \
|
||||||
|
do { \
|
||||||
|
if ((f) == 0) { \
|
||||||
|
(f) = (l) = (node); \
|
||||||
|
(node)->prev = 0; \
|
||||||
|
(node)->next = 0; \
|
||||||
|
} \
|
||||||
|
else { \
|
||||||
|
(l)->next = (node); \
|
||||||
|
(node)->prev = (l); \
|
||||||
|
(node)->next = 0; \
|
||||||
|
(l) = (node); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
RE_StaticFunc char *RE_GetP(RE_Parser *P) {
|
||||||
|
if (P->i >= P->string.len) return &RE_NullChar;
|
||||||
|
return P->string.str + P->i;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_StaticFunc char RE_Get(RE_Parser *P) {
|
||||||
|
if (P->i >= P->string.len) return 0;
|
||||||
|
return P->string.str[P->i];
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_StaticFunc char RE_Get1(RE_Parser *P) {
|
||||||
|
if ((P->i + 1) >= P->string.len || P->i >= P->string.len) return 0;
|
||||||
|
return P->string.str[P->i + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_StaticFunc void RE_Advance(RE_Parser *P) {
|
||||||
|
if (P->i >= P->string.len) return;
|
||||||
|
P->i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_StaticFunc RE_Regex *RE_ParseSingle(RE_Parser *P, RE_Arena *arena, RE_Regex **first, RE_Regex **last) {
|
||||||
|
RE_Regex *regex = (RE_Regex *)RE_PushSize(arena, sizeof(RE_Regex));
|
||||||
|
RE_MemoryZero(regex, sizeof(*regex));
|
||||||
|
char *c = RE_GetP(P);
|
||||||
|
RE_Int size_left = P->string.len - P->i;
|
||||||
|
RE_Advance(P);
|
||||||
|
switch (*c) {
|
||||||
|
case ')': RE_STRICT_ASSERT(regex->kind != RE_MATCH_NULL && "Invalid regex syntax, ')' appeared without matching '('"); break;
|
||||||
|
case '\0': RE_STRICT_ASSERT(regex->kind != RE_MATCH_NULL && "Invalid regex syntax, reached end of string obruptly"); break;
|
||||||
|
case '.': regex->kind = RE_MATCH_ANY; break;
|
||||||
|
case '^': regex->kind = RE_MATCH_FRONT; break;
|
||||||
|
case '$': regex->kind = RE_MATCH_BACK; break;
|
||||||
|
|
||||||
|
case '*': {
|
||||||
|
if (*last) {
|
||||||
|
regex->kind = RE_MATCH_ZERO_OR_MORE;
|
||||||
|
RE_Regex *prev = *last;
|
||||||
|
RE_DLL_QUEUE_REMOVE(*first, *last, *last);
|
||||||
|
regex->child = prev;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
RE_STRICT_ASSERT(!"Invalid regex syntax, '*' is not attached to anything");
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case '+': {
|
||||||
|
if (*last) {
|
||||||
|
regex->kind = RE_MATCH_ONE_OR_MORE;
|
||||||
|
RE_Regex *prev = *last;
|
||||||
|
RE_DLL_QUEUE_REMOVE(*first, *last, *last);
|
||||||
|
regex->child = prev;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
RE_STRICT_ASSERT(!"Invalid regex syntax, '+' is not attached to anything");
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case '?': {
|
||||||
|
if (*last) {
|
||||||
|
regex->kind = RE_MATCH_ZERO_OR_ONE;
|
||||||
|
RE_Regex *prev = *last;
|
||||||
|
RE_DLL_QUEUE_REMOVE(*first, *last, *last);
|
||||||
|
regex->child = prev;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
RE_STRICT_ASSERT(!"Invalid regex syntax, '?' is not attached to anything");
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case '[': {
|
||||||
|
regex->kind = RE_MATCH_SELECTED;
|
||||||
|
if (RE_Get(P) == '^') {
|
||||||
|
regex->kind = RE_MATCH_NOT_SELECTED;
|
||||||
|
RE_Advance(P);
|
||||||
|
}
|
||||||
|
while (RE_Get(P) != 0 && RE_Get(P) != ']') {
|
||||||
|
RE_Regex *r = RE_ParseSingle(P, arena, ®ex->group.first, ®ex->group.last);
|
||||||
|
if (r->kind == RE_MATCH_NULL) {
|
||||||
|
regex->kind = RE_MATCH_NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (r->kind == RE_MATCH_WORD && RE_Get(P) == '-') {
|
||||||
|
char word = RE_Get1(P);
|
||||||
|
if (word >= '!' && word <= '~') {
|
||||||
|
RE_Advance(P);
|
||||||
|
RE_Regex *right = RE_ParseSingle(P, arena, 0, 0);
|
||||||
|
if (right->kind == RE_MATCH_NULL) {
|
||||||
|
regex->kind = RE_MATCH_NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
RE_ASSERT(right->kind == RE_MATCH_WORD);
|
||||||
|
RE_ASSERT(right->word == word);
|
||||||
|
r->word_min = word > r->word ? r->word : word;
|
||||||
|
r->word_max = word > r->word ? word : r->word;
|
||||||
|
r->kind = RE_MATCH_RANGE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RE_DLL_QUEUE_ADD(regex->group.first, regex->group.last, r);
|
||||||
|
}
|
||||||
|
RE_Advance(P);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case '(': {
|
||||||
|
regex->kind = RE_MATCH_GROUP;
|
||||||
|
while (RE_Get(P) != 0 && RE_Get(P) != ')') {
|
||||||
|
RE_Regex *r = RE_ParseSingle(P, arena, ®ex->group.first, ®ex->group.last);
|
||||||
|
if (r->kind == RE_MATCH_NULL) {
|
||||||
|
regex->kind = RE_MATCH_NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
RE_DLL_QUEUE_ADD(regex->group.first, regex->group.last, r);
|
||||||
|
}
|
||||||
|
RE_Advance(P);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case '|': {
|
||||||
|
if (*last) {
|
||||||
|
regex->kind = RE_MATCH_OR;
|
||||||
|
RE_Regex *left = *last;
|
||||||
|
RE_Regex *right = RE_ParseSingle(P, arena, first, last);
|
||||||
|
if (right->kind == RE_MATCH_NULL) {
|
||||||
|
regex->kind = RE_MATCH_NULL;
|
||||||
|
RE_STRICT_ASSERT(!"Invalid regex syntax, '|' appeared but it's right option is invalid");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
RE_DLL_QUEUE_REMOVE(*first, *last, left);
|
||||||
|
regex->left = left;
|
||||||
|
regex->right = right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case '\\': {
|
||||||
|
regex->kind = RE_MATCH_WORD;
|
||||||
|
regex->word = RE_Get(P);
|
||||||
|
switch (regex->word) {
|
||||||
|
case 'n': regex->word = '\n'; break;
|
||||||
|
case 't': regex->word = '\t'; break;
|
||||||
|
case 'r': regex->word = '\r'; break;
|
||||||
|
case 'w': regex->kind = RE_MATCH_ANY_WORD; break;
|
||||||
|
case 'd': regex->kind = RE_MATCH_ANY_DIGIT; break;
|
||||||
|
case 's': regex->kind = RE_MATCH_ANY_WHITESPACE; break;
|
||||||
|
case '\0': {
|
||||||
|
regex->kind = RE_MATCH_NULL;
|
||||||
|
RE_STRICT_ASSERT(!"Invalid regex syntax, escape '\\' followed by end of string");
|
||||||
|
} break;
|
||||||
|
}
|
||||||
|
RE_Advance(P);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
default: {
|
||||||
|
regex->kind = RE_MATCH_WORD;
|
||||||
|
RE_Utf32Result decode = RE_ConvertUTF8ToUTF32(c, (int)size_left);
|
||||||
|
if (decode.error) {
|
||||||
|
regex->kind = RE_MATCH_NULL;
|
||||||
|
RE_STRICT_ASSERT(!"Invalid regex syntax, string is an invalid utf8");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
regex->word32 = decode.out_str;
|
||||||
|
for (int i = 0; i < decode.advance - 1; i += 1)
|
||||||
|
RE_Advance(P);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return regex;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_StaticFunc RE_Int RE_MatchSingle(RE_Regex *regex, RE_String string) {
|
||||||
|
switch (regex->kind) {
|
||||||
|
case RE_MATCH_ZERO_OR_MORE: {
|
||||||
|
RE_Int result = 0;
|
||||||
|
for (; string.len;) {
|
||||||
|
// @idea
|
||||||
|
// In this case (asd)*(asd) we just quit with 0
|
||||||
|
// when we meet asd
|
||||||
|
// Maybe this should be collapsed in parsing stage/
|
||||||
|
// asd should be combined with *asd etc. cause
|
||||||
|
// now it's a bit weird but I dont know why you would
|
||||||
|
// type that in the first place
|
||||||
|
if (RE_MatchSingle(regex->next, string) != -1) break;
|
||||||
|
RE_Int index = RE_MatchSingle(regex->child, string);
|
||||||
|
if (index == -1) break;
|
||||||
|
string = RE_Skip(string, index);
|
||||||
|
result += index;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case RE_MATCH_ONE_OR_MORE: {
|
||||||
|
RE_Int result = 0;
|
||||||
|
for (; string.len;) {
|
||||||
|
RE_Int index = RE_MatchSingle(regex->child, string);
|
||||||
|
if (index == -1) break;
|
||||||
|
string = RE_Skip(string, index);
|
||||||
|
result += index;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == 0) return -1;
|
||||||
|
return result;
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case RE_MATCH_OR: {
|
||||||
|
RE_Int right = RE_MatchSingle(regex->right, string);
|
||||||
|
RE_Int left = RE_MatchSingle(regex->left, string);
|
||||||
|
if (left > right) return left;
|
||||||
|
else return right;
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case RE_MATCH_GROUP: {
|
||||||
|
RE_Int result = 0;
|
||||||
|
for (RE_Regex *it = regex->group.first; it; it = it->next) {
|
||||||
|
if (string.len == 0) return -1;
|
||||||
|
RE_Int index = RE_MatchSingle(it, string);
|
||||||
|
if (index == -1) return -1;
|
||||||
|
result += index;
|
||||||
|
string = RE_Skip(string, index);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case RE_MATCH_NOT_SELECTED: {
|
||||||
|
for (RE_Regex *it = regex->group.first; it; it = it->next) {
|
||||||
|
RE_Int index = RE_MatchSingle(it, string);
|
||||||
|
if (index != -1) return -1;
|
||||||
|
}
|
||||||
|
RE_Utf32Result decode = RE_ConvertUTF8ToUTF32(string.str, (int)string.len);
|
||||||
|
if (decode.error) return -1;
|
||||||
|
return decode.advance;
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case RE_MATCH_SELECTED: {
|
||||||
|
for (RE_Regex *it = regex->group.first; it; it = it->next) {
|
||||||
|
RE_Int index = RE_MatchSingle(it, string);
|
||||||
|
if (index != -1) return index;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case RE_MATCH_RANGE: {
|
||||||
|
if (string.str[0] >= regex->word_min && string.str[0] <= regex->word_max)
|
||||||
|
return 1;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
case RE_MATCH_ANY_WORD: {
|
||||||
|
if ((string.str[0] >= 'a' && string.str[0] <= 'z') || (string.str[0] >= 'A' && string.str[0] <= 'Z'))
|
||||||
|
return 1;
|
||||||
|
return -1;
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case RE_MATCH_ANY_DIGIT: {
|
||||||
|
if (string.str[0] >= '0' && string.str[0] <= '9')
|
||||||
|
return 1;
|
||||||
|
return -1;
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case RE_MATCH_ANY_WHITESPACE: {
|
||||||
|
if (string.str[0] == ' ' || string.str[0] == '\n' || string.str[0] == '\t' || string.str[0] == '\r')
|
||||||
|
return 1;
|
||||||
|
return -1;
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case RE_MATCH_ANY: {
|
||||||
|
if (string.str[0] != '\n') {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case RE_MATCH_ZERO_OR_ONE: {
|
||||||
|
RE_Int index = RE_MatchSingle(regex->child, string);
|
||||||
|
if (index == -1) index = 0;
|
||||||
|
return index;
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case RE_MATCH_WORD: {
|
||||||
|
RE_Utf32Result decode = RE_ConvertUTF8ToUTF32(string.str, (int)string.len);
|
||||||
|
if (decode.error) return -1;
|
||||||
|
if (decode.out_str == regex->word32) return decode.advance;
|
||||||
|
return -1;
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case RE_MATCH_BACK:
|
||||||
|
case RE_MATCH_NULL: return -1;
|
||||||
|
|
||||||
|
default: RE_ASSERT(!"Invalid codepath");
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_API bool RE1_AreEqual(char *regex, char *string) {
|
||||||
|
char buff[4096];
|
||||||
|
RE_Regex *re = RE1_Parse(buff, sizeof(buff), regex);
|
||||||
|
bool result = RE3_AreEqual(re, string, RE_StringLength(string));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_API bool RE2_AreEqual(RE_Regex *regex, char *string) {
|
||||||
|
return RE3_AreEqual(regex, string, RE_StringLength(string));
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_API bool RE3_AreEqual(RE_Regex *regex, char *string, RE_Int len) {
|
||||||
|
RE_Int result = RE3_MatchFront(regex, string, len, string);
|
||||||
|
return result == len ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_API RE_Match RE1_Find(char *regex, char *string) {
|
||||||
|
char buff[4096];
|
||||||
|
RE_Regex *re = RE1_Parse(buff, sizeof(buff), regex);
|
||||||
|
RE_Match result = RE2_Find(re, string);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_API RE_Match RE2_Find(RE_Regex *regex, char *string) {
|
||||||
|
return RE3_Find(regex, string, RE_StringLength(string));
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_API RE_Match RE3_Find(RE_Regex *regex, char *string, RE_Int len) {
|
||||||
|
RE_Match result;
|
||||||
|
for (RE_Int i = 0; i < len; i += 1) {
|
||||||
|
result.size = RE3_MatchFront(regex, string + i, len - i, string);
|
||||||
|
if (result.size != -1) {
|
||||||
|
result.pos = i;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result.size = 0;
|
||||||
|
result.pos = -1;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_API RE_Match RE2_FindAgain(RE_Regex *regex, char *string, RE_Match prev_match) {
|
||||||
|
return RE2_Find(regex, string + prev_match.pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_API RE_Match RE3_FindAgain(RE_Regex *regex, char *string, RE_Int len, RE_Match prev_match) {
|
||||||
|
return RE3_Find(regex, string + prev_match.pos, len - prev_match.pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_API RE_Int RE3_MatchFront(RE_Regex *regex, char *string, RE_Int len, char *string_front) {
|
||||||
|
RE_String re_string;
|
||||||
|
re_string.str = string;
|
||||||
|
re_string.len = len;
|
||||||
|
RE_Int submatch_len = 0;
|
||||||
|
for (RE_Regex *it = regex; it; it = it->next) {
|
||||||
|
if (it->kind == RE_MATCH_FRONT) {
|
||||||
|
if (re_string.str == string_front)
|
||||||
|
continue;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (it->kind == RE_MATCH_BACK) {
|
||||||
|
if (re_string.len == 0)
|
||||||
|
continue;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_Int index = RE_MatchSingle(it, re_string);
|
||||||
|
if (index == -1) return -1;
|
||||||
|
re_string = RE_Skip(re_string, index);
|
||||||
|
submatch_len += index;
|
||||||
|
}
|
||||||
|
return submatch_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_API RE_Regex *RE1_ParseEx(RE_Arena *arena, char *string) {
|
||||||
|
return RE2_ParseEx(arena, string, RE_StringLength(string));
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_API RE_Regex *RE2_ParseEx(RE_Arena *arena, char *string, RE_Int len) {
|
||||||
|
RE_Parser P;
|
||||||
|
RE_MemoryZero(&P, sizeof(P));
|
||||||
|
P.string.str = string;
|
||||||
|
P.string.len = len;
|
||||||
|
|
||||||
|
for (; P.i < P.string.len;) {
|
||||||
|
RE_Regex *regex = RE_ParseSingle(&P, arena, &P.first, &P.last);
|
||||||
|
RE_DLL_QUEUE_ADD(P.first, P.last, regex);
|
||||||
|
if (regex->kind == RE_MATCH_NULL) {
|
||||||
|
P.first = &RE_NullRegex;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return P.first;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_API RE_Regex *RE1_Parse(char *buff, RE_Int buffsize, char *string) {
|
||||||
|
RE_Arena arena = RE_ArenaFromBuffer(buff, buffsize);
|
||||||
|
RE_Regex *result = RE1_ParseEx(&arena, string);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_API RE_Regex *RE2_Parse(char *buff, RE_Int buffsize, char *string, RE_Int len) {
|
||||||
|
RE_Arena arena = RE_ArenaFromBuffer(buff, buffsize);
|
||||||
|
RE_Regex *result = RE2_ParseEx(&arena, string, len);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
1931
stb_sprintf.h
Normal file
1931
stb_sprintf.h
Normal file
File diff suppressed because it is too large
Load Diff
616
string.h
Normal file
616
string.h
Normal file
@@ -0,0 +1,616 @@
|
|||||||
|
#ifndef S8_HEADER
|
||||||
|
#define S8_HEADER
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
// Preprocessor Input: ALLOCATOR_TYPE
|
||||||
|
|
||||||
|
#ifndef S8_FN
|
||||||
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
|
#define S8_FN __attribute__((unused)) static
|
||||||
|
#else
|
||||||
|
#define S8_FN static
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef S8_API
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#define S8_API extern "C"
|
||||||
|
#else
|
||||||
|
#define S8_API
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef S8_Allocator
|
||||||
|
struct MA_Arena;
|
||||||
|
#define S8_Allocator MA_Arena *
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct S8_String S8_String;
|
||||||
|
typedef struct S8_Node S8_Node;
|
||||||
|
typedef struct S8_List S8_List;
|
||||||
|
|
||||||
|
struct S8_String {
|
||||||
|
char *str;
|
||||||
|
int64_t len;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct S8_Node {
|
||||||
|
S8_Node *next;
|
||||||
|
S8_String string;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct S8_List {
|
||||||
|
int64_t node_count;
|
||||||
|
int64_t char_count;
|
||||||
|
S8_Node *first;
|
||||||
|
S8_Node *last;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
S8_NO_FLAGS = 0,
|
||||||
|
S8_IGNORE_CASE = 1,
|
||||||
|
S8_SPLIT_INCLUSIVE = 4,
|
||||||
|
S8_MATCH_FIND_LAST = 32,
|
||||||
|
};
|
||||||
|
|
||||||
|
#define S8_Lit(string) S8_Make((char *)string, sizeof(string) - 1)
|
||||||
|
#define S8_ConstLit(string) \
|
||||||
|
{ string, sizeof(string) - 1 }
|
||||||
|
#define S8_Expand(string) (int)(string).len, (string).str
|
||||||
|
|
||||||
|
#define S8_FORMAT(allocator, str, result) \
|
||||||
|
va_list args1; \
|
||||||
|
va_start(args1, str); \
|
||||||
|
S8_String result = S8_FormatV(allocator, str, args1); \
|
||||||
|
va_end(args1)
|
||||||
|
|
||||||
|
#define S8_For(it, x) for (S8_Node *it = (x).first; it; it = it->next)
|
||||||
|
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
S8_API bool S8_AreEqual(S8_String a, S8_String b, unsigned ignore_case);
|
||||||
|
inline bool operator==(S8_String a, S8_String b) { return S8_AreEqual(a, b, false); }
|
||||||
|
inline S8_String operator""_s(const char *str, size_t size) { return {(char *)str, (int64_t)size}; }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
S8_API char CHAR_ToLowerCase(char a);
|
||||||
|
S8_API char CHAR_ToUpperCase(char a);
|
||||||
|
S8_API bool CHAR_IsWhitespace(char w);
|
||||||
|
S8_API bool CHAR_IsAlphabetic(char a);
|
||||||
|
S8_API bool CHAR_IsIdent(char a);
|
||||||
|
S8_API bool CHAR_IsDigit(char a);
|
||||||
|
S8_API bool CHAR_IsAlphanumeric(char a);
|
||||||
|
S8_API bool S8_AreEqual(S8_String a, S8_String b, unsigned ignore_case);
|
||||||
|
S8_API bool S8_EndsWith(S8_String a, S8_String end, unsigned ignore_case);
|
||||||
|
S8_API bool S8_StartsWith(S8_String a, S8_String start, unsigned ignore_case);
|
||||||
|
S8_API S8_String S8_Make(char *str, int64_t len);
|
||||||
|
S8_API S8_String S8_Copy(S8_Allocator allocator, S8_String string);
|
||||||
|
S8_API void S8_NormalizePath(S8_String s);
|
||||||
|
S8_API S8_String S8_Chop(S8_String string, int64_t len);
|
||||||
|
S8_API S8_String S8_Skip(S8_String string, int64_t len);
|
||||||
|
S8_API S8_String S8_GetPostfix(S8_String string, int64_t len);
|
||||||
|
S8_API S8_String S8_GetPrefix(S8_String string, int64_t len);
|
||||||
|
S8_API S8_String S8_Slice(S8_String string, int64_t first_index, int64_t one_past_last_index);
|
||||||
|
S8_API S8_String S8_Trim(S8_String string);
|
||||||
|
S8_API S8_String S8_TrimEnd(S8_String string);
|
||||||
|
S8_API S8_String S8_ToLowerCase(S8_Allocator allocator, S8_String s);
|
||||||
|
S8_API S8_String S8_ToUpperCase(S8_Allocator allocator, S8_String s);
|
||||||
|
S8_API bool S8_Find(S8_String string, S8_String find, unsigned flags, int64_t *index_out);
|
||||||
|
S8_API S8_List S8_Split(S8_Allocator allocator, S8_String string, S8_String find, unsigned flags);
|
||||||
|
S8_API S8_String S8_MergeWithSeparator(S8_Allocator allocator, S8_List list, S8_String separator);
|
||||||
|
S8_API S8_String S8_Merge(S8_Allocator allocator, S8_List list);
|
||||||
|
S8_API S8_String S8_ReplaceAll(S8_Allocator allocator, S8_String string, S8_String replace, S8_String with, unsigned flags);
|
||||||
|
S8_API S8_List S8_FindAll(S8_Allocator allocator, S8_String string, S8_String find, unsigned flags);
|
||||||
|
S8_API S8_String S8_ChopLastSlash(S8_String s);
|
||||||
|
S8_API S8_String S8_ChopLastPeriod(S8_String s);
|
||||||
|
S8_API S8_String S8_SkipToLastSlash(S8_String s);
|
||||||
|
S8_API S8_String S8_SkipToLastPeriod(S8_String s);
|
||||||
|
S8_API bool S8_IsPointerInside(S8_String string, char *p);
|
||||||
|
S8_API S8_String S8_SkipToP(S8_String string, char *p);
|
||||||
|
S8_API S8_String S8_SkipPast(S8_String string, S8_String a);
|
||||||
|
S8_API int64_t S8_Length(char *string);
|
||||||
|
S8_API int64_t S8_WideLength(wchar_t *string);
|
||||||
|
S8_API S8_String S8_MakeFromChar(char *string);
|
||||||
|
S8_API S8_String S8_MakeEmpty(void);
|
||||||
|
S8_API S8_List S8_MakeEmptyList(void);
|
||||||
|
S8_API S8_String S8_FormatV(S8_Allocator allocator, const char *str, va_list args1);
|
||||||
|
S8_API S8_String S8_Format(S8_Allocator allocator, const char *str, ...);
|
||||||
|
S8_API S8_Node *S8_CreateNode(S8_Allocator allocator, S8_String string);
|
||||||
|
S8_API void S8_ReplaceNodeString(S8_List *list, S8_Node *node, S8_String new_string);
|
||||||
|
S8_API void S8_AddExistingNode(S8_List *list, S8_Node *node);
|
||||||
|
S8_API void S8_AddArray(S8_Allocator allocator, S8_List *list, char **array, int count);
|
||||||
|
S8_API void S8_AddArrayWithPrefix(S8_Allocator allocator, S8_List *list, char *prefix, char **array, int count);
|
||||||
|
S8_API S8_List S8_MakeList(S8_Allocator allocator, S8_String a);
|
||||||
|
S8_API S8_List S8_CopyList(S8_Allocator allocator, S8_List a);
|
||||||
|
S8_API S8_List S8_ConcatLists(S8_Allocator allocator, S8_List a, S8_List b);
|
||||||
|
S8_API S8_Node *S8_AddNode(S8_Allocator allocator, S8_List *list, S8_String string);
|
||||||
|
S8_API S8_String S8_AddF(S8_Allocator allocator, S8_List *list, const char *str, ...);
|
||||||
|
|
||||||
|
#endif // S8_HEADER
|
||||||
|
#ifdef S8_IMPLEMENTATION
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#ifndef S8_VSNPRINTF
|
||||||
|
#include <stdio.h>
|
||||||
|
#define S8_VSNPRINTF vsnprintf
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef S8_ALLOCATE
|
||||||
|
#include <stdlib.h>
|
||||||
|
#define S8_ALLOCATE(allocator, size) malloc(size)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef S8_ASSERT
|
||||||
|
#include <assert.h>
|
||||||
|
#define S8_ASSERT(x) assert(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef S8_MemoryCopy
|
||||||
|
#include <string.h>
|
||||||
|
#define S8_MemoryCopy(dst, src, s) memcpy(dst, src, s)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
S8_FN int64_t S8__ClampTop(int64_t val, int64_t max) {
|
||||||
|
if (val > max) val = max;
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API char CHAR_ToLowerCase(char a) {
|
||||||
|
if (a >= 'A' && a <= 'Z') a += 32;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API char CHAR_ToUpperCase(char a) {
|
||||||
|
if (a >= 'a' && a <= 'z') a -= 32;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API bool CHAR_IsWhitespace(char w) {
|
||||||
|
bool result = w == '\n' || w == ' ' || w == '\t' || w == '\v' || w == '\r';
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API bool CHAR_IsAlphabetic(char a) {
|
||||||
|
bool result = (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z');
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API bool CHAR_IsIdent(char a) {
|
||||||
|
bool result = (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z') || a == '_';
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API bool CHAR_IsDigit(char a) {
|
||||||
|
bool result = a >= '0' && a <= '9';
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API bool CHAR_IsAlphanumeric(char a) {
|
||||||
|
bool result = CHAR_IsDigit(a) || CHAR_IsAlphabetic(a);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API bool S8_AreEqual(S8_String a, S8_String b, unsigned ignore_case) {
|
||||||
|
if (a.len != b.len) return false;
|
||||||
|
for (int64_t i = 0; i < a.len; i++) {
|
||||||
|
char A = a.str[i];
|
||||||
|
char B = b.str[i];
|
||||||
|
if (ignore_case & S8_IGNORE_CASE) {
|
||||||
|
A = CHAR_ToLowerCase(A);
|
||||||
|
B = CHAR_ToLowerCase(B);
|
||||||
|
}
|
||||||
|
if (A != B)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API bool S8_EndsWith(S8_String a, S8_String end, unsigned ignore_case) {
|
||||||
|
S8_String a_end = S8_GetPostfix(a, end.len);
|
||||||
|
bool result = S8_AreEqual(end, a_end, ignore_case);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API bool S8_StartsWith(S8_String a, S8_String start, unsigned ignore_case) {
|
||||||
|
S8_String a_start = S8_GetPrefix(a, start.len);
|
||||||
|
bool result = S8_AreEqual(start, a_start, ignore_case);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_Make(char *str, int64_t len) {
|
||||||
|
S8_String result;
|
||||||
|
result.str = (char *)str;
|
||||||
|
result.len = len;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_Copy(S8_Allocator allocator, S8_String string) {
|
||||||
|
char *copy = (char *)S8_ALLOCATE(allocator, sizeof(char) * (string.len + 1));
|
||||||
|
S8_MemoryCopy(copy, string.str, string.len);
|
||||||
|
copy[string.len] = 0;
|
||||||
|
S8_String result = S8_Make(copy, string.len);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API void S8_NormalizePath(S8_String s) {
|
||||||
|
for (int64_t i = 0; i < s.len; i++) {
|
||||||
|
if (s.str[i] == '\\')
|
||||||
|
s.str[i] = '/';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_Chop(S8_String string, int64_t len) {
|
||||||
|
len = S8__ClampTop(len, string.len);
|
||||||
|
S8_String result = S8_Make(string.str, string.len - len);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_Skip(S8_String string, int64_t len) {
|
||||||
|
len = S8__ClampTop(len, string.len);
|
||||||
|
int64_t remain = string.len - len;
|
||||||
|
S8_String result = S8_Make(string.str + len, remain);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API bool S8_IsPointerInside(S8_String string, char *p) {
|
||||||
|
uintptr_t pointer = (uintptr_t)p;
|
||||||
|
uintptr_t start = (uintptr_t)string.str;
|
||||||
|
uintptr_t stop = start + (uintptr_t)string.len;
|
||||||
|
bool result = pointer >= start && pointer < stop;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_SkipToP(S8_String string, char *p) {
|
||||||
|
if (S8_IsPointerInside(string, p)) {
|
||||||
|
S8_String result = S8_Make(p, p - string.str);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_SkipPast(S8_String string, S8_String a) {
|
||||||
|
if (S8_IsPointerInside(string, a.str)) {
|
||||||
|
S8_String on_p = S8_Make(a.str, a.str - string.str);
|
||||||
|
S8_String result = S8_Skip(on_p, a.len);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_GetPostfix(S8_String string, int64_t len) {
|
||||||
|
len = S8__ClampTop(len, string.len);
|
||||||
|
int64_t remain_len = string.len - len;
|
||||||
|
S8_String result = S8_Make(string.str + remain_len, len);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_GetPrefix(S8_String string, int64_t len) {
|
||||||
|
len = S8__ClampTop(len, string.len);
|
||||||
|
S8_String result = S8_Make(string.str, len);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_Slice(S8_String string, int64_t first_index, int64_t one_past_last_index) {
|
||||||
|
if (one_past_last_index < 0) one_past_last_index = string.len + one_past_last_index + 1;
|
||||||
|
if (first_index < 0) first_index = string.len + first_index;
|
||||||
|
S8_ASSERT(first_index < one_past_last_index && "S8_Slice, first_index is bigger then one_past_last_index");
|
||||||
|
S8_ASSERT(string.len > 0 && "Slicing string of length 0! Might be an error!");
|
||||||
|
S8_String result = string;
|
||||||
|
if (string.len > 0) {
|
||||||
|
if (one_past_last_index > first_index) {
|
||||||
|
first_index = S8__ClampTop(first_index, string.len - 1);
|
||||||
|
one_past_last_index = S8__ClampTop(one_past_last_index, string.len);
|
||||||
|
result.str += first_index;
|
||||||
|
result.len = one_past_last_index - first_index;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.len = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_Trim(S8_String string) {
|
||||||
|
if (string.len == 0)
|
||||||
|
return string;
|
||||||
|
|
||||||
|
int64_t whitespace_begin = 0;
|
||||||
|
for (; whitespace_begin < string.len; whitespace_begin++) {
|
||||||
|
if (!CHAR_IsWhitespace(string.str[whitespace_begin])) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t whitespace_end = string.len;
|
||||||
|
for (; whitespace_end != whitespace_begin; whitespace_end--) {
|
||||||
|
if (!CHAR_IsWhitespace(string.str[whitespace_end - 1])) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (whitespace_begin == whitespace_end) {
|
||||||
|
string.len = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
string = S8_Slice(string, whitespace_begin, whitespace_end);
|
||||||
|
}
|
||||||
|
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_TrimEnd(S8_String string) {
|
||||||
|
int64_t whitespace_end = string.len;
|
||||||
|
for (; whitespace_end != 0; whitespace_end--) {
|
||||||
|
if (!CHAR_IsWhitespace(string.str[whitespace_end - 1])) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_String result = S8_GetPrefix(string, whitespace_end);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_ToLowerCase(S8_Allocator allocator, S8_String s) {
|
||||||
|
S8_String copy = S8_Copy(allocator, s);
|
||||||
|
for (int64_t i = 0; i < copy.len; i++) {
|
||||||
|
copy.str[i] = CHAR_ToLowerCase(copy.str[i]);
|
||||||
|
}
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_ToUpperCase(S8_Allocator allocator, S8_String s) {
|
||||||
|
S8_String copy = S8_Copy(allocator, s);
|
||||||
|
for (int64_t i = 0; i < copy.len; i++) {
|
||||||
|
copy.str[i] = CHAR_ToUpperCase(copy.str[i]);
|
||||||
|
}
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API bool S8_Find(S8_String string, S8_String find, unsigned flags, int64_t *index_out) {
|
||||||
|
bool result = false;
|
||||||
|
if (flags & S8_MATCH_FIND_LAST) {
|
||||||
|
for (int64_t i = string.len; i != 0; i--) {
|
||||||
|
int64_t index = i - 1;
|
||||||
|
S8_String substring = S8_Slice(string, index, index + find.len);
|
||||||
|
if (S8_AreEqual(substring, find, flags)) {
|
||||||
|
if (index_out)
|
||||||
|
*index_out = index;
|
||||||
|
result = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for (int64_t i = 0; i < string.len; i++) {
|
||||||
|
S8_String substring = S8_Slice(string, i, i + find.len);
|
||||||
|
if (S8_AreEqual(substring, find, flags)) {
|
||||||
|
if (index_out)
|
||||||
|
*index_out = i;
|
||||||
|
result = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_List S8_Split(S8_Allocator allocator, S8_String string, S8_String find, unsigned flags) {
|
||||||
|
S8_ASSERT((flags & S8_MATCH_FIND_LAST) == 0);
|
||||||
|
S8_List result = S8_MakeEmptyList();
|
||||||
|
int64_t index = 0;
|
||||||
|
while (S8_Find(string, find, flags, &index)) {
|
||||||
|
S8_String before_match = S8_Make(string.str, index);
|
||||||
|
S8_AddNode(allocator, &result, before_match);
|
||||||
|
if (flags & S8_SPLIT_INCLUSIVE) {
|
||||||
|
S8_String match = S8_Make(string.str + index, find.len);
|
||||||
|
S8_AddNode(allocator, &result, match);
|
||||||
|
}
|
||||||
|
string = S8_Skip(string, index + find.len);
|
||||||
|
}
|
||||||
|
if (string.len) S8_AddNode(allocator, &result, string);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_MergeWithSeparator(S8_Allocator allocator, S8_List list, S8_String separator) {
|
||||||
|
if (list.node_count == 0) return S8_MakeEmpty();
|
||||||
|
if (list.char_count == 0) return S8_MakeEmpty();
|
||||||
|
// S8_ASSERT((flags & S8_MATCH_FIND_LAST) == 0);
|
||||||
|
int64_t base_size = (list.char_count + 1);
|
||||||
|
int64_t sep_size = (list.node_count - 1) * separator.len;
|
||||||
|
int64_t size = base_size + sep_size;
|
||||||
|
char *buff = (char *)S8_ALLOCATE(allocator, sizeof(char) * size);
|
||||||
|
S8_String string = S8_Make(buff, 0);
|
||||||
|
for (S8_Node *it = list.first; it; it = it->next) {
|
||||||
|
S8_ASSERT(string.len + it->string.len <= size);
|
||||||
|
S8_MemoryCopy(string.str + string.len, it->string.str, it->string.len);
|
||||||
|
string.len += it->string.len;
|
||||||
|
if (it != list.last) {
|
||||||
|
S8_MemoryCopy(string.str + string.len, separator.str, separator.len);
|
||||||
|
string.len += separator.len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
S8_ASSERT(string.len == size - 1);
|
||||||
|
string.str[size] = 0;
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_Merge(S8_Allocator allocator, S8_List list) {
|
||||||
|
return S8_MergeWithSeparator(allocator, list, S8_Lit(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_ReplaceAll(S8_Allocator allocator, S8_String string, S8_String replace, S8_String with, unsigned flags) {
|
||||||
|
S8_ASSERT((flags & S8_MATCH_FIND_LAST) == 0);
|
||||||
|
S8_List list = S8_Split(allocator, string, replace, flags | S8_SPLIT_INCLUSIVE);
|
||||||
|
for (S8_Node *it = list.first; it; it = it->next) {
|
||||||
|
if (S8_AreEqual(it->string, replace, flags)) {
|
||||||
|
S8_ReplaceNodeString(&list, it, with);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
S8_String result = S8_Merge(allocator, list);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_List S8_FindAll(S8_Allocator allocator, S8_String string, S8_String find, unsigned flags) { // @untested
|
||||||
|
S8_ASSERT((flags & S8_MATCH_FIND_LAST) == 0);
|
||||||
|
S8_List result = S8_MakeEmptyList();
|
||||||
|
int64_t index = 0;
|
||||||
|
while (S8_Find(string, find, flags, &index)) {
|
||||||
|
S8_String match = S8_Make(string.str + index, find.len);
|
||||||
|
S8_AddNode(allocator, &result, match);
|
||||||
|
string = S8_Skip(string, index + find.len);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_ChopLastSlash(S8_String s) {
|
||||||
|
S8_String result = s;
|
||||||
|
S8_Find(s, S8_Lit("/"), S8_MATCH_FIND_LAST, &result.len);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_ChopLastPeriod(S8_String s) {
|
||||||
|
S8_String result = s;
|
||||||
|
S8_Find(s, S8_Lit("."), S8_MATCH_FIND_LAST, &result.len);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_SkipToLastSlash(S8_String s) {
|
||||||
|
int64_t pos;
|
||||||
|
S8_String result = s;
|
||||||
|
if (S8_Find(s, S8_Lit("/"), S8_MATCH_FIND_LAST, &pos)) {
|
||||||
|
result = S8_Skip(result, pos + 1);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_SkipToLastPeriod(S8_String s) {
|
||||||
|
int64_t pos;
|
||||||
|
S8_String result = s;
|
||||||
|
if (S8_Find(s, S8_Lit("."), S8_MATCH_FIND_LAST, &pos)) {
|
||||||
|
result = S8_Skip(result, pos + 1);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API int64_t S8_Length(char *string) {
|
||||||
|
int64_t len = 0;
|
||||||
|
while (*string++ != 0)
|
||||||
|
len++;
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API int64_t S8_WideLength(wchar_t *string) {
|
||||||
|
int64_t len = 0;
|
||||||
|
while (*string++ != 0)
|
||||||
|
len++;
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_MakeFromChar(char *string) {
|
||||||
|
S8_String result;
|
||||||
|
result.str = (char *)string;
|
||||||
|
result.len = S8_Length(string);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_MakeEmpty(void) {
|
||||||
|
return S8_Make(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_List S8_MakeEmptyList(void) {
|
||||||
|
S8_List result;
|
||||||
|
result.first = 0;
|
||||||
|
result.last = 0;
|
||||||
|
result.char_count = 0;
|
||||||
|
result.node_count = 0;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_FormatV(S8_Allocator allocator, const char *str, va_list args1) {
|
||||||
|
va_list args2;
|
||||||
|
va_copy(args2, args1);
|
||||||
|
int64_t len = S8_VSNPRINTF(0, 0, str, args2);
|
||||||
|
va_end(args2);
|
||||||
|
|
||||||
|
char *result = (char *)S8_ALLOCATE(allocator, sizeof(char) * (len + 1));
|
||||||
|
S8_VSNPRINTF(result, (int)(len + 1), str, args1);
|
||||||
|
S8_String res = S8_Make(result, len);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_Format(S8_Allocator allocator, const char *str, ...) {
|
||||||
|
S8_FORMAT(allocator, str, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_Node *S8_CreateNode(S8_Allocator allocator, S8_String string) {
|
||||||
|
S8_Node *result = (S8_Node *)S8_ALLOCATE(allocator, sizeof(S8_Node));
|
||||||
|
result->string = string;
|
||||||
|
result->next = 0;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API void S8_ReplaceNodeString(S8_List *list, S8_Node *node, S8_String new_string) {
|
||||||
|
list->char_count -= node->string.len;
|
||||||
|
list->char_count += new_string.len;
|
||||||
|
node->string = new_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API void S8_AddExistingNode(S8_List *list, S8_Node *node) {
|
||||||
|
if (list->first) {
|
||||||
|
list->last->next = node;
|
||||||
|
list->last = list->last->next;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
list->first = list->last = node;
|
||||||
|
}
|
||||||
|
list->node_count += 1;
|
||||||
|
list->char_count += node->string.len;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API void S8_AddArray(S8_Allocator allocator, S8_List *list, char **array, int count) {
|
||||||
|
for (int i = 0; i < count; i += 1) {
|
||||||
|
S8_String s = S8_MakeFromChar(array[i]);
|
||||||
|
S8_AddNode(allocator, list, s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API void S8_AddArrayWithPrefix(S8_Allocator allocator, S8_List *list, char *prefix, char **array, int count) {
|
||||||
|
for (int i = 0; i < count; i += 1) {
|
||||||
|
S8_AddF(allocator, list, "%s%s", prefix, array[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_List S8_MakeList(S8_Allocator allocator, S8_String a) {
|
||||||
|
S8_List result = S8_MakeEmptyList();
|
||||||
|
S8_AddNode(allocator, &result, a);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_List S8_CopyList(S8_Allocator allocator, S8_List a) {
|
||||||
|
S8_List result = S8_MakeEmptyList();
|
||||||
|
for (S8_Node *it = a.first; it; it = it->next) S8_AddNode(allocator, &result, it->string);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_List S8_ConcatLists(S8_Allocator allocator, S8_List a, S8_List b) {
|
||||||
|
S8_List result = S8_MakeEmptyList();
|
||||||
|
for (S8_Node *it = a.first; it; it = it->next) S8_AddNode(allocator, &result, it->string);
|
||||||
|
for (S8_Node *it = b.first; it; it = it->next) S8_AddNode(allocator, &result, it->string);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_Node *S8_AddNode(S8_Allocator allocator, S8_List *list, S8_String string) {
|
||||||
|
S8_Node *node = S8_CreateNode(allocator, string);
|
||||||
|
S8_AddExistingNode(list, node);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
S8_API S8_String S8_AddF(S8_Allocator allocator, S8_List *list, const char *str, ...) {
|
||||||
|
S8_FORMAT(allocator, str, result);
|
||||||
|
S8_AddNode(allocator, list, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // S8_IMPLEMENTATION
|
||||||
228
table.hpp
Normal file
228
table.hpp
Normal file
@@ -0,0 +1,228 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
/*
|
||||||
|
Hash table implementation:
|
||||||
|
Pointers to values
|
||||||
|
Open adressing
|
||||||
|
Linear Probing
|
||||||
|
Power of 2
|
||||||
|
Robin Hood hashing
|
||||||
|
Resizes on high probe count (min max load factor)
|
||||||
|
|
||||||
|
Hash 0 is reserved for empty hash table entry
|
||||||
|
*/
|
||||||
|
|
||||||
|
// #define TABLE_ALLOCATOR_TYPE Allocator
|
||||||
|
|
||||||
|
#ifndef TABLE_PRIVATE_FUNCTION
|
||||||
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
|
#define TABLE_PRIVATE_FUNCTION __attribute__((unused)) static
|
||||||
|
#else
|
||||||
|
#define TABLE_PRIVATE_FUNCTION static
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef TABLE_ALLOCATE
|
||||||
|
#include <stdlib.h>
|
||||||
|
#define TABLE_ALLOCATE(allocator, size) malloc(size)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef TABLE_DEALLOCATE
|
||||||
|
#include <stdlib.h>
|
||||||
|
#define TABLE_DEALLOCATE(allocator, p) free(p)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef TABLE_ASSERT
|
||||||
|
#include <assert.h>
|
||||||
|
#define TABLE_ASSERT(x) assert(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef TABLE_SET_DEFAULT_ALLOCATOR
|
||||||
|
#define TABLE_SET_DEFAULT_ALLOCATOR
|
||||||
|
// Example:
|
||||||
|
// #define TABLE_SET_DEFAULT_ALLOCATOR if (!allocator) allocator = global_heap;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef TABLE_HASH_BYTES
|
||||||
|
#define TABLE_HASH_BYTES TABLE__HashBytes
|
||||||
|
TABLE_PRIVATE_FUNCTION uint64_t TABLE__HashBytes(void *data, unsigned size) {
|
||||||
|
uint8_t *data8 = (uint8_t *)data;
|
||||||
|
uint64_t hash = (uint64_t)14695981039346656037ULL;
|
||||||
|
for (unsigned i = 0; i < size; i++) {
|
||||||
|
hash = hash ^ (uint64_t)(data8[i]);
|
||||||
|
hash = hash * (uint64_t)1099511628211ULL;
|
||||||
|
}
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define TABLE__WRAP_AROUND_POWER_OF_2(x, pow2) (((x) & ((pow2)-1llu)))
|
||||||
|
|
||||||
|
TABLE_PRIVATE_FUNCTION int TABLE_CStringLen(char *str) {
|
||||||
|
int i = 0;
|
||||||
|
while(str[i]) i += 1;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Value>
|
||||||
|
struct Table {
|
||||||
|
struct Entry {
|
||||||
|
uint64_t hash;
|
||||||
|
uint64_t key;
|
||||||
|
size_t distance;
|
||||||
|
Value value;
|
||||||
|
};
|
||||||
|
|
||||||
|
size_t len, cap;
|
||||||
|
Entry *values;
|
||||||
|
#ifdef TABLE_ALLOCATOR_TYPE
|
||||||
|
TABLE_ALLOCATOR_TYPE allocator;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
inline bool is_empty(Entry *entry) { return entry->hash == 0; }
|
||||||
|
inline bool is_occupied(Entry *entry) { return entry->hash != 0; }
|
||||||
|
|
||||||
|
void reserve(size_t size) {
|
||||||
|
TABLE_ASSERT(size > cap && "New size is smaller then original size");
|
||||||
|
TABLE_SET_DEFAULT_ALLOCATOR;
|
||||||
|
|
||||||
|
Entry *old_values = values;
|
||||||
|
size_t old_cap = cap;
|
||||||
|
|
||||||
|
values = (Entry *)TABLE_ALLOCATE(allocator, sizeof(Entry) * size);
|
||||||
|
for (int i = 0; i < size; i += 1) values[i] = {};
|
||||||
|
cap = size;
|
||||||
|
|
||||||
|
TABLE_ASSERT(!(old_values == 0 && len != 0));
|
||||||
|
if (len == 0) {
|
||||||
|
if (old_values) TABLE_DEALLOCATE(allocator, old_values);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
for (size_t i = 0; i < old_cap; i += 1) {
|
||||||
|
Entry *it = old_values + i;
|
||||||
|
if (is_occupied(it)) {
|
||||||
|
insert(it->key, it->value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TABLE_DEALLOCATE(allocator, old_values);
|
||||||
|
}
|
||||||
|
|
||||||
|
Entry *get_table_entry(uint64_t key) {
|
||||||
|
uint64_t hash = TABLE_HASH_BYTES(&key, sizeof(key));
|
||||||
|
if (hash == 0) hash += 1;
|
||||||
|
uint64_t index = TABLE__WRAP_AROUND_POWER_OF_2(hash, cap);
|
||||||
|
uint64_t i = index;
|
||||||
|
uint64_t distance = 0;
|
||||||
|
for (;;) {
|
||||||
|
Entry *it = values + i;
|
||||||
|
if (distance > it->distance) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (it->hash == hash && it->key == key) {
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
|
||||||
|
distance += 1;
|
||||||
|
i = TABLE__WRAP_AROUND_POWER_OF_2(i + 1, cap);
|
||||||
|
if (i == index) return 0;
|
||||||
|
}
|
||||||
|
TABLE_ASSERT(!"Invalid codepath");
|
||||||
|
}
|
||||||
|
|
||||||
|
// load factor calculation was rearranged
|
||||||
|
// to get rid of division:
|
||||||
|
//> 100 * len / cap = load_factor
|
||||||
|
//> len * 100 = load_factor * cap
|
||||||
|
inline bool reached_load_factor(size_t lfactor) {
|
||||||
|
return (len + 1) * 100 >= lfactor * cap;
|
||||||
|
}
|
||||||
|
|
||||||
|
void insert(uint64_t key, const Value &value) {
|
||||||
|
if (reached_load_factor(max_load_factor)) {
|
||||||
|
if (cap == 0) cap = 16; // 32 cause cap*2
|
||||||
|
reserve(cap * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t hash = TABLE_HASH_BYTES(&key, sizeof(key));
|
||||||
|
if (hash == 0) hash += 1;
|
||||||
|
uint64_t index = TABLE__WRAP_AROUND_POWER_OF_2(hash, cap);
|
||||||
|
uint64_t i = index;
|
||||||
|
Entry to_insert = {hash, key, 0, value};
|
||||||
|
for (;;) {
|
||||||
|
Entry *it = values + i;
|
||||||
|
if (is_empty(it)) {
|
||||||
|
*it = to_insert;
|
||||||
|
len += 1;
|
||||||
|
// If we have more then 8 consecutive items we try to resize
|
||||||
|
if (to_insert.distance > 8 && reached_load_factor(min_load_factor)) {
|
||||||
|
reserve(cap * 2);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (it->hash == hash && it->key == key) {
|
||||||
|
*it = to_insert;
|
||||||
|
// If we have more then 8 consecutive items we try to resize
|
||||||
|
if (to_insert.distance > 8 && reached_load_factor(min_load_factor)) {
|
||||||
|
reserve(cap * 2);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Robin hood hashing
|
||||||
|
if (to_insert.distance > it->distance) {
|
||||||
|
Entry temp = to_insert;
|
||||||
|
to_insert = *it;
|
||||||
|
*it = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
to_insert.distance += 1;
|
||||||
|
i = TABLE__WRAP_AROUND_POWER_OF_2(i + 1, cap);
|
||||||
|
TABLE_ASSERT(i != index && "Did a full 360 through a hash table, no good :( that shouldnt be possible");
|
||||||
|
}
|
||||||
|
TABLE_ASSERT(!"Invalid codepath");
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove(uint64_t key) {
|
||||||
|
Entry *entry = get_table_entry(key);
|
||||||
|
entry->hash = 0;
|
||||||
|
entry->distance = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Value *get(uint64_t key) {
|
||||||
|
Entry *v = get_table_entry(key);
|
||||||
|
if (!v) return 0;
|
||||||
|
return &v->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
Value *gets(char *str) {
|
||||||
|
int len = TABLE_CStringLen(str);
|
||||||
|
uint64_t hash = TABLE_HASH_BYTES(str, len);
|
||||||
|
return get(hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
void puts(char *str, const Value &value) {
|
||||||
|
int len = TABLE_CStringLen(str);
|
||||||
|
uint64_t hash = TABLE_HASH_BYTES(str, len);
|
||||||
|
insert(hash, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
len = 0;
|
||||||
|
for (size_t i = 0; i < cap; i += 1) {
|
||||||
|
Entry *it = values + i;
|
||||||
|
it->hash = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dealloc() {
|
||||||
|
COR_DEALLOCATE(allocator, values);
|
||||||
|
len = 0;
|
||||||
|
cap = 0;
|
||||||
|
values = 0;
|
||||||
|
}
|
||||||
|
static const size_t max_load_factor = 80;
|
||||||
|
static const size_t min_load_factor = 50;
|
||||||
|
static const size_t significant_distance = 8;
|
||||||
|
};
|
||||||
280
unicode.h
Normal file
280
unicode.h
Normal file
@@ -0,0 +1,280 @@
|
|||||||
|
#ifndef UTF_HEADER
|
||||||
|
#define UTF_HEADER
|
||||||
|
#include <stdint.h>
|
||||||
|
typedef struct UTF32_Result UTF32_Result;
|
||||||
|
typedef struct UTF8_Result UTF8_Result;
|
||||||
|
typedef struct UTF16_Result UTF16_Result;
|
||||||
|
typedef struct UTF8_Iter UTF8_Iter;
|
||||||
|
|
||||||
|
#ifndef UTF_StaticFunc
|
||||||
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
|
#define UTF_StaticFunc __attribute__((unused)) static
|
||||||
|
#else
|
||||||
|
#define UTF_StaticFunc static
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef UTF_API
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#define UTF_API extern "C"
|
||||||
|
#else
|
||||||
|
#define UTF_API
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct UTF32_Result {
|
||||||
|
uint32_t out_str;
|
||||||
|
int advance;
|
||||||
|
int error;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct UTF8_Result {
|
||||||
|
uint8_t out_str[4];
|
||||||
|
int len;
|
||||||
|
int error;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct UTF16_Result {
|
||||||
|
uint16_t out_str[2];
|
||||||
|
int len;
|
||||||
|
int error;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct UTF8_Iter {
|
||||||
|
char *str;
|
||||||
|
int len;
|
||||||
|
int utf8_codepoint_byte_size;
|
||||||
|
int i;
|
||||||
|
uint32_t item;
|
||||||
|
};
|
||||||
|
|
||||||
|
UTF_API UTF32_Result UTF_ConvertUTF16ToUTF32(uint16_t *c, int max_advance);
|
||||||
|
UTF_API UTF8_Result UTF_ConvertUTF32ToUTF8(uint32_t codepoint);
|
||||||
|
UTF_API UTF32_Result UTF_ConvertUTF8ToUTF32(char *c, int max_advance);
|
||||||
|
UTF_API UTF16_Result UTF_ConvertUTF32ToUTF16(uint32_t codepoint);
|
||||||
|
UTF_API int64_t UTF_CreateCharFromWidechar(char *buffer, int64_t buffer_size, wchar_t *in, int64_t inlen);
|
||||||
|
UTF_API int64_t UTF_CreateWidecharFromChar(wchar_t *buffer, int64_t buffer_size, char *in, int64_t inlen);
|
||||||
|
UTF_API void UTF8_Advance(UTF8_Iter *iter);
|
||||||
|
UTF_API UTF8_Iter UTF8_IterateEx(char *str, int len);
|
||||||
|
UTF_API UTF8_Iter UTF8_Iterate(char *str);
|
||||||
|
|
||||||
|
#define UTF8_For(name, str, len) for (UTF8_Iter name = UTF8_IterateEx(str, (int)len); name.item; UTF8_Advance(&name))
|
||||||
|
|
||||||
|
#endif // UTF_HEADER
|
||||||
|
#ifdef UTF_IMPLEMENTATION
|
||||||
|
|
||||||
|
UTF_StaticFunc int UTF__StringLength(char *string) {
|
||||||
|
int len = 0;
|
||||||
|
while (*string++ != 0)
|
||||||
|
len++;
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
UTF_StaticFunc void UTF__MemoryZero(void *p, size_t size) {
|
||||||
|
uint8_t *p8 = (uint8_t *)p;
|
||||||
|
while (size--) *p8++ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
UTF_API UTF32_Result UTF_ConvertUTF16ToUTF32(uint16_t *c, int max_advance) {
|
||||||
|
UTF32_Result result;
|
||||||
|
UTF__MemoryZero(&result, sizeof(result));
|
||||||
|
if (max_advance >= 1) {
|
||||||
|
result.advance = 1;
|
||||||
|
result.out_str = c[0];
|
||||||
|
if (c[0] >= 0xD800 && c[0] <= 0xDBFF && c[1] >= 0xDC00 && c[1] <= 0xDFFF) {
|
||||||
|
if (max_advance >= 2) {
|
||||||
|
result.out_str = 0x10000;
|
||||||
|
result.out_str += (uint32_t)(c[0] & 0x03FF) << 10u | (c[1] & 0x03FF);
|
||||||
|
result.advance = 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
result.error = 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.error = 1;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
UTF_API UTF8_Result UTF_ConvertUTF32ToUTF8(uint32_t codepoint) {
|
||||||
|
UTF8_Result result;
|
||||||
|
UTF__MemoryZero(&result, sizeof(result));
|
||||||
|
|
||||||
|
if (codepoint <= 0x7F) {
|
||||||
|
result.len = 1;
|
||||||
|
result.out_str[0] = (char)codepoint;
|
||||||
|
}
|
||||||
|
else if (codepoint <= 0x7FF) {
|
||||||
|
result.len = 2;
|
||||||
|
result.out_str[0] = 0xc0 | (0x1f & (codepoint >> 6));
|
||||||
|
result.out_str[1] = 0x80 | (0x3f & codepoint);
|
||||||
|
}
|
||||||
|
else if (codepoint <= 0xFFFF) { // 16 bit word
|
||||||
|
result.len = 3;
|
||||||
|
result.out_str[0] = 0xe0 | (0xf & (codepoint >> 12)); // 4 bits
|
||||||
|
result.out_str[1] = 0x80 | (0x3f & (codepoint >> 6)); // 6 bits
|
||||||
|
result.out_str[2] = 0x80 | (0x3f & codepoint); // 6 bits
|
||||||
|
}
|
||||||
|
else if (codepoint <= 0x10FFFF) { // 21 bit word
|
||||||
|
result.len = 4;
|
||||||
|
result.out_str[0] = 0xf0 | (0x7 & (codepoint >> 18)); // 3 bits
|
||||||
|
result.out_str[1] = 0x80 | (0x3f & (codepoint >> 12)); // 6 bits
|
||||||
|
result.out_str[2] = 0x80 | (0x3f & (codepoint >> 6)); // 6 bits
|
||||||
|
result.out_str[3] = 0x80 | (0x3f & codepoint); // 6 bits
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.error = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
UTF_API UTF32_Result UTF_ConvertUTF8ToUTF32(char *c, int max_advance) {
|
||||||
|
UTF32_Result result;
|
||||||
|
UTF__MemoryZero(&result, sizeof(result));
|
||||||
|
|
||||||
|
if ((c[0] & 0x80) == 0) { // Check if leftmost zero of first byte is unset
|
||||||
|
if (max_advance >= 1) {
|
||||||
|
result.out_str = c[0];
|
||||||
|
result.advance = 1;
|
||||||
|
}
|
||||||
|
else result.error = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if ((c[0] & 0xe0) == 0xc0) {
|
||||||
|
if ((c[1] & 0xc0) == 0x80) { // Continuation byte required
|
||||||
|
if (max_advance >= 2) {
|
||||||
|
result.out_str = (uint32_t)(c[0] & 0x1f) << 6u | (c[1] & 0x3f);
|
||||||
|
result.advance = 2;
|
||||||
|
}
|
||||||
|
else result.error = 2;
|
||||||
|
}
|
||||||
|
else result.error = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if ((c[0] & 0xf0) == 0xe0) {
|
||||||
|
if ((c[1] & 0xc0) == 0x80 && (c[2] & 0xc0) == 0x80) { // Two continuation bytes required
|
||||||
|
if (max_advance >= 3) {
|
||||||
|
result.out_str = (uint32_t)(c[0] & 0xf) << 12u | (uint32_t)(c[1] & 0x3f) << 6u | (c[2] & 0x3f);
|
||||||
|
result.advance = 3;
|
||||||
|
}
|
||||||
|
else result.error = 3;
|
||||||
|
}
|
||||||
|
else result.error = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if ((c[0] & 0xf8) == 0xf0) {
|
||||||
|
if ((c[1] & 0xc0) == 0x80 && (c[2] & 0xc0) == 0x80 && (c[3] & 0xc0) == 0x80) { // Three continuation bytes required
|
||||||
|
if (max_advance >= 4) {
|
||||||
|
result.out_str = (uint32_t)(c[0] & 0xf) << 18u | (uint32_t)(c[1] & 0x3f) << 12u | (uint32_t)(c[2] & 0x3f) << 6u | (uint32_t)(c[3] & 0x3f);
|
||||||
|
result.advance = 4;
|
||||||
|
}
|
||||||
|
else result.error = 4;
|
||||||
|
}
|
||||||
|
else result.error = 4;
|
||||||
|
}
|
||||||
|
else result.error = 4;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
UTF_API UTF16_Result UTF_ConvertUTF32ToUTF16(uint32_t codepoint) {
|
||||||
|
UTF16_Result result;
|
||||||
|
UTF__MemoryZero(&result, sizeof(result));
|
||||||
|
if (codepoint < 0x10000) {
|
||||||
|
result.out_str[0] = (uint16_t)codepoint;
|
||||||
|
result.out_str[1] = 0;
|
||||||
|
result.len = 1;
|
||||||
|
}
|
||||||
|
else if (codepoint <= 0x10FFFF) {
|
||||||
|
uint32_t code = (codepoint - 0x10000);
|
||||||
|
result.out_str[0] = (uint16_t)(0xD800 | (code >> 10));
|
||||||
|
result.out_str[1] = (uint16_t)(0xDC00 | (code & 0x3FF));
|
||||||
|
result.len = 2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.error = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define UTF__HANDLE_DECODE_ERROR(question_mark) \
|
||||||
|
{ \
|
||||||
|
if (outlen < buffer_size - 1) buffer[outlen++] = (question_mark); \
|
||||||
|
break; \
|
||||||
|
}
|
||||||
|
|
||||||
|
UTF_API int64_t UTF_CreateCharFromWidechar(char *buffer, int64_t buffer_size, wchar_t *in, int64_t inlen) {
|
||||||
|
int64_t outlen = 0;
|
||||||
|
for (int64_t i = 0; i < inlen && in[i];) {
|
||||||
|
UTF32_Result decode = UTF_ConvertUTF16ToUTF32((uint16_t *)(in + i), (int)(inlen - i));
|
||||||
|
if (!decode.error) {
|
||||||
|
i += decode.advance;
|
||||||
|
UTF8_Result encode = UTF_ConvertUTF32ToUTF8(decode.out_str);
|
||||||
|
if (!encode.error) {
|
||||||
|
for (int64_t j = 0; j < encode.len; j++) {
|
||||||
|
if (outlen < buffer_size - 1) {
|
||||||
|
buffer[outlen++] = encode.out_str[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else UTF__HANDLE_DECODE_ERROR('?');
|
||||||
|
}
|
||||||
|
else UTF__HANDLE_DECODE_ERROR('?');
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[outlen] = 0;
|
||||||
|
return outlen;
|
||||||
|
}
|
||||||
|
|
||||||
|
UTF_API int64_t UTF_CreateWidecharFromChar(wchar_t *buffer, int64_t buffer_size, char *in, int64_t inlen) {
|
||||||
|
int64_t outlen = 0;
|
||||||
|
for (int64_t i = 0; i < inlen;) {
|
||||||
|
UTF32_Result decode = UTF_ConvertUTF8ToUTF32(in + i, (int)(inlen - i));
|
||||||
|
if (!decode.error) {
|
||||||
|
i += decode.advance;
|
||||||
|
UTF16_Result encode = UTF_ConvertUTF32ToUTF16(decode.out_str);
|
||||||
|
if (!encode.error) {
|
||||||
|
for (int64_t j = 0; j < encode.len; j++) {
|
||||||
|
if (outlen < buffer_size - 1) {
|
||||||
|
buffer[outlen++] = encode.out_str[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else UTF__HANDLE_DECODE_ERROR(0x003f);
|
||||||
|
}
|
||||||
|
else UTF__HANDLE_DECODE_ERROR(0x003f);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[outlen] = 0;
|
||||||
|
return outlen;
|
||||||
|
}
|
||||||
|
|
||||||
|
UTF_API void UTF8_Advance(UTF8_Iter *iter) {
|
||||||
|
iter->i += iter->utf8_codepoint_byte_size;
|
||||||
|
UTF32_Result r = UTF_ConvertUTF8ToUTF32(iter->str + iter->i, iter->len - iter->i);
|
||||||
|
if (r.error) {
|
||||||
|
iter->item = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
iter->utf8_codepoint_byte_size = r.advance;
|
||||||
|
iter->item = r.out_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
UTF_API UTF8_Iter UTF8_IterateEx(char *str, int len) {
|
||||||
|
UTF8_Iter result;
|
||||||
|
UTF__MemoryZero(&result, sizeof(result));
|
||||||
|
result.str = str;
|
||||||
|
result.len = len;
|
||||||
|
if (len) UTF8_Advance(&result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
UTF_API UTF8_Iter UTF8_Iterate(char *str) {
|
||||||
|
return UTF8_IterateEx(str, UTF__StringLength(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // UTF_IMPLEMENTATION
|
||||||
Reference in New Issue
Block a user