Function writeUnsignedFixed [src]

This is an "advanced" function. It allows one to use a fixed amount of memory to store a ULEB128. 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 ULEB128 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.

Prototype

pub fn writeUnsignedFixed(comptime l: usize, ptr: *[l]u8, int: std.meta.Int(.unsigned, l * 7)) void

Parameters

l: usizeptr: *[l]u8int: std.meta.Int(.unsigned, l * 7)

Example

test writeUnsignedFixed { { var buf: [4]u8 = undefined; writeUnsignedFixed(4, &buf, 0); var reader: std.Io.Reader = .fixed(&buf); try testing.expectEqual(0, try reader.takeLeb128(u64)); } { var buf: [4]u8 = undefined; writeUnsignedFixed(4, &buf, 1); var reader: std.Io.Reader = .fixed(&buf); try testing.expectEqual(1, try reader.takeLeb128(u64)); } { var buf: [4]u8 = undefined; writeUnsignedFixed(4, &buf, 1000); var reader: std.Io.Reader = .fixed(&buf); try testing.expectEqual(1000, try reader.takeLeb128(u64)); } { var buf: [4]u8 = undefined; writeUnsignedFixed(4, &buf, 10000000); var reader: std.Io.Reader = .fixed(&buf); try testing.expectEqual(10000000, try reader.takeLeb128(u64)); } }

Source

pub fn writeUnsignedFixed(comptime l: usize, ptr: *[l]u8, int: std.meta.Int(.unsigned, l * 7)) void { writeUnsignedExtended(ptr, int); }