diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-03-21 21:48:53 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-03-21 21:48:53 -0400 |
| commit | 5ee185a4896e43c67b6d299becfa616da78fb9f4 (patch) | |
| tree | 183ceef2fd21230c89334d7d039255d1c86c5dca /stdlib/bytes.c | |
| parent | f4aaf7b73481248f6768302be688700a364a1af8 (diff) | |
Move stdlib into src/
Diffstat (limited to 'stdlib/bytes.c')
| -rw-r--r-- | stdlib/bytes.c | 64 |
1 files changed, 0 insertions, 64 deletions
diff --git a/stdlib/bytes.c b/stdlib/bytes.c deleted file mode 100644 index b24a721b..00000000 --- a/stdlib/bytes.c +++ /dev/null @@ -1,64 +0,0 @@ -// The logic for unsigned bytes -#include <stdbool.h> -#include <stdint.h> - -#include "bytes.h" -#include "stdlib.h" -#include "text.h" -#include "util.h" - -public const Byte_t Byte$min = 0; -public const Byte_t Byte$max = UINT8_MAX; - -PUREFUNC public Text_t Byte$as_text(const void *b, bool colorize, const TypeInfo_t*) -{ - if (!b) return Text("Byte"); - return Text$format(colorize ? "\x1b[35m0x%02X\x1b[m" : "0x%02X", *(Byte_t*)b); -} - -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); - if (prefix && uppercase) - text.length = (int64_t)snprintf((char*)text.ascii, 8, "0x%02X", byte); - else if (prefix && !uppercase) - text.length = (int64_t)snprintf((char*)text.ascii, 8, "0x%02x", byte); - else if (!prefix && uppercase) - text.length = (int64_t)snprintf((char*)text.ascii, 8, "%02X", byte); - else if (!prefix && !uppercase) - text.length = (int64_t)snprintf((char*)text.ascii, 8, "%02x", byte); - return text; -} - -public PUREFUNC Byte_t Byte$from_int(Int_t i, bool truncate) { - if unlikely (truncate && Int$compare_value(i, I_small(0xFF)) > 0) - fail("This value is too large to convert to a byte without truncation: %k", (Text_t[1]){Int$value_as_text(i)}); - else if unlikely (truncate && Int$compare_value(i, I_small(0)) < 0) - fail("Negative values can't be converted to bytes: %k", (Text_t[1]){Int$value_as_text(i)}); - return (i.small != 0); -} -public PUREFUNC Byte_t Byte$from_int64(Int64_t i, bool truncate) { - if unlikely (truncate && i != (Int64_t)(Byte_t)i) - fail("This value can't be converted to a byte without truncation: %ld", i); - return (Byte_t)i; -} -public PUREFUNC Byte_t Byte$from_int32(Int32_t i, bool truncate) { - if unlikely (truncate && i != (Int32_t)(Byte_t)i) - fail("This value can't be converted to a byte without truncation: %d", i); - return (Byte_t)i; -} -public PUREFUNC Byte_t Byte$from_int16(Int16_t i, bool truncate) { - if unlikely (truncate && i != (Int16_t)(Byte_t)i) - fail("This value can't be converted to a byte without truncation: %d", i); - return (Byte_t)i; -} - -public const TypeInfo_t Byte$info = { - .size=sizeof(Byte_t), - .align=__alignof__(Byte_t), - .metamethods={ - .as_text=Byte$as_text, - }, -}; - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 |
