diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-15 15:33:47 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-15 15:33:47 -0400 |
| commit | e422079fcced744e3a6247aeb12a09a658989072 (patch) | |
| tree | 393d5e52ba67dcc822ccfa9a812198edda5e735d /stdlib/bytes.c | |
| parent | 259c7efcf8c3808d8151d8e15f1167ad2b6f2ca7 (diff) | |
Add a Byte datatype
Diffstat (limited to 'stdlib/bytes.c')
| -rw-r--r-- | stdlib/bytes.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/stdlib/bytes.c b/stdlib/bytes.c new file mode 100644 index 00000000..5b471bfe --- /dev/null +++ b/stdlib/bytes.c @@ -0,0 +1,38 @@ +// 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 Byte_t *b, bool colorize, const TypeInfo *type) +{ + (void)type; + if (!b) return Text("Byte"); + return Text$format(colorize ? "\x1b[35m%u[B]\x1b[m" : "%u[B]", *b); +} + +public Byte_t Byte$random(Byte_t min, Byte_t max) +{ + if (min > max) + fail("Random minimum value (%u) is larger than the maximum value (%u)", min, max); + if (min == max) + return min; + + uint32_t r = arc4random_uniform((uint32_t)max - (uint32_t)min + 1u); + return (Byte_t)(min + r); +} + +public const TypeInfo Byte$info = { + .size=sizeof(Byte_t), + .align=__alignof__(Byte_t), + .tag=CustomInfo, + .CustomInfo={.as_text=(void*)Byte$as_text}, +}; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 |
