|
@@ -42,10 +42,11 @@ struct Arena { |
|
|
size_t head; |
|
|
size_t head; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
void *pushSize(Arena *arena, size_t size) { |
|
|
|
|
|
if (arena->capacity - arena->head >= size) { |
|
|
|
|
|
arena->head += size; |
|
|
|
|
|
return (char *)arena->memory + arena->head + size; |
|
|
|
|
|
|
|
|
void *pushSize(Arena *arena, size_t bytes) { |
|
|
|
|
|
if (arena->capacity - arena->head >= bytes) { |
|
|
|
|
|
void *ptr = (char *)arena->memory + arena->head; |
|
|
|
|
|
arena->head += bytes; |
|
|
|
|
|
return ptr; |
|
|
} |
|
|
} |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
@@ -88,6 +89,9 @@ void zeroList(list<T> *list) { |
|
|
memset(list->data, 0, list->capacity * sizeof(T)); |
|
|
memset(list->data, 0, list->capacity * sizeof(T)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#define foreach(it, list, type) for (size_t __i = 0; __i < list.length; __i++) { type it = list.data[__i]; |
|
|
|
|
|
#define foreach_end() } |
|
|
|
|
|
|
|
|
// ### Strings ### |
|
|
// ### Strings ### |
|
|
#define strlit(lit) ((string){(char *)(lit), sizeof(lit) - 1}) |
|
|
#define strlit(lit) ((string){(char *)(lit), sizeof(lit) - 1}) |
|
|
|
|
|
|
|
@@ -96,16 +100,18 @@ struct string { |
|
|
size_t len; |
|
|
size_t len; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
#define PushString(arena, length) ((string){ (char *)pushSize(arena, length), (length) }) |
|
|
|
|
|
|
|
|
const char *cstring(Arena *arena, list<char> buf) { |
|
|
const char *cstring(Arena *arena, list<char> buf) { |
|
|
char *arr = PushArray(arena, char, buf.length + 1); |
|
|
char *arr = PushArray(arena, char, buf.length + 1); |
|
|
memcpy(arr, buf.data, sizeof(char) * buf.length); |
|
|
|
|
|
|
|
|
memmove(arr, buf.data, buf.length); |
|
|
arr[buf.length] = '\0'; |
|
|
arr[buf.length] = '\0'; |
|
|
return arr; |
|
|
return arr; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const char *cstring(Arena *arena, string str) { |
|
|
const char *cstring(Arena *arena, string str) { |
|
|
char *arr = PushArray(arena, char, str.len + 1); |
|
|
char *arr = PushArray(arena, char, str.len + 1); |
|
|
memcpy(arr, str.str, str.len * sizeof(char)); |
|
|
|
|
|
|
|
|
memmove(arr, str.str, str.len); |
|
|
arr[str.len] = '\0'; |
|
|
arr[str.len] = '\0'; |
|
|
return arr; |
|
|
return arr; |
|
|
} |
|
|
} |
|
@@ -122,6 +128,18 @@ bool strEql(string s1, string s2) { |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
string strReverse(Arena *arena, string str) { |
|
|
|
|
|
string reversed = PushString(arena, str.len); |
|
|
|
|
|
for ( |
|
|
|
|
|
int mainIndex = str.len - 1, reversedIndex = 0; |
|
|
|
|
|
mainIndex < str.len; |
|
|
|
|
|
mainIndex--, reversedIndex++ |
|
|
|
|
|
) { |
|
|
|
|
|
reversed.str[reversedIndex] = str.str[mainIndex]; |
|
|
|
|
|
} |
|
|
|
|
|
return reversed; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
string strSlice(char *data, size_t start, size_t stop) { |
|
|
string strSlice(char *data, size_t start, size_t stop) { |
|
|
return { |
|
|
return { |
|
|
data + start, |
|
|
data + start, |
|
|