mirror of
https://mzte.de/git/LordMZTE/dotfiles.git
synced 2024-11-15 14:33:26 +01:00
35 lines
977 B
Zig
35 lines
977 B
Zig
|
const std = @import("std");
|
||
|
|
||
|
pub fn delimitedWriter(writer: anytype, delimeter: u8) DelimitedWriter(@TypeOf(writer)) {
|
||
|
return DelimitedWriter(@TypeOf(writer)).init(writer, delimeter);
|
||
|
}
|
||
|
|
||
|
/// Utility struct for building delimeter-separated strings
|
||
|
pub fn DelimitedWriter(comptime Writer: type) type {
|
||
|
return struct {
|
||
|
writer: Writer,
|
||
|
delimeter: u8,
|
||
|
has_written: bool,
|
||
|
|
||
|
const Self = @This();
|
||
|
|
||
|
pub fn init(writer: Writer, delimeter: u8) Self {
|
||
|
return .{
|
||
|
.writer = writer,
|
||
|
.delimeter = delimeter,
|
||
|
.has_written = false,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/// Push a string, inserting a delimiter if necessary
|
||
|
pub fn push(self: *Self, str: []const u8) !void {
|
||
|
if (self.has_written) {
|
||
|
try self.writer.writeByte(self.delimeter);
|
||
|
}
|
||
|
self.has_written = true;
|
||
|
|
||
|
try self.writer.writeAll(str);
|
||
|
}
|
||
|
};
|
||
|
}
|