Small cleanup

This commit is contained in:
LemonBoy 2020-10-31 15:16:59 +01:00 committed by Andrew Kelley
parent 2b5e93fd3e
commit d4a8fc8b67

View file

@ -463,12 +463,11 @@ pub fn formatType(
try formatType(@enumToInt(value), fmt, options, writer, max_depth); try formatType(@enumToInt(value), fmt, options, writer, max_depth);
try writer.writeAll(")"); try writer.writeAll(")");
}, },
.Union => { .Union => |info| {
try writer.writeAll(@typeName(T)); try writer.writeAll(@typeName(T));
if (max_depth == 0) { if (max_depth == 0) {
return writer.writeAll("{ ... }"); return writer.writeAll("{ ... }");
} }
const info = @typeInfo(T).Union;
if (info.tag_type) |UnionTagType| { if (info.tag_type) |UnionTagType| {
try writer.writeAll("{ ."); try writer.writeAll("{ .");
try writer.writeAll(@tagName(@as(UnionTagType, value))); try writer.writeAll(@tagName(@as(UnionTagType, value)));
@ -507,30 +506,30 @@ pub fn formatType(
if (info.child == u8) { if (info.child == u8) {
return formatText(value, fmt, options, writer); return formatText(value, fmt, options, writer);
} }
return format(writer, "{}@{x}", .{ @typeName(@typeInfo(T).Pointer.child), @ptrToInt(value) }); return format(writer, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value) });
}, },
.Enum, .Union, .Struct => { .Enum, .Union, .Struct => {
return formatType(value.*, fmt, options, writer, max_depth); return formatType(value.*, fmt, options, writer, max_depth);
}, },
else => return format(writer, "{}@{x}", .{ @typeName(@typeInfo(T).Pointer.child), @ptrToInt(value) }), else => return format(writer, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value) }),
}, },
.Many, .C => { .Many, .C => {
if (ptr_info.sentinel) |sentinel| { if (ptr_info.sentinel) |sentinel| {
return formatType(mem.span(value), fmt, options, writer, max_depth); return formatType(mem.span(value), fmt, options, writer, max_depth);
} }
if (ptr_info.child == u8) { if (ptr_info.child == u8) {
if (fmt.len > 0 and fmt[0] == 's') { if (fmt.len > 0 and comptime mem.indexOfScalar(u8, "sxXzZ", fmt[0]) != null) {
return formatText(mem.span(value), fmt, options, writer); return formatText(mem.span(value), fmt, options, writer);
} }
} }
return format(writer, "{}@{x}", .{ @typeName(@typeInfo(T).Pointer.child), @ptrToInt(value) }); return format(writer, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value) });
}, },
.Slice => { .Slice => {
if (max_depth == 0) { if (max_depth == 0) {
return writer.writeAll("[ ... ]"); return writer.writeAll("[ ... ]");
} }
if (ptr_info.child == u8) { if (ptr_info.child == u8) {
if (fmt.len == 0 or fmt[0] == 's' or fmt[0] == 'x' or fmt[0] == 'X') { if (fmt.len == 0 or comptime mem.indexOfScalar(u8, "sxXzZ", fmt[0]) != null) {
return formatText(value, fmt, options, writer); return formatText(value, fmt, options, writer);
} }
} }