| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | |
| 5 | pub fn fmtReplacer(bytes: string, from: u8, to: u8) std.fmt.Alt(FormatData, formatReplacer) { |
| 6 | return .{ .data = .{ |
| 7 | .bytes = bytes, |
| 8 | .from = from, |
| 9 | .to = to, |
| 10 | } }; |
| 11 | } |
| 12 | |
| 13 | const FormatData = struct { |
| 14 | bytes: string, |
| 15 | from: u8, |
| 16 | to: u8, |
| 17 | }; |
| 18 | |
| 19 | fn formatReplacer( |
| 20 | self: FormatData, |
| 21 | writer: *std.Io.Writer, |
| 22 | ) !void { |
| 23 | for (self.bytes) |c| { |
| 24 | try writer.writeByte(if (c == self.from) self.to else @intCast(c)); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | test { |
| 29 | const in = "C:\\Program Files\\Custom Utilities\\StringFinder.exe"; |
| 30 | const out = "C:/Program Files/Custom Utilities/StringFinder.exe"; |
| 31 | try std.testing.expectFmt(out, "{f}", .{fmtReplacer(in, '\\', '/')}); |
| 32 | } |