dev
Download
147 lines 5.1 KiBCModified 2026-03-08 raw
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
// The logic for unsigned bytes
#include <stdbool.h>
#include <stdint.h>

#include "bytes.h"
#include "integers.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 *info) {
    (void)info;
    if (!b) return Text("Byte");
    Byte_t byte = *(Byte_t *)b;
    char digits[] = {
        '0',
        'x',
        (byte / 16) <= 9 ? '0' + (byte / 16) : 'a' + (byte / 16) - 10,
        (byte & 15) <= 9 ? '0' + (byte & 15) : 'a' + (byte & 15) - 10,
        '\0',
    };
    Text_t text = Text$from_str(digits);
    if (colorize) text = Text$concat(Text("\x1b[35m"), text, Text("\x1b[m"));
    return text;
}

public
CONSTFUNC bool Byte$is_between(const Byte_t x, const Byte_t low, const Byte_t high) {
    return (low <= x && x <= high) || (high <= x && x <= low);
}

public
OptionalByte_t Byte$parse(Text_t text, OptionalInt_t base, Text_t *remainder) {
    OptionalInt_t full_int = Int$parse(text, base, 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){.has_value = true, .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);
    char *p = (char *)text.ascii;
    if (prefix) {
        *(p++) = '0';
        *(p++) = 'x';
    }

    if (uppercase) {
        *(p++) = (byte / 16) > 9 ? 'A' + (byte / 16) - 10 : '0' + (byte / 16);
        *(p++) = (byte & 15) > 9 ? 'A' + (byte & 15) - 10 : '0' + (byte & 15);
    } else {
        *(p++) = (byte / 16) > 9 ? 'a' + (byte / 16) - 10 : '0' + (byte / 16);
        *(p++) = (byte & 15) > 9 ? 'a' + (byte & 15) - 10 : '0' + (byte & 15);
    }
    text.length = (int64_t)(p - text.ascii);
    return text;
}

public
bool Byte$get_bit(Byte_t x, Int_t bit_index) {
    if (Int$compare_value(bit_index, I(1)) < 0) fail("Invalid bit index (expected 1 or higher): ", bit_index);
    if (Int$compare_value(bit_index, I(8)) > 0)
        fail("Bit index is too large! There are only 8 bits in a byte, but index "
             "is: ",
             bit_index);
    return ((x & (Byte_t)(1L << (Int64$from_int(bit_index, true) - 1L))) != 0);
}

#ifdef __TINYC__
#define __builtin_add_overflow(x, y, result)                                                                           \
    ({                                                                                                                 \
        *(result) = (x) + (y);                                                                                         \
        false;                                                                                                         \
    })
#endif

typedef struct {
    OptionalByte_t current, last;
    Int8_t step;
} ByteRange_t;

static OptionalByte_t _next_Byte(ByteRange_t *info) {
    OptionalByte_t i = info->current;
    if (i.has_value) {
        Byte_t next;
        bool overflow = __builtin_add_overflow(i.value, info->step, &next);
        if (overflow || (info->last.has_value && (info->step >= 0 ? next > info->last.value : next < info->last.value)))
            info->current = (OptionalByte_t){.has_value = false};
        else info->current = (OptionalByte_t){.has_value = true, .value = next};
    }
    return i;
}

public
Closure_t Byte$to(Byte_t first, Byte_t last, OptionalInt8_t step) {
    ByteRange_t *range = GC_MALLOC(sizeof(ByteRange_t));
    range->current = (OptionalByte_t){.has_value = true, .value = first};
    range->last = (OptionalByte_t){.has_value = true, .value = last};
    range->step = step.has_value ? step.value : (last >= first ? 1 : -1);
    return (Closure_t){.fn = _next_Byte, .userdata = range};
}

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: ", i);
    else if unlikely (!truncate && Int$compare_value(i, I_small(0)) < 0)
        fail("Negative values can't be converted to bytes: ", i);
    return (Byte_t)(i.small >> 2);
}
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: ", 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: ", 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: ", 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,
        },
};