From 0b08e8e330597ee2a6b6e6828e2a33dab6ae54f6 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Wed, 5 Jan 2022 17:58:07 +0100 Subject: Avoid using the print API The current Zig print API is quite good at slowing down compilation and producing binary bloat. This commit makes an attempt to avoid using it when not nessesary. --- clap.zig | 60 +++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 23 deletions(-) (limited to 'clap.zig') diff --git a/clap.zig b/clap.zig index 8b2357b..7234283 100644 --- a/clap.zig +++ b/clap.zig @@ -405,18 +405,21 @@ pub fn helpFull( continue; var cs = io.countingWriter(stream); - try stream.print("\t", .{}); + try stream.writeAll("\t"); try printParam(cs.writer(), Id, param, Error, context, valueText); try stream.writeByteNTimes(' ', max_spacing - @intCast(usize, cs.bytes_written)); + const help_text = try helpText(context, param); var help_text_line_it = mem.split(u8, help_text, "\n"); var indent_line = false; while (help_text_line_it.next()) |line| : (indent_line = true) { if (indent_line) { - try stream.print("\t", .{}); + try stream.writeAll("\t"); try stream.writeByteNTimes(' ', max_spacing); } - try stream.print("\t{s}\n", .{line}); + try stream.writeAll("\t"); + try stream.writeAll(line); + try stream.writeAll("\n"); } } } @@ -430,25 +433,29 @@ fn printParam( valueText: fn (@TypeOf(context), Param(Id)) Error![]const u8, ) !void { if (param.names.short) |s| { - try stream.print("-{c}", .{s}); + try stream.writeAll(&[_]u8{ '-', s }); } else { - try stream.print(" ", .{}); + try stream.writeAll(" "); } if (param.names.long) |l| { if (param.names.short) |_| { - try stream.print(", ", .{}); + try stream.writeAll(", "); } else { - try stream.print(" ", .{}); + try stream.writeAll(" "); } - try stream.print("--{s}", .{l}); + try stream.writeAll("--"); + try stream.writeAll(l); } - switch (param.takes_value) { - .none => {}, - .one => try stream.print(" <{s}>", .{valueText(context, param)}), - .many => try stream.print(" <{s}>...", .{valueText(context, param)}), - } + if (param.takes_value == .none) + return; + + try stream.writeAll(" <"); + try stream.writeAll(try valueText(context, param)); + try stream.writeAll(">"); + if (param.takes_value == .many) + try stream.writeAll("..."); } /// A wrapper around helpFull for simple helpText and valueText functions that @@ -567,7 +574,7 @@ pub fn usageFull( try cs.writeByte(name); } if (cos.bytes_written != 0) - try cs.writeByte(']'); + try cs.writeAll("]"); var positional: ?Param(Id) = null; for (params) |param| { @@ -587,13 +594,17 @@ pub fn usageFull( }; if (cos.bytes_written != 0) - try cs.writeByte(' '); - - try cs.print("[{s}{s}", .{ prefix, name }); - switch (param.takes_value) { - .none => {}, - .one => try cs.print(" <{s}>", .{try valueText(context, param)}), - .many => try cs.print(" <{s}>...", .{try valueText(context, param)}), + try cs.writeAll(" "); + + try cs.writeAll("["); + try cs.writeAll(prefix); + try cs.writeAll(name); + if (param.takes_value != .none) { + try cs.writeAll(" <"); + try cs.writeAll(try valueText(context, param)); + try cs.writeAll(">"); + if (param.takes_value == .many) + try cs.writeAll("..."); } try cs.writeByte(']'); @@ -601,8 +612,11 @@ pub fn usageFull( if (positional) |p| { if (cos.bytes_written != 0) - try cs.writeByte(' '); - try cs.print("<{s}>", .{try valueText(context, p)}); + try cs.writeAll(" "); + + try cs.writeAll("<"); + try cs.writeAll(try valueText(context, p)); + try cs.writeAll(">"); } } -- cgit v1.2.3