feature. See also
. The project being documented here (as the example) is the Zig library itself.
ConfigHeader.expandVariablesAutoconfAt
fn expandVariablesAutoconfAt(
w: *Writer,
contents: []const u8,
conf: *const Configuration,
value_pairs: []const Value.Pair,
value_map: *const ValueMap,
) !void
File
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) {
continue;
}
const valid_varname_end = std.mem.findNonePos(u8, contents, curr + 1, valid_varname_chars) orelse 0;
if (valid_varname_end != close_pos) {
continue;
}
const key = contents[curr + 1 .. close_pos];
const index = value_map.getIndex(key) orelse {
try w.writeAll(key);
return error.MissingValue;
};
const value = value_pairs[index].index;
value_map.values()[index] = true;
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..]);
}