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.

parseBitCount

Parses the string buf as a base 10 integer of type u16.

Unlike std.fmt.parseInt, does not allow the '_' character in buf.

AstGen.parseBitCount
fn parseBitCount(buf: []const u8) std.fmt.ParseIntError!u16

File

lib/std/zig/AstGen.zig:8031

Code

fn parseBitCount(buf: []const u8) std.fmt.ParseIntError!u16 {
    if (buf.len == 0) return error.InvalidCharacter;

    var x: u16 = 0;

    for (buf) |c| {
        const digit = switch (c) {
            '0'...'9' => c - '0',
            else => return error.InvalidCharacter,
        };

        if (x != 0) x = try std.math.mul(u16, x, 10);
        x = try std.math.add(u16, x, digit);
    }

    return x;
}