expand_variables_cmake
Function parameters
Parameters
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_cmake(
allocator: Allocator,
contents: []const u8,
values: std.StringArrayHashMap(Value),
) ![]const u8 {
var result: std.array_list.Managed(u8) = .init(allocator);
errdefer result.deinit();
const valid_varname_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/_.+-";
const open_var = "${";
var curr: usize = 0;
var source_offset: usize = 0;
const Position = struct {
source: usize,
target: usize,
};
var var_stack: std.array_list.Managed(Position) = .init(allocator);
defer var_stack.deinit();
loop: while (curr < contents.len) : (curr += 1) {
switch (contents[curr]) {
'@' => blk: {
if (std.mem.indexOfScalarPos(u8, contents, curr + 1, '@')) |close_pos| {
if (close_pos == curr + 1) {
// closed immediately, preserve as a literal
break :blk;
}
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
break :blk;
}
const key = contents[curr + 1 .. close_pos];
const value = values.get(key) orelse return error.MissingValue;
const missing = contents[source_offset..curr];
try result.appendSlice(missing);
switch (value) {
.undef, .defined => {},
.boolean => |b| {
try result.append(if (b) '1' else '0');
},
.int => |i| {
try result.print("{d}", .{i});
},
.ident, .string => |s| {
try result.appendSlice(s);
},
}
curr = close_pos;
source_offset = close_pos + 1;
continue :loop;
}
},
'$' => blk: {
const next = curr + 1;
if (next == contents.len or contents[next] != '{') {
// no open bracket detected, preserve as a literal
break :blk;
}
const missing = contents[source_offset..curr];
try result.appendSlice(missing);
try result.appendSlice(open_var);
source_offset = curr + open_var.len;
curr = next;
try var_stack.append(Position{
.source = curr,
.target = result.items.len - open_var.len,
});
continue :loop;
},
'}' => blk: {
if (var_stack.items.len == 0) {
// no open bracket, preserve as a literal
break :blk;
}
const open_pos = var_stack.pop().?;
if (source_offset == open_pos.source) {
source_offset += open_var.len;
}
const missing = contents[source_offset..curr];
try result.appendSlice(missing);
const key_start = open_pos.target + open_var.len;
const key = result.items[key_start..];
if (key.len == 0) {
return error.MissingKey;
}
const value = values.get(key) orelse return error.MissingValue;
result.shrinkRetainingCapacity(result.items.len - key.len - open_var.len);
switch (value) {
.undef, .defined => {},
.boolean => |b| {
try result.append(if (b) '1' else '0');
},
.int => |i| {
try result.print("{d}", .{i});
},
.ident, .string => |s| {
try result.appendSlice(s);
},
}
source_offset = curr + 1;
continue :loop;
},
'\\' => {
// backslash is not considered a special character
continue :loop;
},
else => {},
}
if (var_stack.items.len > 0 and std.mem.indexOfScalar(u8, valid_varname_chars, contents[curr]) == null) {
return error.InvalidCharacter;
}
}
if (source_offset != contents.len) {
const missing = contents[source_offset..];
try result.appendSlice(missing);
}
return result.toOwnedSlice();
}