2024-06-28 12:07:02 +02:00
|
|
|
|
2024-06-28 18:50:30 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void* talloc(u64);
|
2024-06-28 12:07:02 +02:00
|
|
|
|
|
|
|
typedef struct string {
|
|
|
|
u64 count;
|
2024-06-28 18:50:30 +02:00
|
|
|
u8 *data;
|
2024-06-28 12:07:02 +02:00
|
|
|
} string;
|
|
|
|
|
2024-07-15 21:40:27 +02:00
|
|
|
const string null_string = {0, 0};
|
|
|
|
|
2024-07-04 20:56:27 +02:00
|
|
|
#define fixed_string STR
|
2024-07-02 15:27:33 +02:00
|
|
|
#define STR(s) ((string){ length_of_null_terminated_string((const char*)s), (u8*)s })
|
2024-06-28 12:07:02 +02:00
|
|
|
|
|
|
|
inline u64 length_of_null_terminated_string(const char* cstring) {
|
|
|
|
u64 len = 0;
|
|
|
|
while (*cstring != 0) {
|
|
|
|
len += 1;
|
|
|
|
cstring += 1;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2024-07-01 13:10:06 +02:00
|
|
|
string alloc_string(Allocator allocator, u64 count) {
|
2024-06-28 18:50:30 +02:00
|
|
|
string s;
|
|
|
|
s.count = count;
|
2024-07-01 13:10:06 +02:00
|
|
|
s.data = cast(u8*)alloc(allocator, count);
|
2024-06-28 18:50:30 +02:00
|
|
|
return s;
|
|
|
|
}
|
2024-07-01 13:10:06 +02:00
|
|
|
void dealloc_string(Allocator allocator, string s) {
|
2024-07-15 21:40:27 +02:00
|
|
|
assert(s.count > 0 && s.data, "You tried to deallocate an empty string. That's doesn't make sense.");
|
2024-07-01 13:10:06 +02:00
|
|
|
dealloc(allocator, s.data);
|
2024-06-28 18:50:30 +02:00
|
|
|
}
|
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
|
|
|
string talloc_string(u64 count) {
|
2024-07-01 13:10:06 +02:00
|
|
|
string s = alloc_string(temp, count);
|
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
|
|
|
return s;
|
|
|
|
}
|
2024-06-28 18:50:30 +02:00
|
|
|
|
2024-07-01 13:10:06 +02:00
|
|
|
string string_concat(const string left, const string right, Allocator allocator) {
|
2024-07-15 21:40:27 +02:00
|
|
|
|
|
|
|
if (right.count + left.count == 0) return null_string;
|
|
|
|
if (left.count == 0) return right;
|
|
|
|
if (right.count == 0) return left;
|
|
|
|
|
2024-06-28 12:07:02 +02:00
|
|
|
string result;
|
|
|
|
result.count = left.count + right.count;
|
2024-07-01 13:10:06 +02:00
|
|
|
result.data = cast(u8*)alloc(allocator, result.count);
|
2024-06-28 12:07:02 +02:00
|
|
|
memcpy(result.data, left.data, left.count);
|
|
|
|
memcpy(result.data+left.count, right.data, right.count);
|
|
|
|
return result;
|
|
|
|
}
|
2024-07-01 13:10:06 +02:00
|
|
|
char *convert_to_null_terminated_string(const string s, Allocator allocator) {
|
|
|
|
char *cstring = cast(char*)alloc(allocator, s.count+1);
|
2024-06-28 12:07:02 +02:00
|
|
|
memcpy(cstring, s.data, s.count);
|
|
|
|
cstring[s.count] = 0;
|
|
|
|
return cstring;
|
|
|
|
}
|
|
|
|
|
2024-06-28 18:50:30 +02:00
|
|
|
char *temp_convert_to_null_terminated_string(const string s) {
|
2024-07-01 13:10:06 +02:00
|
|
|
char *c = convert_to_null_terminated_string(s, temp);
|
2024-06-28 12:07:02 +02:00
|
|
|
return c;
|
2024-06-28 18:50:30 +02:00
|
|
|
}
|
|
|
|
bool strings_match(string a, string b) {
|
|
|
|
if (a.count != b.count) return false;
|
|
|
|
|
|
|
|
// Count match, pointer match: they are the same
|
|
|
|
if (a.data == b.data) return true;
|
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
|
|
|
|
2024-06-28 18:50:30 +02:00
|
|
|
return memcmp(a.data, b.data, a.count) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
string string_view(string s, u64 start_index, u64 count) {
|
|
|
|
assert(start_index < s.count, "array_view start_index % out of range for string count %", start_index, s.count);
|
|
|
|
assert(count > 0, "array_view count must be more than 0");
|
|
|
|
assert(start_index + count <= s.count, "array_view start_index + count is out of range");
|
|
|
|
|
|
|
|
string result;
|
|
|
|
result.data = s.data+start_index;
|
|
|
|
result.count = count;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-07-01 02:14:08 +02:00
|
|
|
// Returns first index from left where "sub" matches in "s". Returns -1 if no match is found.
|
|
|
|
s64 string_find_from_left(string s, string sub) {
|
|
|
|
for (s64 i = 0; i <= s.count-sub.count; i++) {
|
|
|
|
if (strings_match(string_view(s, i, sub.count), sub)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns first index from right where "sub" matches in "s" Returns -1 if no match is found.
|
|
|
|
s64 string_find_from_right(string s, string sub) {
|
|
|
|
for (s64 i = s.count-sub.count; i >= 0 ; i--) {
|
|
|
|
if (strings_match(string_view(s, i, sub.count), sub)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2024-07-02 15:27:33 +02:00
|
|
|
}
|
|
|
|
|
2024-07-08 15:33:01 +02:00
|
|
|
bool string_starts_with(string s, string sub) {
|
|
|
|
if (s.count < sub.count) return false;
|
|
|
|
|
|
|
|
s.count = sub.count;
|
|
|
|
|
|
|
|
return strings_match(s, sub);
|
|
|
|
}
|