diff --git a/includes/std/types.pat b/includes/std/types.pat deleted file mode 100644 index 6cc177a..0000000 --- a/includes/std/types.pat +++ /dev/null @@ -1,100 +0,0 @@ -#include -#include - -namespace std::type { - - struct GUID { - u32 time_low; - u16 time_mid; - u16 time_high_and_version; - u8 clock_seq_and_reserved; - u8 clock_seq_low; - u8 node[6]; - } [[format("std::type::guid_formatter"), single_color]]; - - fn guid_formatter(GUID guid) { - bool valid = ((le u16(guid.time_high_and_version) >> 12) <= 5) && (((guid.clock_seq_and_reserved >> 4) >= 8) || ((guid.clock_seq_and_reserved >> 4) == 0)); - - return std::format("{}{{{:08X}-{:04X}-{:04X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}}}", - valid ? "" : "Invalid ", - le u32(guid.time_low), - le u16(guid.time_mid), - le u16(guid.time_high_and_version), - guid.clock_seq_and_reserved, - guid.clock_seq_low, - guid.node[0], - guid.node[1], - guid.node[2], - guid.node[3], - guid.node[4], - guid.node[5]); - }; - - - using float16 = u16 [[format("std::type::float16_formatter")]]; - - fn float16_formatter(float16 value) { - float sign = (value >> 15) == 0b01 ? -1.0 : 1.0; - float exponent = std::math::pow(2.0, float((value >> 10) & 0x1F) - 14); - u16 mantissa = value & 0x3FF; - - float fraction = 0; - for (s8 i = 9, i >= 0, i -= 1) { - if ((mantissa & (1 << i)) != 0) { - fraction += 1.0 / std::math::pow(2.0, (10.0 - i)); - } - } - - - return std::format("{:f}", sign * exponent * fraction); - - }; - - - bitfield Bits { - bit0 : 1; - bit1 : 1; - bit2 : 1; - bit3 : 1; - bit4 : 1; - bit5 : 1; - bit6 : 1; - bit7 : 1; - } [[format("std::type::bits_formatter"), right_to_left]]; - - bitfield Nibbles { - low : 4; - high : 4; - } [[format("std::type::nibble_formatter")]]; - - union Byte { - u8 value; - Bits bits; - Nibbles nibbles; - } [[format("std::type::byte_formatter"), single_color]]; - - fn byte_formatter(Byte byte) { - return std::format("0x{0:02X} (0b{1:08b}) LSB:{2}, MSB:{3}", - byte.value, - byte.value, - byte.bits.bit0, - byte.bits.bit7); - }; - - fn bits_formatter(Bits bits) { - return std::format("0b{}{}{}{}{}{}{}{}", - bits.bit7, - bits.bit6, - bits.bit5, - bits.bit4, - bits.bit6, - bits.bit2, - bits.bit1, - bits.bit0); - }; - - fn nibble_formatter(Nibbles nibbles) { - return std::format("{{ {0:0X}, {1:0X} }}", nibbles.high, nibbles.low); - }; - -} diff --git a/includes/type/byte.pat b/includes/type/byte.pat new file mode 100644 index 0000000..9f8597e --- /dev/null +++ b/includes/type/byte.pat @@ -0,0 +1,56 @@ +#include + +namespace type { + + bitfield Bits { + bit0 : 1; + bit1 : 1; + bit2 : 1; + bit3 : 1; + bit4 : 1; + bit5 : 1; + bit6 : 1; + bit7 : 1; + } [[format("type::impl::format_bits"), right_to_left]]; + + bitfield Nibbles { + low : 4; + high : 4; + } [[format("type::impl::format_nibbles")]]; + + union Byte { + u8 value; + Bits bits; + Nibbles nibbles; + } [[format("type::impl::format_byte"), single_color]]; + + + namespace impl { + + fn format_byte(Byte byte) { + return std::format("0x{0:02X} (0b{1:08b}) LSB:{2}, MSB:{3}", + byte.value, + byte.value, + byte.bits.bit0, + byte.bits.bit7); + }; + + fn format_bits(Bits bits) { + return std::format("0b{}{}{}{}{}{}{}{}", + bits.bit7, + bits.bit6, + bits.bit5, + bits.bit4, + bits.bit6, + bits.bit2, + bits.bit1, + bits.bit0); + }; + + fn format_nibbles(Nibbles nibbles) { + return std::format("{{ {0:0X}, {1:0X} }}", nibbles.high, nibbles.low); + }; + + } + +} \ No newline at end of file diff --git a/includes/type/float16.pat b/includes/type/float16.pat new file mode 100644 index 0000000..0de0b62 --- /dev/null +++ b/includes/type/float16.pat @@ -0,0 +1,27 @@ +#include +#include + +namespace type { + + using float16 = u16 [[format("type::impl::format_float16")]]; + + namespace impl { + + fn format_float16(float16 value) { + float sign = (value >> 15) == 0b01 ? -1.0 : 1.0; + float exponent = std::math::pow(2.0, float((value >> 10) & 0x1F) - 14); + u16 mantissa = value & 0x3FF; + + float fraction = 0; + for (s8 i = 9, i >= 0, i -= 1) { + if ((mantissa & (1 << i)) != 0) { + fraction += 1.0 / std::math::pow(2.0, (10.0 - i)); + } + } + + return std::format("{:f}", sign * exponent * fraction); + }; + + } + +} \ No newline at end of file diff --git a/includes/type/guid.pat b/includes/type/guid.pat new file mode 100644 index 0000000..912adb6 --- /dev/null +++ b/includes/type/guid.pat @@ -0,0 +1,36 @@ +#include + +namespace type { + + struct GUID { + u32 time_low; + u16 time_mid; + u16 time_high_and_version; + u8 clock_seq_and_reserved; + u8 clock_seq_low; + u8 node[6]; + } [[sealed, format("type::impl::format_guid")]]; + + namespace impl { + + fn format_guid(GUID guid) { + bool valid = ((le u16(guid.time_high_and_version) >> 12) <= 5) && (((guid.clock_seq_and_reserved >> 4) >= 8) || ((guid.clock_seq_and_reserved >> 4) == 0)); + + return std::format("{}{{{:08X}-{:04X}-{:04X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}}}", + valid ? "" : "Invalid ", + le u32(guid.time_low), + le u16(guid.time_mid), + le u16(guid.time_high_and_version), + guid.clock_seq_and_reserved, + guid.clock_seq_low, + guid.node[0], + guid.node[1], + guid.node[2], + guid.node[3], + guid.node[4], + guid.node[5]); + }; + + } + +} \ No newline at end of file diff --git a/includes/type/ip.pat b/includes/type/ip.pat new file mode 100644 index 0000000..2ce9212 --- /dev/null +++ b/includes/type/ip.pat @@ -0,0 +1,37 @@ +#include + +namespace type { + + struct IPv4Address { + u8 bytes[4]; + } [[sealed, format("type::impl::format_ipv4_address")]]; + + struct IPv6Address { + u16 words[8]; + } [[sealed, format("type::impl::format_ipv6_address")]]; + + namespace impl { + + fn format_ipv4_address(IPv4Address address) { + return std::format("{}.{}.{}.{}", + address.bytes[0], + address.bytes[1], + address.bytes[2], + address.bytes[3]); + }; + + fn format_ipv6_address(IPv6Address address) { + return std::format("{:04X}:{:04X}:{:04X}:{:04X}:{:04X}:{:04X}:{:04X}:{:04X}", + address.words[0], + address.words[1], + address.words[2], + address.words[3], + address.words[4], + address.words[5], + address.words[6], + address.words[7]); + }; + + } + +} \ No newline at end of file diff --git a/includes/type/mac.pat b/includes/type/mac.pat new file mode 100644 index 0000000..883e8e7 --- /dev/null +++ b/includes/type/mac.pat @@ -0,0 +1,23 @@ +#include + +namespace type { + + struct MACAddress { + u8 bytes[6]; + } [[sealed, format("type::impl::format_mac_address")]]; + + namespace impl { + + fn format_mac_address(MACAddress address) { + return std::format("{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}", + address.bytes[0], + address.bytes[1], + address.bytes[2], + address.bytes[3], + address.bytes[4], + address.bytes[5]); + }; + + } + +} \ No newline at end of file diff --git a/includes/type/time.pat b/includes/type/time.pat new file mode 100644 index 0000000..c36e1e3 --- /dev/null +++ b/includes/type/time.pat @@ -0,0 +1,16 @@ +#include +#include + +namespace type { + + using time_t = u16 [[format("type::impl::format_time_t")]]; + + namespace impl { + + fn format_time_t(time_t value) { + return std::time::format(std::time::to_utc(value)); + }; + + } + +} \ No newline at end of file