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.

expandVariablesAutoconfAt

ConfigHeader.expandVariablesAutoconfAt
fn expandVariablesAutoconfAt(
    w: *Writer,
    contents: []const u8,
    conf: *const Configuration,
    value_pairs: []const Value.Pair,
    value_map: *const ValueMap,
) !void

File

lib/compiler/Maker/Step/ConfigHeader.zig:459

Code

fn expandVariablesAutoconfAt(
    w: *Writer,
    contents: []const u8,
    conf: *const Configuration,
    value_pairs: []const Value.Pair,
    value_map: *const ValueMap,
) !void {
    const valid_varname_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";

    var curr: usize = 0;
    var source_offset: usize = 0;
    while (curr < contents.len) : (curr += 1) {
        if (contents[curr] != '@') continue;
        if (std.mem.findScalarPos(u8, contents, curr + 1, '@')) |close_pos| {
            if (close_pos == curr + 1) {
                // closed immediately, preserve as a literal
                continue;
            }
            const valid_varname_end = std.mem.findNonePos(u8, contents, curr + 1, valid_varname_chars) orelse 0;
            if (valid_varname_end != close_pos) {
                // contains invalid characters, preserve as a literal
                continue;
            }

            const key = contents[curr + 1 .. close_pos];
            const index = value_map.getIndex(key) orelse {
                // Report the missing key to the caller.
                try w.writeAll(key);
                return error.MissingValue;
            };
            const value = value_pairs[index].index;
            value_map.values()[index] = true; // Mark as used.
            try w.writeAll(contents[source_offset..curr]);
            switch (value.unpack(conf)) {
                .undef, .defined => {},
                .bool => |b| try w.writeByte(@as(u8, '0') + @intFromBool(b)),
                inline .u64, .i64 => |i| try w.print("{d}", .{i}),
                .ident, .string => |s| try w.writeAll(s),
            }

            curr = close_pos;
            source_offset = close_pos + 1;
        }
    }

    try w.writeAll(contents[source_offset..]);
}