DoxigAlpha

writeIleb128

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

Function parameters

Parameters

#
writer:anytype
arg:anytype

Read a single unsigned LEB128 value from the given reader as type T,

Functions

#
readUleb128
Read a single unsigned LEB128 value from the given reader as type T,
writeUleb128
Write a single unsigned integer as unsigned LEB128 to the given writer.
readIleb128
Read a single signed LEB128 value from the given reader as type T,
writeIleb128
Write a single signed integer as signed LEB128 to the given writer.
writeUnsignedFixed
This is an "advanced" function.
writeUnsignedExtended
Same as `writeUnsignedFixed` but with a runtime-known length.
writeSignedFixed
This is an "advanced" function.

Source

Implementation

#
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);
        }
    }
}