authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-10-26 12:52:55 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-10-26 12:52:55 -07:00
logdbff408c7a0d853be1ea17155b0982d96fde9483
tree440aa4717b59a46bc6201cd1aa21333d7fccd443
parent01fae956e2f17aa992e717e041a3dd457d440b31

add fmtReplacer()


1 files changed, 13 insertions(+), 0 deletions(-)

src/lib.zig+13
...@@ -275,3 +275,16 @@ pub fn ensureFieldSubset(comptime L: type, comptime R: type) void {...@@ -275,3 +275,16 @@ pub fn ensureFieldSubset(comptime L: type, comptime R: type) void {
275 if (!@hasField(R, item.name)) @compileError(std.fmt.comptimePrint("{s} is missing the {s} field from {s}", .{ R, item.name, L }));275 if (!@hasField(R, item.name)) @compileError(std.fmt.comptimePrint("{s} is missing the {s} field from {s}", .{ R, item.name, L }));
276 }276 }
277}277}
278
279pub fn fmtReplacer(bytes: string, from: u8, to: u8) std.fmt.Formatter(formatReplacer) {
280 return .{ .data = .{ .bytes = bytes, .from = from, .to = to } };
281}
282
283const ReplacerData = struct { bytes: string, from: u8, to: u8 };
284fn formatReplacer(self: ReplacerData, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
285 _ = fmt;
286 _ = options;
287 for (self.bytes) |c| {
288 try writer.writeByte(if (c == self.from) self.to else @intCast(u8, c));
289 }
290}