Add Byte.hex()

This commit is contained in:
Bruce Hill 2024-11-05 15:33:08 -05:00
parent b238f1df41
commit a8a35ea688
3 changed files with 17 additions and 1 deletions

View File

@ -101,6 +101,7 @@ env_t *new_compilation_unit(CORD libname)
)},
{"Byte", Type(ByteType), "Byte_t", "Byte$info", TypedArray(ns_entry_t,
{"max", "Byte$max", "Byte"},
{"hex", "Byte$hex", "func(byte:Byte, uppercase=yes, prefix=no -> Text)"},
{"min", "Byte$min", "Byte"},
)},
{"Int", Type(BigIntType), "Int_t", "Int$info", TypedArray(ns_entry_t,
@ -643,7 +644,7 @@ binding_t *get_namespace_binding(env_t *env, ast_t *self, const char *name)
case ArrayType: return NULL;
case TableType: return NULL;
case CStringType: case DateTimeType:
case BoolType: case IntType: case BigIntType: case NumType: {
case BoolType: case IntType: case BigIntType: case NumType: case ByteType: {
binding_t *b = get_binding(env, CORD_to_const_char_star(type_to_cord(cls_type)));
assert(b);
return get_binding(Match(b->type, TypeInfoType)->env, name);

View File

@ -17,6 +17,19 @@ PUREFUNC public Text_t Byte$as_text(const Byte_t *b, bool colorize, const TypeIn
return Text$format(colorize ? "\x1b[36mByte\x1b[m(\x1b[35m0x%02X\x1b[m)" : "Byte(0x%02X)", *b);
}
public Text_t Byte$hex(Byte_t byte, bool uppercase, bool prefix) {
Text_t text = {.tag=TEXT_SHORT_ASCII};
if (prefix && uppercase)
text.length = (int64_t)snprintf(text.short_ascii, sizeof(text.short_ascii), "0x%02X", byte);
else if (prefix && !uppercase)
text.length = (int64_t)snprintf(text.short_ascii, sizeof(text.short_ascii), "0x%02x", byte);
else if (!prefix && uppercase)
text.length = (int64_t)snprintf(text.short_ascii, sizeof(text.short_ascii), "%02X", byte);
else if (!prefix && !uppercase)
text.length = (int64_t)snprintf(text.short_ascii, sizeof(text.short_ascii), "%02x", byte);
return text;
}
public const TypeInfo_t Byte$info = {
.size=sizeof(Byte_t),
.align=__alignof__(Byte_t),

View File

@ -28,6 +28,8 @@ extern const Byte_t Byte$max;
extern const TypeInfo_t Byte$info;
Text_t Byte$hex(Byte_t byte, bool uppercase, bool prefix);
typedef struct {
Byte_t value;
bool is_null:1;