DoxigAlpha

expand_variables_autoconf_at

Function parameters

Parameters

#
bw:*Writer
contents:[]const u8
values:std.StringArrayHashMap(Value)
used:[]bool

Type definitions in this namespace

Types

#

Functions in this namespace

Functions

#
getOutput
Deprecated; use `getOutputFile`.

= .config_header

Values

#
base_id
= .config_header

Source

Implementation

#
fn expand_variables_autoconf_at(
    bw: *Writer,
    contents: []const u8,
    values: std.StringArrayHashMap(Value),
    used: []bool,
) !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.indexOfScalarPos(u8, contents, curr + 1, '@')) |close_pos| {
            if (close_pos == curr + 1) {
                // closed immediately, preserve as a literal
                continue;
            }
            const valid_varname_end = std.mem.indexOfNonePos(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 = values.getIndex(key) orelse {
                // Report the missing key to the caller.
                try bw.writeAll(key);
                return error.MissingValue;
            };
            const value = values.unmanaged.entries.slice().items(.value)[index];
            used[index] = true;
            try bw.writeAll(contents[source_offset..curr]);
            switch (value) {
                .undef, .defined => {},
                .boolean => |b| try bw.writeByte(@as(u8, '0') + @intFromBool(b)),
                .int => |i| try bw.print("{d}", .{i}),
                .ident, .string => |s| try bw.writeAll(s),
            }

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

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