This commit is contained in:
2025-11-09 13:30:05 +11:00
parent e9290ba9f2
commit 768424e199
2 changed files with 66 additions and 96 deletions

View File

@@ -78,36 +78,7 @@ void scratchEnd(Scratch scratch) {
arenaFreeFrom(scratch.arena, scratch.start); arenaFreeFrom(scratch.arena, scratch.start);
} }
template <typename T> const char *cstring(Arena *arena, CharList buf) {
T *appendList(list<T> *list, T element) {
if (list->head < list->length) {
list->data[list->head] = element;
list->head++;
return &(list->data[list->head - 1]);
} else {
return 0;
}
}
template <typename T>
void zeroListFull(list<T> *list) {
memset(list->data, 0, list->head * sizeof(T));
}
template <typename T>
void zeroList(list<T> *list) {
list->head = 0;
memset(list->data, 0, list->head * sizeof(T));
}
inline string operator""_s(const char *cstrLiteral, size_t length) {
return {
(char *)cstrLiteral,
length,
};
}
const char *cstring(Arena *arena, list<char> buf) {
char *arr = PushArray(arena, char, buf.length + 1); char *arr = PushArray(arena, char, buf.length + 1);
memmove(arr, buf.data, buf.length); memmove(arr, buf.data, buf.length);
arr[buf.length] = '\0'; arr[buf.length] = '\0';
@@ -245,8 +216,8 @@ inline bool isNumeric(char c) {
return stringContains(NUMERIC_CHARS, c); return stringContains(NUMERIC_CHARS, c);
} }
list<string> strSplit(Arena *arena, string splitStr, string inputStr) { StringList strSplit(Arena *arena, string splitStr, string inputStr) {
list<string> result = {0}; StringList result = {0};
if (inputStr.length > 0) { if (inputStr.length > 0) {
size_t splitCount = 0; size_t splitCount = 0;
size_t c = 0; size_t c = 0;
@@ -333,10 +304,10 @@ ParsePositiveReal32Result parsePositiveReal32(string str, size_t *lengthPointer)
return result; return result;
} }
list<string> getArgs(Arena *arena, int argc, char **argv) { StringList getArgs(Arena *arena, int argc, char **argv) {
list<string> args = PushList(arena, string, (size_t)argc - 1); StringList args = PushList(arena, StringList, (size_t)argc - 1);
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
appendList(&args, strFromCString(arena, argv[i])); AppendList(string, &args, strFromCString(arena, argv[i]));
} }
return args; return args;
} }
@@ -354,7 +325,7 @@ Timestamp timestampFromUnixTime(UnixTimestamp *unixTimestamp) {
} }
string formatTimeHms(Arena *arena, UnixTimestamp time) { string formatTimeHms(Arena *arena, UnixTimestamp time) {
local_persist const string format = "HH-MM-SS"_s; local_persist const string format = strlit("HH-MM-SS");
string buf = PushString(arena, format.length); string buf = PushString(arena, format.length);
tm *timestamp = gmtime((time_t *)&time); tm *timestamp = gmtime((time_t *)&time);
strftime(buf.str, buf.length + 1, "%T", timestamp); strftime(buf.str, buf.length + 1, "%T", timestamp);
@@ -362,14 +333,14 @@ string formatTimeHms(Arena *arena, UnixTimestamp time) {
} }
string formatTimeHms(Arena *arena, Timestamp *time) { string formatTimeHms(Arena *arena, Timestamp *time) {
local_persist const string format = "HH-MM-SS"_s; local_persist const string format = s("HH-MM-SS");
string buf = PushString(arena, format.length); string buf = PushString(arena, format.length);
strftime(buf.str, buf.length + 1, "%T", (tm *)time); strftime(buf.str, buf.length + 1, "%T", (tm *)time);
return buf; return buf;
} }
string formatTimeYmd(Arena *arena, UnixTimestamp time) { string formatTimeYmd(Arena *arena, UnixTimestamp time) {
local_persist const string format = "YYYY-mm-dd"_s; local_persist const string format = s("YYYY-mm-dd");
string buf = PushString(arena, format.length); string buf = PushString(arena, format.length);
tm *timestamp = gmtime((time_t *)&time); tm *timestamp = gmtime((time_t *)&time);
strftime(buf.str, buf.length + 1, "%Y-%m-%d", timestamp); strftime(buf.str, buf.length + 1, "%Y-%m-%d", timestamp);
@@ -377,7 +348,7 @@ string formatTimeYmd(Arena *arena, UnixTimestamp time) {
} }
string formatTimeYmd(Arena *arena, Timestamp *time) { string formatTimeYmd(Arena *arena, Timestamp *time) {
local_persist const string format = "YYYY-mm-dd"_s; local_persist const string format = s("YYYY-mm-dd");
string buf = PushString(arena, format.length); string buf = PushString(arena, format.length);
strftime(buf.str, buf.length + 1, "%Y-%m-%d", (tm *)time); strftime(buf.str, buf.length + 1, "%Y-%m-%d", (tm *)time);
return buf; return buf;

113
core.h
View File

@@ -2,6 +2,7 @@
#define CORE_H #define CORE_H
// cstdlib includes // cstdlib includes
#include <cwchar>
#include <math.h> #include <math.h>
#include <stdint.h> // necessary for int type sizes #include <stdint.h> // necessary for int type sizes
#include <stdio.h> #include <stdio.h>
@@ -30,6 +31,7 @@ typedef uint64_t uint64;
typedef uint8_t byte; typedef uint8_t byte;
typedef float real32; typedef float real32;
typedef double real64; typedef double real64;
typedef struct string string;
// ### Sizes and Numbers ### // ### Sizes and Numbers ###
#define Bytes(n) (n) #define Bytes(n) (n)
@@ -74,59 +76,53 @@ void scratchEnd(Scratch scratch);
#define PushStructZero(arena, type) (type *)pushSizeFill(arena, sizeof(type), 0) #define PushStructZero(arena, type) (type *)pushSizeFill(arena, sizeof(type), 0)
// ### Vectors ### // ### Vectors ###
template <typename T> union Vec2 {
union Vector2 {
struct { struct {
T x; real32 x;
T y; real32 y;
}; };
T vec[2]; real32 vec[2];
}; };
template <typename T> inline function Vec2 vec2(real32 x, real32 y) {
inline function Vector2<T> vec2(T x, T y) { Vec2 result = {0};
Vector2<T> result = {0};
result.x = x; result.x = x;
result.y = y; result.y = y;
return result; return result;
} }
template <typename T> union Vec3 {
union Vector3 {
struct { struct {
T x; real32 x;
T y; real32 y;
T z; real32 z;
}; };
T vec[3]; real32 vec[3];
}; };
template <typename T> inline function Vec3 vec3(real32 x, real32 y, real32 z) {
inline function Vector3<T> vec3(T x, T y, T z) { Vec3 result = {0};
Vector3<T> result = {0};
result.x = x; result.x = x;
result.y = y; result.y = y;
result.z = z; result.z = z;
return result; return result;
} }
template <typename T> union Vec4 {
union Vector4 {
struct { struct {
T r; real32 r;
T g; real32 g;
T b; real32 b;
T a; real32 a;
}; };
struct { struct {
T x; real32 x;
T y; real32 y;
T z; real32 z;
T w; real32 w;
}; };
T vec[4]; real32 vec[4];
}; };
template <typename T> inline function Vec4 vec4(real32 x, real32 y, real32 z, real32 w) {
inline function Vector4<T> vec4(T x, T y, T z, T w) { Vec4 result = {0};
Vector4<T> result = {0};
result.x = x; result.x = x;
result.y = y; result.y = y;
result.z = z; result.z = z;
@@ -135,24 +131,29 @@ inline function Vector4<T> vec4(T x, T y, T z, T w) {
} }
// ### Lists ### // ### Lists ###
template <typename T> #define DefineList(type, prefix) \
struct list { typedef struct {\
T* data; type* data;\
size_t length; size_t length;\
size_t head; size_t head;\
}; } prefix ## List
#define PushList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, 0 }) DefineList(string, String);
#define EmptyList(type) (list<type>{ NULL, 0, 0 })
#define PushListZero(arena, type, size) (list<type>{ PushArrayZero(arena, type, size), size, 0 })
#define PushFullList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, size })
#define PushFullListZero(arena, type, size) (list<type>{ PushArrayZero(arena, type, size), size, size })
#define ArrayAsList(type, array) (list<type>{ array, ArrayCount(array), ArrayCount(array) })
template <typename T> T *appendList(list<T> *list, T element); #define PushList(arena, type, size) ((type){ PushArray(arena, type, size), size, 0 })
template <typename T> void zeroList(list<T> *list); #define EmptyList(type) ((type){ NULL, 0, 0 })
template <typename T> void zeroListFull(list<T> *list); #define PushListZero(arena, type, size) ((type){ PushArrayZero(arena, type, size), size, 0 })
template <typename T> list<T> listSlice(list<T> l, size_t start, size_t stop = 0); #define PushFullList(arena, type, size) ((type){ PushArray(arena, type, size), size, size })
#define PushFullListZero(arena, type, size) ((type){ PushArrayZero(arena, type, size), size, size })
#define ArrayAsList(type, array) ((type){ array, ArrayCount(array), ArrayCount(array) })
#define AppendList(type, list, element) \
if ((list)->head < (list)->length) { \
(list)->data[(list)->head++] = (element); \
}
#define ZeroListFull(list) memset((list)->data, 0, (list)->head * sizeof(T))
#define ZeroList(list) (list)->head = 0; \
memset((list)->data, 0, (list)->head * sizeof(T));
// ### Strings ### // ### Strings ###
struct string { struct string {
@@ -162,13 +163,13 @@ struct string {
#define STB_SPRINTF_DECORATE(name) stb_##name // define this before including if you want to change the names #define STB_SPRINTF_DECORATE(name) stb_##name // define this before including if you want to change the names
#include "vendor/stb_sprintf.h" #include "vendor/stb_sprintf.h"
#define strlit(lit) (string{(char *)(lit), sizeof(lit) - 1}) #define s(lit) (string{(char *)(lit), sizeof(lit) - 1})
#define PushString(arena, length) (string{ (char *)pushSize(arena, length), (length) }) #define PushString(arena, length) (string{ (char *)pushSize(arena, length), (length) })
#define PushStringFill(arena, length, characterByte) (string{ (char *)pushSizeFill(arena, length, characterByte), (length) }) #define PushStringFill(arena, length, characterByte) (string{ (char *)pushSizeFill(arena, length, characterByte), (length) })
string operator""_s(const char *cstrLiteral, size_t length);
// C Strings // C Strings
const char *cstring(Arena *arena, list<char> buf); DefineList(char, Char);
const char *cstringFromCharList(Arena *arena, CharList buf);
const char *cstring(Arena *arena, string str); const char *cstring(Arena *arena, string str);
size_t calcStringLen(const char *str); size_t calcStringLen(const char *str);
string strFromCString(Arena *arena, const char *str); string strFromCString(Arena *arena, const char *str);
@@ -180,7 +181,7 @@ bool stringContains(string str, char c);
string strReverse(Arena *arena, string str); string strReverse(Arena *arena, string str);
string strSlice(string str, size_t start, size_t stop = 0); string strSlice(string str, size_t start, size_t stop = 0);
string strSlice(char *data, size_t start, size_t stop = 0); string strSlice(char *data, size_t start, size_t stop = 0);
list<string> strSplit(Arena *arena, string splitStr, string inputStr); StringList strSplit(Arena *arena, string splitStr, string inputStr);
string strPrintfv(Arena *arena, const char *fmt, va_list args); string strPrintfv(Arena *arena, const char *fmt, va_list args);
string strPrintf(Arena *arena, const char *fmt, ...); string strPrintf(Arena *arena, const char *fmt, ...);
@@ -192,7 +193,7 @@ ParsePositiveReal32Result parsePositiveReal32(Arena *arena, string str, size_t *
inline function bool isNumeric(char c); inline function bool isNumeric(char c);
// ### Cmdline ### // ### Cmdline ###
list<string> getArgs(Arena *arena, int argc, char **argv); StringList getArgs(Arena *arena, int argc, char **argv);
// ### Time ### // ### Time ###
typedef uint64 UnixTimestamp; typedef uint64 UnixTimestamp;
@@ -261,11 +262,9 @@ enum StdStream {
#define COLOR_TEXT_FG_BG(text, foregroundcolor, backgroundcolor) ANSI_INSTRUCTION_FROM_ENUM(foregroundcolor) ANSI_INSTRUCTION_FROM_ENUM(backgroundcolor) text ANSI_RESET #define COLOR_TEXT_FG_BG(text, foregroundcolor, backgroundcolor) ANSI_INSTRUCTION_FROM_ENUM(foregroundcolor) ANSI_INSTRUCTION_FROM_ENUM(backgroundcolor) text ANSI_RESET
#define COLOR_TEXT_RGB(text, red, green, blue) ANSI_INSTRUCTION_STR("38;2;" #red ";" #green ";" #blue) text ANSI_RESET #define COLOR_TEXT_RGB(text, red, green, blue) ANSI_INSTRUCTION_STR("38;2;" #red ";" #green ";" #blue) text ANSI_RESET
void print(list<int> l, StdStream target = StdStream_stdout); DefineList(int, Int);
void print(list<string> l, StdStream target = StdStream_stdout); void printIntList(IntList l, StdStream target = StdStream_stdout);
void print(list<Vector2<real32>> l, StdStream target = StdStream_stdout); void printStrList(StringList l, StdStream target = StdStream_stdout);
void print(list<Vector3<real32>> l, StdStream target = StdStream_stdout);
void print(list<Vector4<real32>> l, StdStream target = StdStream_stdout);
void print(const char *fmt, ...); void print(const char *fmt, ...);
void printErr(const char *fmt, ...); void printErr(const char *fmt, ...);