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
This commit is contained in:
Oded Shapira
2022-08-05 14:45:34 +03:00
committed by GitHub
parent 65f2b7821b
commit aa6c90fa5b

33
includes/type/leb128.pat Normal file
View File

@@ -0,0 +1,33 @@
#pragma once
#include <std/io.pat>
#include <std/mem.pat>
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);
};
}
}