From 28180886ae91dbadff5bf52615bd20e8d9ba713c Mon Sep 17 00:00:00 2001 From: Tenari Date: Sat, 11 Apr 2026 10:46:13 -0500 Subject: [PATCH] stringChunkListsEq() + fix some i32 compares with u32 --- all.h | 1 + string.c | 4 ++-- string_chunk.c | 17 +++++++++++++++++ unix_os.c | 10 +++++----- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/all.h b/all.h index 267d8f9..7e06230 100644 --- a/all.h +++ b/all.h @@ -707,6 +707,7 @@ fn void stringChunkListAppend(StringArena* a, StringChunkList* list, String stri fn void stringChunkListDeleteLast(StringArena* a, StringChunkList* list); fn StringChunkList stringChunkListInit(StringArena* a); fn void stringChunkCopyToBuffer(StringChunkList* list, u8* buffer, u32 len); +fn bool stringChunkListsEq(StringChunkList* a, StringChunkList* b); ///// BINARY SERIALIZATION NONSENSE fn u64 writeU64ToBufferLE(u8* buffer, u64 value); diff --git a/string.c b/string.c index d475b7e..a724458 100644 --- a/string.c +++ b/string.c @@ -4,7 +4,7 @@ fn bool stringsEq(String* a, String* b) { if (a->length != b->length) { return false; } - for (i32 i = 0; i < a->length; i++) { + for (u32 i = 0; i < a->length; i++) { if (a->bytes[i] != b->bytes[i]) { return false; } @@ -16,7 +16,7 @@ fn bool cStringEqString(str a, String* b) { if (strlen(a) != b->length) { return false; } - for (i32 i = 0; i < b->length; i++) { + for (u32 i = 0; i < b->length; i++) { if (a[i] != b->bytes[i]) { return false; } diff --git a/string_chunk.c b/string_chunk.c index de25a55..dae1a08 100644 --- a/string_chunk.c +++ b/string_chunk.c @@ -160,3 +160,20 @@ fn void stringChunkCopyToBuffer(StringChunkList* list, u8* buffer, u32 len) { buffer[i] = *((char*)(chunk + 1) + (i%STRING_CHUNK_PAYLOAD_SIZE)); } } + +fn bool stringChunkListsEq(StringChunkList* a, StringChunkList* b) { + if (a->total_size != b->total_size) return false; + StringChunk* chunk_a = a->first; + StringChunk* chunk_b = b->first; + for (u32 i = 0; i < a->total_size; i++) { + if (i > 0 && i % STRING_CHUNK_PAYLOAD_SIZE == 0) { + chunk_a = chunk_a->next; + chunk_b = chunk_b->next; + } + u8 a_char = *((char*)(chunk_a + 1) + (i%STRING_CHUNK_PAYLOAD_SIZE)); + u8 b_char = *((char*)(chunk_b + 1) + (i%STRING_CHUNK_PAYLOAD_SIZE)); + if (a_char != b_char) return false; + } + return true; +} + diff --git a/unix_os.c b/unix_os.c index 458df0a..fd52209 100644 --- a/unix_os.c +++ b/unix_os.c @@ -191,7 +191,7 @@ fn bool osFileCreate(String filename) { return true; */ bool result = true; - size_t handle = open((str)filename.bytes, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + i32 handle = open((str)filename.bytes, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (handle == -1) { result = false; } @@ -215,7 +215,7 @@ fn bool osFileCreateWrite(String filename, String data) { return result; */ bool result = true; - size_t handle = open( + i32 handle = open( (str)filename.bytes, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH @@ -238,7 +238,7 @@ fn bool osFileWrite(String filename, String data) { close(handle); */ bool result = true; - size_t handle = open((str) filename.bytes, O_RDWR | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH); + i32 handle = open((str) filename.bytes, O_RDWR | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH); if (handle == -1) result = false; write(handle, data.bytes, data.length); close(handle); @@ -246,7 +246,7 @@ fn bool osFileWrite(String filename, String data) { } fn Resulti64 osFileOpenForWriting(String filename) { - size_t handle = open((str)filename.bytes, O_WRONLY | O_APPEND, S_IRUSR | S_IRGRP | S_IROTH); + i32 handle = open((str)filename.bytes, O_WRONLY | O_APPEND, S_IRUSR | S_IRGRP | S_IROTH); Resulti64 result = { .success = handle != -1, .value = (i64)handle, @@ -266,7 +266,7 @@ fn Resulti64 osFileClose(Resulti64 handle) { fn bool osFileWriteOpenFile(Resulti64 handle, String data) { assert(handle.success == true); i32 wrote_this_round = 0; - for (i32 bytes_written = 0; bytes_written < data.length; bytes_written += wrote_this_round) { + for (u32 bytes_written = 0; bytes_written < data.length; bytes_written += wrote_this_round) { wrote_this_round = write((size_t)handle.value, data.bytes + bytes_written, data.length - bytes_written); if (wrote_this_round == -1) return false; }