authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-17 16:15:33-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-01 16:35:26-07:00
log7569494b454aebeff67825fbbbe0cfe45e6e60d7
tree13a768a7a971716fffe8a6dce89f6e5c1485c92e
parentbb1dffcf32b5f59c2eaadbb522063d6c0415d924

fix byte formatted printing


3 files changed, 40 insertions(+), 42 deletions(-)

lib/std/io/BufferedWriter.zig+35-35
......@@ -860,46 +860,46 @@ pub fn printInt(
860860 options: std.fmt.Options,
861861 value: anytype,
862862) anyerror!void {
863 comptime var base = 10;
864 comptime var case: std.fmt.Case = .lower;
865
866863 const int_value = if (@TypeOf(value) == comptime_int) blk: {
867864 const Int = std.math.IntFittingRange(value, value);
868865 break :blk @as(Int, value);
869866 } else value;
870867
871 if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "d")) {
872 base = 10;
873 case = .lower;
874 } else if (comptime std.mem.eql(u8, fmt, "c")) {
875 if (@typeInfo(@TypeOf(int_value)).int.bits <= 8) {
876 return printAsciiChar(bw, @as(u8, int_value), options);
877 } else {
878 @compileError("cannot print integer that is larger than 8 bits as an ASCII character");
879 }
880 } else if (comptime std.mem.eql(u8, fmt, "u")) {
881 if (@typeInfo(@TypeOf(int_value)).int.bits <= 21) {
882 return printUnicodeCodepoint(bw, @as(u21, int_value), options);
883 } else {
884 @compileError("cannot print integer that is larger than 21 bits as an UTF-8 sequence");
885 }
886 } else if (comptime std.mem.eql(u8, fmt, "b")) {
887 base = 2;
888 case = .lower;
889 } else if (comptime std.mem.eql(u8, fmt, "x")) {
890 base = 16;
891 case = .lower;
892 } else if (comptime std.mem.eql(u8, fmt, "X")) {
893 base = 16;
894 case = .upper;
895 } else if (comptime std.mem.eql(u8, fmt, "o")) {
896 base = 8;
897 case = .lower;
898 } else {
899 invalidFmtError(fmt, value);
868 switch (fmt.len) {
869 0 => return printIntOptions(bw, int_value, 10, .lower, options),
870 1 => switch (fmt[0]) {
871 'd' => return printIntOptions(bw, int_value, 10, .lower, options),
872 'c' => {
873 if (@typeInfo(@TypeOf(int_value)).int.bits <= 8) {
874 return printAsciiChar(bw, @as(u8, int_value), options);
875 } else {
876 @compileError("cannot print integer that is larger than 8 bits as an ASCII character");
877 }
878 },
879 'u' => {
880 if (@typeInfo(@TypeOf(int_value)).int.bits <= 21) {
881 return printUnicodeCodepoint(bw, @as(u21, int_value), options);
882 } else {
883 @compileError("cannot print integer that is larger than 21 bits as an UTF-8 sequence");
884 }
885 },
886 'b' => return printIntOptions(bw, int_value, 2, .lower, options),
887 'x' => return printIntOptions(bw, int_value, 16, .lower, options),
888 'X' => return printIntOptions(bw, int_value, 16, .upper, options),
889 'o' => return printIntOptions(bw, int_value, 8, .lower, options),
890 'B' => return printByteSize(bw, int_value, .decimal, options),
891 else => invalidFmtError(fmt, value),
892 },
893 2 => {
894 if (fmt[0] == 'B' and fmt[1] == 'i') {
895 return printByteSize(bw, int_value, .binary, options);
896 } else {
897 invalidFmtError(fmt, value);
898 }
899 },
900 else => invalidFmtError(fmt, value),
900901 }
901
902 return printIntOptions(bw, int_value, base, case, options);
902 comptime unreachable;
903903}
904904
905905pub fn printAsciiChar(bw: *BufferedWriter, c: u8, options: std.fmt.Options) anyerror!void {
......@@ -1128,7 +1128,7 @@ pub const ByteSizeUnits = enum {
11281128pub fn printByteSize(
11291129 bw: *std.io.BufferedWriter,
11301130 value: u64,
1131 units: ByteSizeUnits,
1131 comptime units: ByteSizeUnits,
11321132 options: std.fmt.Options,
11331133) anyerror!void {
11341134 if (value == 0) return alignBufferOptions(bw, "0B", options);
src/link.zig+4-4
......@@ -1928,7 +1928,7 @@ fn resolveLibInput(
19281928 .root_dir = lib_directory,
19291929 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.tbd", .{lib_name}),
19301930 };
1931 try checked_paths.writer(gpa).print("\n {}", .{test_path});
1931 try checked_paths.print(gpa, "\n {}", .{test_path});
19321932 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
19331933 error.FileNotFound => break :tbd,
19341934 else => |e| fatal("unable to search for tbd library '{}': {s}", .{ test_path, @errorName(e) }),
......@@ -1947,7 +1947,7 @@ fn resolveLibInput(
19471947 },
19481948 }),
19491949 };
1950 try checked_paths.writer(gpa).print("\n {}", .{test_path});
1950 try checked_paths.print(gpa, "\n {}", .{test_path});
19511951 switch (try resolvePathInputLib(gpa, arena, unresolved_inputs, resolved_inputs, ld_script_bytes, target, .{
19521952 .path = test_path,
19531953 .query = name_query.query,
......@@ -1964,7 +1964,7 @@ fn resolveLibInput(
19641964 .root_dir = lib_directory,
19651965 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.so", .{lib_name}),
19661966 };
1967 try checked_paths.writer(gpa).print("\n {}", .{test_path});
1967 try checked_paths.print(gpa, "\n {}", .{test_path});
19681968 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
19691969 error.FileNotFound => break :so,
19701970 else => |e| fatal("unable to search for so library '{}': {s}", .{
......@@ -1982,7 +1982,7 @@ fn resolveLibInput(
19821982 .root_dir = lib_directory,
19831983 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.a", .{lib_name}),
19841984 };
1985 try checked_paths.writer(gpa).print("\n {}", .{test_path});
1985 try checked_paths.print(gpa, "\n {}", .{test_path});
19861986 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
19871987 error.FileNotFound => break :mingw,
19881988 else => |e| fatal("unable to search for static library '{}': {s}", .{ test_path, @errorName(e) }),
src/print_zoir.zig+1-3
......@@ -103,9 +103,7 @@ const PrintZon = struct {
103103
104104 fn newline(pz: *PrintZon) !void {
105105 try pz.w.writeByte('\n');
106 for (0..pz.indent) |_| {
107 try pz.w.writeByteNTimes(' ', 2);
108 }
106 try pz.w.splatByteAll(' ', 2 * pz.indent);
109107 }
110108};
111109