1 // The logic for unsigned bytes
12 const Byte_t Byte$min = 0;
14 const Byte_t Byte$max = UINT8_MAX;
16 PUREFUNC public Text_t Byte$as_text(const void *b, bool colorize, const TypeInfo_t *info) {
18 if (!b) return Text("Byte");
19 Byte_t byte = *(Byte_t *)b;
23 (byte / 16) <= 9 ? '0' + (byte / 16) : 'a' + (byte / 16) - 10,
24 (byte & 15) <= 9 ? '0' + (byte & 15) : 'a' + (byte & 15) - 10,
27 Text_t text = Text$from_str(digits);
28 if (colorize) text = Text$concat(Text("\x1b[35m"), text, Text("\x1b[m"));
33 CONSTFUNC bool Byte$is_between(const Byte_t x, const Byte_t low, const Byte_t high) {
34 return (low <= x && x <= high) || (high <= x && x <= low);
38 OptionalByte_t Byte$parse(Text_t text, OptionalInt_t base, Text_t *remainder) {
39 OptionalInt_t full_int = Int$parse(text, base, remainder);
40 if (full_int.small != 0L && Int$compare_value(full_int, I(0)) >= 0 && Int$compare_value(full_int, I(255)) <= 0) {
41 return (OptionalByte_t){.has_value = true, .value = Byte$from_int(full_int, true)};
48 Text_t Byte$hex(Byte_t byte, bool uppercase, bool prefix) {
49 struct Text_s text = {.tag = TEXT_ASCII};
50 text.ascii = GC_MALLOC_ATOMIC(8);
51 char *p = (char *)text.ascii;
58 *(p++) = (byte / 16) > 9 ? 'A' + (byte / 16) - 10 : '0' + (byte / 16);
59 *(p++) = (byte & 15) > 9 ? 'A' + (byte & 15) - 10 : '0' + (byte & 15);
61 *(p++) = (byte / 16) > 9 ? 'a' + (byte / 16) - 10 : '0' + (byte / 16);
62 *(p++) = (byte & 15) > 9 ? 'a' + (byte & 15) - 10 : '0' + (byte & 15);
64 text.length = (int64_t)(p - text.ascii);
69 bool Byte$get_bit(Byte_t x, Int_t bit_index) {
70 if (Int$compare_value(bit_index, I(1)) < 0) fail("Invalid bit index (expected 1 or higher): ", bit_index);
71 if (Int$compare_value(bit_index, I(8)) > 0)
72 fail("Bit index is too large! There are only 8 bits in a byte, but index "
75 return ((x & (Byte_t)(1L << (Int64$from_int(bit_index, true) - 1L))) != 0);
79 #define __builtin_add_overflow(x, y, result) \
81 *(result) = (x) + (y); \
87 OptionalByte_t current, last;
91 static OptionalByte_t _next_Byte(ByteRange_t *info) {
92 OptionalByte_t i = info->current;
95 bool overflow = __builtin_add_overflow(i.value, info->step, &next);
96 if (overflow || (info->last.has_value && (info->step >= 0 ? next > info->last.value : next < info->last.value)))
97 info->current = (OptionalByte_t){.has_value = false};
98 else info->current = (OptionalByte_t){.has_value = true, .value = next};
104 Closure_t Byte$to(Byte_t first, Byte_t last, OptionalInt8_t step) {
105 ByteRange_t *range = GC_MALLOC(sizeof(ByteRange_t));
106 range->current = (OptionalByte_t){.has_value = true, .value = first};
107 range->last = (OptionalByte_t){.has_value = true, .value = last};
108 range->step = step.has_value ? step.value : (last >= first ? 1 : -1);
109 return (Closure_t){.fn = _next_Byte, .userdata = range};
113 PUREFUNC Byte_t Byte$from_int(Int_t i, bool truncate) {
114 if unlikely (!truncate && Int$compare_value(i, I_small(0xFF)) > 0)
115 fail("This value is too large to convert to a byte without truncation: ", i);
116 else if unlikely (!truncate && Int$compare_value(i, I_small(0)) < 0)
117 fail("Negative values can't be converted to bytes: ", i);
118 return (Byte_t)(i.small >> 2);
121 PUREFUNC Byte_t Byte$from_int64(Int64_t i, bool truncate) {
122 if unlikely (!truncate && i != (Int64_t)(Byte_t)i)
123 fail("This value can't be converted to a byte without truncation: ", i);
127 PUREFUNC Byte_t Byte$from_int32(Int32_t i, bool truncate) {
128 if unlikely (!truncate && i != (Int32_t)(Byte_t)i)
129 fail("This value can't be converted to a byte without truncation: ", i);
133 PUREFUNC Byte_t Byte$from_int16(Int16_t i, bool truncate) {
134 if unlikely (!truncate && i != (Int16_t)(Byte_t)i)
135 fail("This value can't be converted to a byte without truncation: ", i);
140 const TypeInfo_t Byte$info = {
141 .size = sizeof(Byte_t),
142 .align = __alignof__(Byte_t),
145 .as_text = Byte$as_text,