fix byte formatted printing

This commit is contained in:
Andrew Kelley 2025-02-17 16:15:33 -08:00
parent bb1dffcf32
commit 7569494b45
3 changed files with 40 additions and 42 deletions

View File

@ -860,46 +860,46 @@ pub fn printInt(
options: std.fmt.Options,
value: anytype,
) anyerror!void {
comptime var base = 10;
comptime var case: std.fmt.Case = .lower;
const int_value = if (@TypeOf(value) == comptime_int) blk: {
const Int = std.math.IntFittingRange(value, value);
break :blk @as(Int, value);
} else value;
if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "d")) {
base = 10;
case = .lower;
} else if (comptime std.mem.eql(u8, fmt, "c")) {
if (@typeInfo(@TypeOf(int_value)).int.bits <= 8) {
return printAsciiChar(bw, @as(u8, int_value), options);
} else {
@compileError("cannot print integer that is larger than 8 bits as an ASCII character");
}
} else if (comptime std.mem.eql(u8, fmt, "u")) {
if (@typeInfo(@TypeOf(int_value)).int.bits <= 21) {
return printUnicodeCodepoint(bw, @as(u21, int_value), options);
} else {
@compileError("cannot print integer that is larger than 21 bits as an UTF-8 sequence");
}
} else if (comptime std.mem.eql(u8, fmt, "b")) {
base = 2;
case = .lower;
} else if (comptime std.mem.eql(u8, fmt, "x")) {
base = 16;
case = .lower;
} else if (comptime std.mem.eql(u8, fmt, "X")) {
base = 16;
case = .upper;
} else if (comptime std.mem.eql(u8, fmt, "o")) {
base = 8;
case = .lower;
} else {
invalidFmtError(fmt, value);
switch (fmt.len) {
0 => return printIntOptions(bw, int_value, 10, .lower, options),
1 => switch (fmt[0]) {
'd' => return printIntOptions(bw, int_value, 10, .lower, options),
'c' => {
if (@typeInfo(@TypeOf(int_value)).int.bits <= 8) {
return printAsciiChar(bw, @as(u8, int_value), options);
} else {
@compileError("cannot print integer that is larger than 8 bits as an ASCII character");
}
},
'u' => {
if (@typeInfo(@TypeOf(int_value)).int.bits <= 21) {
return printUnicodeCodepoint(bw, @as(u21, int_value), options);
} else {
@compileError("cannot print integer that is larger than 21 bits as an UTF-8 sequence");
}
},
'b' => return printIntOptions(bw, int_value, 2, .lower, options),
'x' => return printIntOptions(bw, int_value, 16, .lower, options),
'X' => return printIntOptions(bw, int_value, 16, .upper, options),
'o' => return printIntOptions(bw, int_value, 8, .lower, options),
'B' => return printByteSize(bw, int_value, .decimal, options),
else => invalidFmtError(fmt, value),
},
2 => {
if (fmt[0] == 'B' and fmt[1] == 'i') {
return printByteSize(bw, int_value, .binary, options);
} else {
invalidFmtError(fmt, value);
}
},
else => invalidFmtError(fmt, value),
}
return printIntOptions(bw, int_value, base, case, options);
comptime unreachable;
}
pub fn printAsciiChar(bw: *BufferedWriter, c: u8, options: std.fmt.Options) anyerror!void {
@ -1128,7 +1128,7 @@ pub const ByteSizeUnits = enum {
pub fn printByteSize(
bw: *std.io.BufferedWriter,
value: u64,
units: ByteSizeUnits,
comptime units: ByteSizeUnits,
options: std.fmt.Options,
) anyerror!void {
if (value == 0) return alignBufferOptions(bw, "0B", options);

View File

@ -1928,7 +1928,7 @@ fn resolveLibInput(
.root_dir = lib_directory,
.sub_path = try std.fmt.allocPrint(arena, "lib{s}.tbd", .{lib_name}),
};
try checked_paths.writer(gpa).print("\n {}", .{test_path});
try checked_paths.print(gpa, "\n {}", .{test_path});
var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
error.FileNotFound => break :tbd,
else => |e| fatal("unable to search for tbd library '{}': {s}", .{ test_path, @errorName(e) }),
@ -1947,7 +1947,7 @@ fn resolveLibInput(
},
}),
};
try checked_paths.writer(gpa).print("\n {}", .{test_path});
try checked_paths.print(gpa, "\n {}", .{test_path});
switch (try resolvePathInputLib(gpa, arena, unresolved_inputs, resolved_inputs, ld_script_bytes, target, .{
.path = test_path,
.query = name_query.query,
@ -1964,7 +1964,7 @@ fn resolveLibInput(
.root_dir = lib_directory,
.sub_path = try std.fmt.allocPrint(arena, "lib{s}.so", .{lib_name}),
};
try checked_paths.writer(gpa).print("\n {}", .{test_path});
try checked_paths.print(gpa, "\n {}", .{test_path});
var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
error.FileNotFound => break :so,
else => |e| fatal("unable to search for so library '{}': {s}", .{
@ -1982,7 +1982,7 @@ fn resolveLibInput(
.root_dir = lib_directory,
.sub_path = try std.fmt.allocPrint(arena, "lib{s}.a", .{lib_name}),
};
try checked_paths.writer(gpa).print("\n {}", .{test_path});
try checked_paths.print(gpa, "\n {}", .{test_path});
var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
error.FileNotFound => break :mingw,
else => |e| fatal("unable to search for static library '{}': {s}", .{ test_path, @errorName(e) }),

View File

@ -103,9 +103,7 @@ const PrintZon = struct {
fn newline(pz: *PrintZon) !void {
try pz.w.writeByte('\n');
for (0..pz.indent) |_| {
try pz.w.writeByteNTimes(' ', 2);
}
try pz.w.splatByteAll(' ', 2 * pz.indent);
}
};