From aa6c90fa5bbbedaf7bb45cd654c78a00dca4036b Mon Sep 17 00:00:00 2001 From: Oded Shapira <32619328+dondish@users.noreply.github.com> Date: Fri, 5 Aug 2022 14:45:34 +0300 Subject: [PATCH] includes/type: Added LEB128 type (#40) * Implement VarInt type * VarInts are little endian, make result u128 * Rename VarInt to LEB128 * It didn't remove the varint file --- includes/type/leb128.pat | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 includes/type/leb128.pat diff --git a/includes/type/leb128.pat b/includes/type/leb128.pat new file mode 100644 index 0000000..e9b7603 --- /dev/null +++ b/includes/type/leb128.pat @@ -0,0 +1,33 @@ +#pragma once + +#include +#include + +namespace type { + + struct LEB128 { + u8 array[while($ == addressof(this) || std::mem::read_unsigned($-1, 1) & 0x80 != 0)] [[hidden]]; + } [[format("type::impl::format_leb128"), transform("type::impl::transform_leb128")]]; + + namespace impl { + + fn transform_leb128_array(auto array) { + u128 res = array[0] & 0x7f; + for(u8 i = 1, array[i-1] & 0x80 != 0, i+=1) { + res |= u64(array[i] & 0x7f) << 7 * i; + } + return res; + }; + + fn format_leb128(auto leb128) { + u128 res = type::impl::transform_leb128_array(leb128.array); + return std::format("{} ({:#x})", res, res); + }; + + fn transform_leb128(auto leb128) { + return type::impl::transform_leb128_array(leb128.array); + }; + } + +} +