From d2685aabf525dbb8644f4d9b255541126df6fd13 Mon Sep 17 00:00:00 2001 From: Nik Date: Wed, 31 Aug 2022 15:05:10 +0200 Subject: [PATCH] lib/type: Added size type --- includes/type/size.pat | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 includes/type/size.pat diff --git a/includes/type/size.pat b/includes/type/size.pat new file mode 100644 index 0000000..6d3f1ec --- /dev/null +++ b/includes/type/size.pat @@ -0,0 +1,40 @@ +namespace type { + + using Size8 = u8 [[format("type::impl::size_formatter")]]; + using Size16 = u16 [[format("type::impl::size_formatter")]]; + using Size32 = u32 [[format("type::impl::size_formatter")]]; + using Size64 = u64 [[format("type::impl::size_formatter")]]; + using Size128 = u128 [[format("type::impl::size_formatter")]]; + + namespace impl { + + fn size_formatter(u128 size) { + u32 i = 0; + while (size >= 1024 && i <= 6) { + i += 1; + size /= 1024; + } + + str result = std::format("{} ", size); + + if (i == 0 && size == 1) + return result + "Byte"; + else if (i == 0) + return result + "Bytes"; + else if (i == 1) + return result + "kiB"; + else if (i == 2) + return result + "MiB"; + else if (i == 3) + return result + "GiB"; + else if (i == 4) + return result + "TiB"; + else if (i == 5) + return result + "PiB"; + else + return result + "EiB"; + }; + + } + +} \ No newline at end of file