DoxigAlpha

writeUleb128

Write a single unsigned integer as unsigned 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 writeUleb128(writer: anytype, arg: anytype) !void {
    const Arg = @TypeOf(arg);
    const Int = switch (Arg) {
        comptime_int => std.math.IntFittingRange(arg, arg),
        else => Arg,
    };
    const Value = if (@typeInfo(Int).int.bits < 8) u8 else Int;
    var value: Value = arg;

    while (true) {
        const byte: u8 = @truncate(value & 0x7f);
        value >>= 7;
        if (value == 0) {
            try writer.writeByte(byte);
            break;
        } else {
            try writer.writeByte(byte | 0x80);
        }
    }
}