update
This commit is contained in:
43
core.h
43
core.h
@@ -5,6 +5,7 @@
|
||||
#include "stdbool.h"
|
||||
#include "stdint.h" // necessary for int type sizes
|
||||
#include "time.h" // TODO(djledda): try not to depend on this one
|
||||
#include <string.h>
|
||||
|
||||
// ### Misc macros ###
|
||||
#if DJSTDLIB_DEBUG
|
||||
@@ -151,6 +152,7 @@ inline function Vec4 vec4(real32 x, real32 y, real32 z, real32 w) {
|
||||
#define ListElementSize(list) MemberSizeUnderlying(list, data)
|
||||
|
||||
DefineList(string, String);
|
||||
DefineList(void, Void);
|
||||
|
||||
#define PushList(arena, type, size) (type){ .data=pushSize(arena, ListElementSize(type)*(size)), .length=0, .capacity=(size) }
|
||||
#define PushListZero(arena, type, size) (type){ .data=pushSizeFill(arena, ListElementSize(type)*(size), 0), .length=0, .capacity=(size) }
|
||||
@@ -161,22 +163,37 @@ DefineList(string, String);
|
||||
#define __ArrayAsList(array) { .data=(array), .length=ArrayCount(array), .capacity=ArrayCount(array) }
|
||||
#define AsList(listtype, ...) (listtype)__ArrayAsList(((listtype##_underlying[])__VA_ARGS__))
|
||||
|
||||
// TODO(dledda): clone list
|
||||
// TODO(dledda): list back element accessor
|
||||
#define AppendList(list, element) \
|
||||
if ((list)->length < (list)->capacity) { \
|
||||
(list)->data[(list)->length++] = (element); \
|
||||
#define ListAppend(list, element) \
|
||||
if ((list).length < (list).capacity) { \
|
||||
(list).data[(list).length++] = (element); \
|
||||
}
|
||||
#define ZeroListFull(list) memset((list)->data, 0, (list)->length * sizeof((list)->data[0]))
|
||||
#define ZeroList(list) (list)->length = 0; \
|
||||
memset((list)->data, 0, (list)->length * sizeof((list)->data[0]))
|
||||
/** 1 indicates last element, 2 second last, etc. */
|
||||
#define ListGetFromEnd(list, count) ((list).data[(list).length - (count)])
|
||||
#define ListLast(list) ListGetFromEnd(list, 1)
|
||||
|
||||
inline VoidList __cloneList(Arena *arena, VoidList list, size_t underlyingSize) {
|
||||
byte *newData = PushArrayZero(arena, void, list.capacity*underlyingSize);
|
||||
memcpy(newData, list.data, list.capacity*underlyingSize);
|
||||
return (VoidList){
|
||||
.data=newData,
|
||||
.capacity=list.capacity,
|
||||
.length=list.length,
|
||||
};
|
||||
}
|
||||
#define CloneList(arena, type, list) (type)cloneList((arena), (VoidList)(list), (MemberSizeUnderlying(list)))
|
||||
#define ListAppendList(appendee, list)\
|
||||
if ((appendee).capacity - (appendee).length >= list.length) {\
|
||||
memcpy((appendee).data + (appendee).length, (list).data, MemberSizeUnderlying(list)*(list).length);\
|
||||
(appendee).length += (list).length;\
|
||||
}
|
||||
|
||||
|
||||
// Following two macros do not use pointers due to copying
|
||||
#define ListSlice(list, start, stop) {\
|
||||
.data= (stop > (list).length || start > stop) ? 0 : (list).data + start,\
|
||||
.length=(stop > (list).length || start > stop) ? 0 : stop - start,\
|
||||
.capacity=(stop > (list).length || start > stop) ? 0 : stop - start,\
|
||||
}
|
||||
.data= (stop > (list).length || start > stop) ? 0 : (list).data + start,\}
|
||||
#define ListTail(list, start) ListSlice(list, start, (list).length)
|
||||
|
||||
#define ListRemove(list, index)\
|
||||
@@ -196,6 +213,7 @@ struct string {
|
||||
#define s(lit) ((string){(char *)(lit), sizeof(lit) - 1})
|
||||
#define PushString(arena, length) ((string){ (char *)pushSize(arena, length), (length) })
|
||||
#define PushStringFill(arena, length, characterByte) ((string){ (char *)pushSizeFill(arena, length, characterByte), (length) })
|
||||
#define StrFromList(list) ((string){.str=(list).data,.length=(list).length})
|
||||
|
||||
// C Strings
|
||||
DefineList(char, Char);
|
||||
@@ -206,7 +224,8 @@ string strFromCString(Arena *arena, const char *str);
|
||||
|
||||
bool strEql(string s1, string s2);
|
||||
bool strStartsWith(string str, string testStr);
|
||||
bool stringContains(string str, char c);
|
||||
bool strContainChar(string str, char c);
|
||||
bool strContains(string a, string b);
|
||||
|
||||
string strReverse(Arena *arena, string str);
|
||||
string strSlice(string str, size_t start, size_t stop);
|
||||
@@ -215,6 +234,8 @@ StringList strSplit(Arena *arena, string splitStr, string inputStr);
|
||||
string strPrintfv(Arena *arena, const char *fmt, va_list args);
|
||||
string strPrintf(Arena *arena, const char *fmt, ...);
|
||||
|
||||
string strListJoin(Arena *arena, StringList list);
|
||||
|
||||
#define DefineResult(type, prefix) \
|
||||
typedef struct prefix ## Result prefix ## Result;\
|
||||
struct prefix ## Result {\
|
||||
@@ -303,8 +324,10 @@ typedef enum {
|
||||
#define COLOR_TEXT_RGB(text, red, green, blue) ANSI_INSTRUCTION_STR("38;2;" #red ";" #green ";" #blue) text ANSI_RESET
|
||||
|
||||
DefineList(int, Int);
|
||||
|
||||
void printIntList(IntList l);
|
||||
void printStrList(StringList l);
|
||||
void printStr(string str);
|
||||
void setStdout();
|
||||
void setStderr();
|
||||
extern void (*print)(const char *fmt, ...);
|
||||
|
||||
Reference in New Issue
Block a user