authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-05 03:47:23 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-05 03:47:23 -07:00
loge6270ad653d1443554cda9c5a020d56a59baa68a
treee083cb1659d5cf97e94d4d964aa0c6c9f0b308e8
parentb96b0f26a452ab814ae2b5705ae69feb531802e8
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

some small tidy


2 files changed, 25 insertions(+), 51 deletions(-)

fmt.zig+21-47
......@@ -54,11 +54,7 @@ const extras = @import("extras");
5454/// A user type may be a `struct`, `vector`, `union` or `enum` type.
5555///
5656/// To print literal curly braces, escape them by writing them twice, e.g. `{{` or `}}`.
57pub fn format(
58 writer: anytype,
59 comptime fmt: []const u8,
60 args: anytype,
61) !void {
57pub fn format(writer: anytype, comptime fmt: []const u8, args: anytype) !void {
6258 const ArgsType = @TypeOf(args);
6359 const args_type_info = @typeInfo(ArgsType);
6460 if (args_type_info != .@"struct") {
......@@ -134,16 +130,14 @@ pub fn format(
134130 const arg_pos = comptime switch (placeholder.arg) {
135131 .none => null,
136132 .number => |pos| pos,
137 .named => |arg_name| std.meta.fieldIndex(ArgsType, arg_name) orelse
138 @compileError("no argument with name '" ++ arg_name ++ "'"),
133 .named => |arg_name| std.meta.fieldIndex(ArgsType, arg_name) orelse @compileError("no argument with name '" ++ arg_name ++ "'"),
139134 };
140135
141136 const width = switch (placeholder.width) {
142137 .none => null,
143138 .number => |v| v,
144139 .named => |arg_name| blk: {
145 const arg_i = comptime std.meta.fieldIndex(ArgsType, arg_name) orelse
146 @compileError("no argument with name '" ++ arg_name ++ "'");
140 const arg_i = comptime std.meta.fieldIndex(ArgsType, arg_name) orelse @compileError("no argument with name '" ++ arg_name ++ "'");
147141 _ = comptime arg_state.nextArg(arg_i) orelse @compileError("too few arguments");
148142 break :blk @field(args, arg_name);
149143 },
......@@ -153,8 +147,7 @@ pub fn format(
153147 .none => null,
154148 .number => |v| v,
155149 .named => |arg_name| blk: {
156 const arg_i = comptime std.meta.fieldIndex(ArgsType, arg_name) orelse
157 @compileError("no argument with name '" ++ arg_name ++ "'");
150 const arg_i = comptime std.meta.fieldIndex(ArgsType, arg_name) orelse @compileError("no argument with name '" ++ arg_name ++ "'");
158151 _ = comptime arg_state.nextArg(arg_i) orelse @compileError("too few arguments");
159152 break :blk @field(args, arg_name);
160153 },
......@@ -165,7 +158,7 @@ pub fn format(
165158 try formatType(
166159 @field(args, fields_info[arg_to_print].name),
167160 placeholder.specifier_arg,
168 FormatOptions{
161 .{
169162 .fill = placeholder.fill,
170163 .alignment = placeholder.alignment,
171164 .width = width,
......@@ -244,8 +237,7 @@ pub const Placeholder = struct {
244237 };
245238
246239 // Parse the positional argument number
247 const arg = comptime parser.specifier() catch |err|
248 @compileError(@errorName(err));
240 const arg = comptime parser.specifier() catch |err| @compileError(@errorName(err));
249241
250242 // Parse the format specifier
251243 const specifier_arg = comptime parser.until(':');
......@@ -291,8 +283,7 @@ pub const Placeholder = struct {
291283 }
292284
293285 // Parse the width parameter
294 const width = comptime parser.specifier() catch |err|
295 @compileError(@errorName(err));
286 const width = comptime parser.specifier() catch |err| @compileError(@errorName(err));
296287
297288 // Skip the dot, if present
298289 if (comptime parser.char()) |ch| {
......@@ -302,8 +293,7 @@ pub const Placeholder = struct {
302293 }
303294
304295 // Parse the precision parameter
305 const precision = comptime parser.specifier() catch |err|
306 @compileError(@errorName(err));
296 const precision = comptime parser.specifier() catch |err| @compileError(@errorName(err));
307297
308298 if (comptime parser.char()) |ch| {
309299 @compileError("extraneous trailing character '" ++ std.unicode.utf8EncodeComptime(ch) ++ "'");
......@@ -450,8 +440,7 @@ fn formatType(value: anytype, comptime fmt: []const u8, options: FormatOptions,
450440 return formatBuf(if (value) "true" else "false", options, writer);
451441 },
452442 .optional => {
453 if (actual_fmt.len == 0 or actual_fmt[0] != '?')
454 @compileError("cannot format optional without a specifier (i.e. {?} or {any})");
443 if (actual_fmt.len == 0 or actual_fmt[0] != '?') @compileError("cannot format optional without a specifier (i.e. {?} or {any})");
455444 const remaining_fmt = comptime stripOptionalOrErrorUnionSpec(actual_fmt);
456445 if (value) |payload| {
457446 return formatType(payload, remaining_fmt, options, writer, max_depth);
......@@ -460,8 +449,7 @@ fn formatType(value: anytype, comptime fmt: []const u8, options: FormatOptions,
460449 }
461450 },
462451 .error_union => {
463 if (actual_fmt.len == 0 or actual_fmt[0] != '!')
464 @compileError("cannot format error union without a specifier (i.e. {!} or {any})");
452 if (actual_fmt.len == 0 or actual_fmt[0] != '!') @compileError("cannot format error union without a specifier (i.e. {!} or {any})");
465453 const remaining_fmt = comptime stripOptionalOrErrorUnionSpec(actual_fmt);
466454 if (value) |payload| {
467455 return formatType(payload, remaining_fmt, options, writer, max_depth);
......@@ -571,8 +559,9 @@ fn formatType(value: anytype, comptime fmt: []const u8, options: FormatOptions,
571559 invalidFmtError(fmt, value);
572560 },
573561 .slice => {
574 if (actual_fmt.len == 0)
562 if (actual_fmt.len == 0) {
575563 @compileError("cannot format slice without a specifier (i.e. {s} or {any})");
564 }
576565 if (max_depth == 0) {
577566 return writer.writeAll("{ ... }");
578567 }
......@@ -590,8 +579,9 @@ fn formatType(value: anytype, comptime fmt: []const u8, options: FormatOptions,
590579 },
591580 },
592581 .array => |info| {
593 if (actual_fmt.len == 0)
582 if (actual_fmt.len == 0) {
594583 @compileError("cannot format array without a specifier (i.e. {s} or {any})");
584 }
595585 if (max_depth == 0) {
596586 return writer.writeAll("{ ... }");
597587 }
......@@ -700,13 +690,7 @@ fn formatAddress(value: anytype, options: FormatOptions, writer: anytype) @TypeO
700690 @compileError("cannot format non-pointer type " ++ @typeName(T) ++ " with * specifier");
701691}
702692
703pub fn formatInt(
704 value: anytype,
705 base: u8,
706 case: Case,
707 options: FormatOptions,
708 writer: anytype,
709) !void {
693pub fn formatInt(value: anytype, base: u8, case: Case, options: FormatOptions, writer: anytype) !void {
710694 std.debug.assert(base >= 2);
711695
712696 const int_value = if (@TypeOf(value) == comptime_int) blk: {
......@@ -768,12 +752,7 @@ pub fn formatInt(
768752 return formatBuf(buf[index..], options, writer);
769753}
770754
771fn formatValue(
772 value: anytype,
773 comptime fmt: []const u8,
774 options: FormatOptions,
775 writer: anytype,
776) !void {
755fn formatValue(value: anytype, comptime fmt: []const u8, options: FormatOptions, writer: anytype) !void {
777756 const T = @TypeOf(value);
778757 switch (@typeInfo(T)) {
779758 // .float, .comptime_float => return formatFloatValue(value, fmt, options, writer),
......@@ -788,18 +767,13 @@ fn invalidFmtError(comptime fmt: []const u8, value: anytype) void {
788767 @compileError("invalid format string '" ++ fmt ++ "' for type '" ++ @typeName(@TypeOf(value)) ++ "'");
789768}
790769
791fn formatBuf(
792 buf: []const u8,
793 options: FormatOptions,
794 writer: anytype,
795) !void {
770fn formatBuf(buf: []const u8, options: FormatOptions, writer: anytype) !void {
796771 if (options.width) |min_width| {
797772 // In case of error assume the buffer content is ASCII-encoded
798773 const width = std.unicode.utf8CountCodepoints(buf) catch buf.len;
799774 const padding = if (width < min_width) min_width - width else 0;
800775
801 if (padding == 0)
802 return writer.writeAll(buf);
776 if (padding == 0) return writer.writeAll(buf);
803777
804778 var fill_buffer: [4]u8 = undefined;
805779 const fill_utf8 = if (std.unicode.utf8Encode(options.fill, &fill_buffer)) |len|
......@@ -863,14 +837,14 @@ const digits2_alphabet = blk: {
863837};
864838
865839/// Converts values in the range [0, 100) to a base 10 string.
866fn digits2(value: u8) [2]u8 {
840pub fn digits2(value: u8) [2]u8 {
867841 return digits2_alphabet[value * 2 ..][0..2].*;
868842}
869843
870fn digitToChar(digit: u8, case: Case) u8 {
844pub fn digitToChar(digit: u8, case: Case) u8 {
871845 return switch (digit) {
872846 0...9 => digit + '0',
873 10...35 => digit + ((if (case == .upper) @as(u8, 'A') else @as(u8, 'a')) - 10),
847 10...35 => digit + (if (case == .upper) @as(u8, 'A') else @as(u8, 'a')) - 10,
874848 else => unreachable,
875849 };
876850}
nio.zig+4-4
......@@ -227,7 +227,7 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type {
227227 return writeAll(self, std.mem.asBytes(&value));
228228 }
229229
230 pub fn writeIntPretty(self: Self, value: anytype, base: u8, case: std.fmt.Case) !void {
230 pub fn writeIntPretty(self: Self, value: anytype, base: u8, case: fmt.Case) !void {
231231 std.debug.assert(base >= 2);
232232
233233 const int_value = if (@TypeOf(value) == comptime_int) @as(std.math.IntFittingRange(value, value), value) else value;
......@@ -247,20 +247,20 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type {
247247 if (base == 10) {
248248 while (a >= 100) : (a = @divTrunc(a, 100)) {
249249 index -= 2;
250 buf[index..][0..2].* = std.fmt.digits2(@intCast(a % 100));
250 buf[index..][0..2].* = fmt.digits2(@intCast(a % 100));
251251 }
252252 if (a < 10) {
253253 index -= 1;
254254 buf[index] = '0' + @as(u8, @intCast(a));
255255 } else {
256256 index -= 2;
257 buf[index..][0..2].* = std.fmt.digits2(@intCast(a));
257 buf[index..][0..2].* = fmt.digits2(@intCast(a));
258258 }
259259 } else {
260260 while (true) {
261261 const digit = a % base;
262262 index -= 1;
263 buf[index] = std.fmt.digitToChar(@intCast(digit), case);
263 buf[index] = fmt.digitToChar(@intCast(digit), case);
264264 a /= base;
265265 if (a == 0) break;
266266 }