From 8e70a5524d399876c8eb40fce0f47e092fd9c00e Mon Sep 17 00:00:00 2001 From: Nik Date: Mon, 10 Oct 2022 22:36:46 +0200 Subject: [PATCH] includes/type: Added templates for number types with specific base --- includes/type/base.pat | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 includes/type/base.pat diff --git a/includes/type/base.pat b/includes/type/base.pat new file mode 100644 index 0000000..56a9e22 --- /dev/null +++ b/includes/type/base.pat @@ -0,0 +1,31 @@ +#pragma once + +#include +#include + +namespace type { + + using Hex = T [[format("type::impl::format_hex")]]; + using Oct = T [[format("type::impl::format_oct")]]; + using Dec = T [[format("type::impl::format_dec")]]; + using Bin = T [[format("type::impl::format_bin")]]; + + namespace impl { + + fn format_number(auto value, str fmt) { + bool negative = value < 0; + + if (negative) + return std::format("-" + fmt, std::math::abs(value)); + else + return std::format(fmt, value); + }; + + fn format_hex(auto value) { return format_number(value, "0x{:02X}"); }; + fn format_oct(auto value) { return format_number(value, "0o{:03o}"); }; + fn format_dec(auto value) { return format_number(value, "{}"); }; + fn format_bin(auto value) { return format_number(value, "0b{:08b}"); }; + + } + +} \ No newline at end of file