writeSignedFixed
This is an "advanced" function. It allows one to use a fixed amount of memory to store an ILEB128. This defeats the entire purpose of using this data encoding; it will no longer use fewer bytes to store smaller numbers. The advantage of using a fixed width is that it makes fields have a predictable size and so depending on the use case this tradeoff can be worthwhile. An example use case of this is in emitting DWARF info where one wants to make a ILEB128 field "relocatable", meaning that it becomes possible to later go back and patch the number to be a different value without shifting all the following code.
Function parameters
Parameters
- l:usize
- ptr:*[l]u8
- int:std.meta.Int(.signed, l * 7)
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 writeSignedFixed(comptime l: usize, ptr: *[l]u8, int: std.meta.Int(.signed, l * 7)) void {
const T = @TypeOf(int);
const U = if (@typeInfo(T).int.bits < 8) u8 else T;
var value: U = @intCast(int);
comptime var i = 0;
inline while (i < (l - 1)) : (i += 1) {
const byte: u8 = @bitCast(@as(i8, @truncate(value)) | -0b1000_0000);
value >>= 7;
ptr[i] = byte;
}
ptr[i] = @as(u7, @bitCast(@as(i7, @truncate(value))));
}