Function writeILEB128 [src]

Alias for std.leb128.writeIleb128

Write a single signed integer as signed LEB128 to the given writer.

Prototype

pub fn writeIleb128(writer: anytype, arg: anytype) !void

Source

pub fn writeIleb128(writer: anytype, arg: anytype) !void { const Arg = @TypeOf(arg); const Int = switch (Arg) { comptime_int => std.math.IntFittingRange(-@abs(arg), @abs(arg)), else => Arg, }; const Signed = if (@typeInfo(Int).int.bits < 8) i8 else Int; const Unsigned = std.meta.Int(.unsigned, @typeInfo(Signed).int.bits); var value: Signed = arg; while (true) { const unsigned: Unsigned = @bitCast(value); const byte: u8 = @truncate(unsigned); value >>= 6; if (value == -1 or value == 0) { try writer.writeByte(byte & 0x7F); break; } else { value >>= 1; try writer.writeByte(byte | 0x80); } } }