Reads an integer from memory with size equal to bytes.len. ReturnType specifies the return type, which must be large enough to store the result.
pub fn readVarInt(comptime ReturnType: type, bytes: []const u8, endian: Endian) ReturnType
pub fn readVarInt(comptime ReturnType: type, bytes: []const u8, endian: Endian) ReturnType {
assert(@typeInfo(ReturnType).int.bits >= bytes.len * 8);
const bits = @typeInfo(ReturnType).int.bits;
const signedness = @typeInfo(ReturnType).int.signedness;
const WorkType = @Int(signedness, @max(16, bits));
var result: WorkType = 0;
switch (endian) {
.big => {
for (bytes) |b| {
result = (result << 8) | b;
}
},
.little => {
const ShiftType = math.Log2Int(WorkType);
for (bytes, 0..) |b, index| {
result = result | (@as(WorkType, b) << @as(ShiftType, @intCast(index * 8)));
}
},
}
return @truncate(result);
}