From c72b0406a32ffc3f04324f7b6c321486762fca41 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 16 Aug 2025 17:21:01 -0400 Subject: Improved parsing and prefix/suffix matching using a `remainder` parameter --- src/stdlib/bytes.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/stdlib/bytes.c') diff --git a/src/stdlib/bytes.c b/src/stdlib/bytes.c index 48c8b93b..130a645f 100644 --- a/src/stdlib/bytes.c +++ b/src/stdlib/bytes.c @@ -3,6 +3,7 @@ #include #include "bytes.h" +#include "integers.h" #include "stdlib.h" #include "text.h" #include "util.h" @@ -29,6 +30,18 @@ public CONSTFUNC bool Byte$is_between(const Byte_t x, const Byte_t low, const By return low <= x && x <= high; } +public OptionalByte_t Byte$parse(Text_t text, Text_t *remainder) +{ + OptionalInt_t full_int = Int$parse(text, remainder); + if (full_int.small != 0L + && Int$compare_value(full_int, I(0)) >= 0 + && Int$compare_value(full_int, I(255)) <= 0) { + return (OptionalByte_t){.value=Byte$from_int(full_int, true)}; + } else { + return NONE_BYTE; + } +} + public Text_t Byte$hex(Byte_t byte, bool uppercase, bool prefix) { struct Text_s text = {.tag=TEXT_ASCII}; text.ascii = GC_MALLOC_ATOMIC(8); -- cgit v1.2.3