Skip to content

Commit 24e43df

Browse files
author
Benjamin Feng
committed
General cleanup
1 parent 5d0b2d9 commit 24e43df

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

lib/std/fmtgen.zig

+25-29
Original file line numberDiff line numberDiff line change
@@ -404,16 +404,16 @@ pub fn formatType(
404404
},
405405
.Optional => {
406406
if (value) |payload| {
407-
return @call(.{ .modifier = .always_tail }, formatType, .{ payload, fmt, options, generator, max_depth });
407+
return formatType(payload, fmt, options, generator, max_depth);
408408
} else {
409409
return generator.yield("null");
410410
}
411411
},
412412
.ErrorUnion => {
413413
if (value) |payload| {
414-
return @call(.{ .modifier = .always_tail }, formatType, .{ payload, fmt, options, generator, max_depth });
414+
return formatType(payload, fmt, options, generator, max_depth);
415415
} else |err| {
416-
return @call(.{ .modifier = .always_tail }, formatType, .{ err, fmt, options, generator, max_depth });
416+
return formatType(err, fmt, options, generator, max_depth);
417417
}
418418
},
419419
.ErrorSet => {
@@ -426,52 +426,50 @@ pub fn formatType(
426426
// }
427427
generator.yield(@typeName(T));
428428
generator.yield(".");
429-
return @call(.{ .modifier = .always_tail }, formatType, .{ @tagName(value), "", options, generator, max_depth });
429+
return generator.yield(@tagName(value));
430430
},
431-
.Union => {
431+
.Union => |union_info| {
432432
// if (comptime std.meta.trait.hasFn("format")(T)) {
433433
// return value.format(fmt, options, context, Errors, output);
434434
// }
435-
generator.yield(@typeName(T));
436-
if (max_depth == 0) {
437-
return generator.yield("{ ... }");
438-
}
439-
const info = @typeInfo(T).Union;
440-
if (info.tag_type) |UnionTagType| {
435+
if (union_info.tag_type) |UnionTagType| {
436+
generator.yield(@typeName(T));
437+
if (max_depth == 0) {
438+
return generator.yield("{ ... }");
439+
}
441440
generator.yield("{ .");
442441
generator.yield(@tagName(@as(UnionTagType, value)));
443442
generator.yield(" = ");
444-
inline for (info.fields) |u_field| {
443+
inline for (union_info.fields) |u_field| {
445444
if (@enumToInt(@as(UnionTagType, value)) == u_field.enum_field.?.value) {
446445
formatType(@field(value, u_field.name), "", options, generator, max_depth - 1);
447446
}
448447
}
449448
generator.yield(" }");
450449
} else {
451-
generator.yield("@");
452-
return formatInt(@ptrToInt(&value), 16, false, FormatOptions{}, generator);
450+
return formatPtr(T, @ptrToInt(&value), generator);
453451
}
454452
},
455-
.Struct => {
453+
.Struct => |struct_info| {
456454
// if (comptime std.meta.trait.hasFn("format")(T)) {
457455
// return value.format(fmt, options, context, Errors, output);
458456
// }
459457
generator.yield(@typeName(T));
460458
if (max_depth == 0) {
461459
return generator.yield("{ ... }");
462460
}
463-
comptime var field_i = 0;
461+
464462
generator.yield("{");
465-
inline while (field_i < @memberCount(T)) : (field_i += 1) {
466-
if (field_i == 0) {
463+
inline for (struct_info.fields) |field, i| {
464+
if (i == 0) {
467465
generator.yield(" .");
468466
} else {
469467
generator.yield(", .");
470468
}
471-
generator.yield(@memberName(T, field_i));
469+
generator.yield(field.name);
472470
generator.yield(" = ");
473471

474-
formatType(@field(value, @memberName(T, field_i)), "", options, generator, max_depth - 1);
472+
formatType(@field(value, field.name), "", options, generator, max_depth - 1);
475473
}
476474
generator.yield(" }");
477475
},
@@ -484,7 +482,7 @@ pub fn formatType(
484482
return formatPtr(T.Child, @ptrToInt(value), generator);
485483
},
486484
builtin.TypeId.Enum, builtin.TypeId.Union, builtin.TypeId.Struct => {
487-
return @call(.{ .modifier = .always_tail }, formatType, .{ value.*, fmt, options, generator, max_depth });
485+
return formatType(value.*, fmt, options, generator, max_depth);
488486
},
489487
else => return formatPtr(T.Child, @ptrToInt(value), generator),
490488
},
@@ -522,7 +520,7 @@ pub fn formatType(
522520
.sentinel = null,
523521
},
524522
});
525-
return @call(.{ .modifier = .always_tail }, formatType, .{ @as(Slice, &value), fmt, options, generator, max_depth });
523+
return formatType(@as(Slice, &value), fmt, options, generator, max_depth);
526524
},
527525
.Fn => {
528526
return formatPtr(T, @ptrToInt(value), generator);
@@ -638,7 +636,7 @@ pub fn formatAsciiChar(
638636
generator: *Generator([]const u8),
639637
) void {
640638
if (std.ascii.isPrint(c))
641-
return generator.yield(@as(*const [1]u8, &c)[0..]);
639+
return generator.yield(@as(*const [1]u8, &c));
642640
@panic("FIXME");
643641
// return format(context, Errors, output, "\\x{x:0<2}", .{c});
644642
}
@@ -654,7 +652,7 @@ pub fn formatBuf(
654652
var leftover_padding = if (width > buf.len) (width - buf.len) else return;
655653
const pad_byte: u8 = options.fill;
656654
while (leftover_padding > 0) : (leftover_padding -= 1) {
657-
generator.yield(@as(*const [1]u8, &pad_byte)[0..1]);
655+
generator.yield(@as(*const [1]u8, &pad_byte));
658656
}
659657
}
660658

@@ -978,15 +976,13 @@ fn formatIntSigned(
978976

979977
const uint = @IntType(false, @TypeOf(value).bit_count);
980978
if (value < 0) {
981-
const minus_sign: u8 = '-';
982-
generator.yield(@as(*const [1]u8, &minus_sign)[0..]);
979+
generator.yield("-");
983980
const new_value = @intCast(uint, -(value + 1)) + 1;
984981
return formatIntUnsigned(new_value, base, uppercase, new_options, generator);
985982
} else if (options.width == null or options.width.? == 0) {
986983
return formatIntUnsigned(@intCast(uint, value), base, uppercase, options, generator);
987984
} else {
988-
const plus_sign: u8 = '+';
989-
generator.yield(@as(*const [1]u8, &plus_sign)[0..]);
985+
generator.yield("+");
990986
const new_value = @intCast(uint, value);
991987
return formatIntUnsigned(new_value, base, uppercase, new_options, generator);
992988
}
@@ -1022,7 +1018,7 @@ fn formatIntUnsigned(
10221018
const zero_byte: u8 = options.fill;
10231019
var leftover_padding = padding - index;
10241020
while (true) {
1025-
generator.yield(@as(*const [1]u8, &zero_byte)[0..]);
1021+
generator.yield(@as(*const [1]u8, &zero_byte));
10261022
leftover_padding -= 1;
10271023
if (leftover_padding == 0) break;
10281024
}

0 commit comments

Comments
 (0)