Repository navigation
Expand file tree
/
Copy pathtemplate.zig
More file actions
86 lines (80 loc) · 2.89 KB
/
Copy pathtemplate.zig
File metadata and controls
86 lines (80 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const std = @import("std");
fn decimalToString(allocator: std.mem.Allocator, n: anytype) ![]const u8 {
const info = @typeInfo(@TypeOf(n));
if (info != .int or info.int.signedness != .unsigned) {
@compileError("Only implemented for unsigned integers.");
}
const s = try std.fmt.allocPrint(allocator, "{d}", .{n});
defer allocator.free(s);
const digits = s.len;
const commas = (digits - 1) / 3;
const result = try allocator.alloc(u8, digits + commas);
errdefer comptime unreachable;
var i: usize = result.len - 1;
var j: usize = s.len - 1;
while (true) {
if ((result.len - i) % 4 == 0) {
result[i] = ',';
i -= 1;
}
result[i] = s[j];
if (i == 0 and j == 0) {
break;
} else if (i > 0 and j > 0) {} else unreachable;
i -= 1;
j -= 1;
}
return result;
}
pub fn fill(
a: std.mem.Allocator,
template: []const u8,
o: anytype,
) ![]const u8 {
var w = try std.Io.Writer.Allocating.initCapacity(a, template.len * 2);
errdefer w.deinit();
const writer = &(w.writer);
var i: usize = 0;
while (i < template.len) {
if (std.mem.indexOfPos(u8, template, i, "{{")) |start| {
if (std.mem.indexOfPos(u8, template, start + 2, "}}")) |end| {
try writer.writeAll(template[i..start]);
defer i = end + 2;
const name = std.mem.trim(u8, template[start + 2 .. end], " ");
inline for (
@typeInfo(@TypeOf(o)).@"struct".fields,
) |f| {
if (std.mem.eql(u8, f.name, name)) {
const field = @field(o, f.name);
switch (@typeInfo(@TypeOf(field))) {
.int => {
const s = try decimalToString(a, field);
defer a.free(s);
try writer.writeAll(s);
},
.pointer => |p| {
if (p.size != .slice or p.child != u8) {
comptime unreachable;
}
try writer.writeAll(field);
},
.@"struct" => return error.InvalidField,
else => comptime unreachable,
}
break;
}
} else {
return error.InvalidField;
}
} else {
// If there is no closing }} treat the initial {{ as a literal
try writer.writeAll(template[i..]);
break;
}
} else {
try writer.writeAll(template[i..]);
break;
}
}
return try w.toOwnedSlice();
}