From a422f9aead499a526179ba2df2aff1aa44fe48d6 Mon Sep 17 00:00:00 2001 From: Mel Date: Sat, 31 May 2025 03:29:51 +0200 Subject: Keyword-less variable declaration parsing Signed-off-by: Mel --- boot/common.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'boot/common.c') diff --git a/boot/common.c b/boot/common.c index d814538..65dc780 100644 --- a/boot/common.c +++ b/boot/common.c @@ -166,6 +166,47 @@ string_print(struct String s) printf("%.*s", (int32)s.length, s.data); } +#define STRING_ARRAY_MAX 8 + +// a string array, used for storing multiple strings. +// if we ever need more strings, just bump `STRING_ARRAY_MAX` up. +struct String_Array +{ + struct String strings[STRING_ARRAY_MAX]; + uint count; +}; + +// initializes a string array with no strings. +struct String_Array +string_array_new() +{ + return (struct String_Array){ + .strings = { 0 }, + .count = 0, + }; +} + +// adds a string to the string array. +bool +string_array_add(struct String_Array* array, struct String string) +{ + if (array->count >= STRING_ARRAY_MAX) return false; + + array->strings[array->count++] = string; + return true; +} + +struct String +string_array_at(const struct String_Array* array, uint index) +{ + check(index < array->count, "index out of bounds"); + return array->strings[index]; +} + +#define STRING_ARRAY_FOR_EACH(cursor, str, array) \ + struct String str = array.strings[0]; \ + for (uint cursor = 0; cursor < array.count; str = array.strings[++cursor]) + // single iteration of the CRC32 checksum algorithm // described in POSIX. // see: https://pubs.opengroup.org/onlinepubs/9799919799/utilities/cksum.html -- cgit 1.4.1