Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

length

Encode a length.

Encoder.length
pub fn length(self: *Encoder, len: usize) !void

File

Code

pub fn length(self: *Encoder, len: usize) !void {
    if (len < 128) return self.buffer.prependSlice(&.{@intCast(len)});
    const len32 = std.math.cast(u32, len) orelse return error.InvalidLength;
    var buf: [@sizeOf(u32) + 1]u8 = undefined;
    std.mem.writeInt(u32, buf[1..], len32, .big);
    var first: usize = 1;
    while (buf[first] == 0) first += 1;
    buf[first - 1] = @intCast((buf.len - first) | 0x80);
    return self.buffer.prependSlice(buf[first - 1 ..]);
}