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(...@@ -860,46 +860,46 @@ pub fn printInt(
860 options: std.fmt.Options,860 options: std.fmt.Options,
861 value: anytype,861 value: anytype,
862) anyerror!void {862) anyerror!void {
863 comptime var base = 10;
864 comptime var case: std.fmt.Case = .lower;
865
866 const int_value = if (@TypeOf(value) == comptime_int) blk: {863 const int_value = if (@TypeOf(value) == comptime_int) blk: {
867 const Int = std.math.IntFittingRange(value, value);864 const Int = std.math.IntFittingRange(value, value);
868 break :blk @as(Int, value);865 break :blk @as(Int, value);
869 } else value;866 } else value;
870867
871 if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "d")) {868 switch (fmt.len) {
872 base = 10;869 0 => return printIntOptions(bw, int_value, 10, .lower, options),
873 case = .lower;870 1 => switch (fmt[0]) {
874 } else if (comptime std.mem.eql(u8, fmt, "c")) {871 'd' => return printIntOptions(bw, int_value, 10, .lower, options),
875 if (@typeInfo(@TypeOf(int_value)).int.bits <= 8) {872 'c' => {
876 return printAsciiChar(bw, @as(u8, int_value), options);873 if (@typeInfo(@TypeOf(int_value)).int.bits <= 8) {
877 } else {874 return printAsciiChar(bw, @as(u8, int_value), options);
878 @compileError("cannot print integer that is larger than 8 bits as an ASCII character");875 } else {
879 }876 @compileError("cannot print integer that is larger than 8 bits as an ASCII character");
880 } else if (comptime std.mem.eql(u8, fmt, "u")) {877 }
881 if (@typeInfo(@TypeOf(int_value)).int.bits <= 21) {878 },
882 return printUnicodeCodepoint(bw, @as(u21, int_value), options);879 'u' => {
883 } else {880 if (@typeInfo(@TypeOf(int_value)).int.bits <= 21) {
884 @compileError("cannot print integer that is larger than 21 bits as an UTF-8 sequence");881 return printUnicodeCodepoint(bw, @as(u21, int_value), options);
885 }882 } else {
886 } else if (comptime std.mem.eql(u8, fmt, "b")) {883 @compileError("cannot print integer that is larger than 21 bits as an UTF-8 sequence");
887 base = 2;884 }
888 case = .lower;885 },
889 } else if (comptime std.mem.eql(u8, fmt, "x")) {886 'b' => return printIntOptions(bw, int_value, 2, .lower, options),
890 base = 16;887 'x' => return printIntOptions(bw, int_value, 16, .lower, options),
891 case = .lower;888 'X' => return printIntOptions(bw, int_value, 16, .upper, options),
892 } else if (comptime std.mem.eql(u8, fmt, "X")) {889 'o' => return printIntOptions(bw, int_value, 8, .lower, options),
893 base = 16;890 'B' => return printByteSize(bw, int_value, .decimal, options),
894 case = .upper;891 else => invalidFmtError(fmt, value),
895 } else if (comptime std.mem.eql(u8, fmt, "o")) {892 },
896 base = 8;893 2 => {
897 case = .lower;894 if (fmt[0] == 'B' and fmt[1] == 'i') {
898 } else {895 return printByteSize(bw, int_value, .binary, options);
899 invalidFmtError(fmt, value);896 } else {
897 invalidFmtError(fmt, value);
898 }
899 },
900 else => invalidFmtError(fmt, value),
900 }901 }
901902 comptime unreachable;
902 return printIntOptions(bw, int_value, base, case, options);
903}903}
904904
905pub fn printAsciiChar(bw: *BufferedWriter, c: u8, options: std.fmt.Options) anyerror!void {905pub fn printAsciiChar(bw: *BufferedWriter, c: u8, options: std.fmt.Options) anyerror!void {
...@@ -1128,7 +1128,7 @@ pub const ByteSizeUnits = enum {...@@ -1128,7 +1128,7 @@ pub const ByteSizeUnits = enum {
1128pub fn printByteSize(1128pub fn printByteSize(
1129 bw: *std.io.BufferedWriter,1129 bw: *std.io.BufferedWriter,
1130 value: u64,1130 value: u64,
1131 units: ByteSizeUnits,1131 comptime units: ByteSizeUnits,
1132 options: std.fmt.Options,1132 options: std.fmt.Options,
1133) anyerror!void {1133) anyerror!void {
1134 if (value == 0) return alignBufferOptions(bw, "0B", options);1134 if (value == 0) return alignBufferOptions(bw, "0B", options);
src/link.zig+4-4
...@@ -1928,7 +1928,7 @@ fn resolveLibInput(...@@ -1928,7 +1928,7 @@ fn resolveLibInput(
1928 .root_dir = lib_directory,1928 .root_dir = lib_directory,
1929 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.tbd", .{lib_name}),1929 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.tbd", .{lib_name}),
1930 };1930 };
1931 try checked_paths.writer(gpa).print("\n {}", .{test_path});1931 try checked_paths.print(gpa, "\n {}", .{test_path});
1932 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {1932 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
1933 error.FileNotFound => break :tbd,1933 error.FileNotFound => break :tbd,
1934 else => |e| fatal("unable to search for tbd library '{}': {s}", .{ test_path, @errorName(e) }),1934 else => |e| fatal("unable to search for tbd library '{}': {s}", .{ test_path, @errorName(e) }),
...@@ -1947,7 +1947,7 @@ fn resolveLibInput(...@@ -1947,7 +1947,7 @@ fn resolveLibInput(
1947 },1947 },
1948 }),1948 }),
1949 };1949 };
1950 try checked_paths.writer(gpa).print("\n {}", .{test_path});1950 try checked_paths.print(gpa, "\n {}", .{test_path});
1951 switch (try resolvePathInputLib(gpa, arena, unresolved_inputs, resolved_inputs, ld_script_bytes, target, .{1951 switch (try resolvePathInputLib(gpa, arena, unresolved_inputs, resolved_inputs, ld_script_bytes, target, .{
1952 .path = test_path,1952 .path = test_path,
1953 .query = name_query.query,1953 .query = name_query.query,
...@@ -1964,7 +1964,7 @@ fn resolveLibInput(...@@ -1964,7 +1964,7 @@ fn resolveLibInput(
1964 .root_dir = lib_directory,1964 .root_dir = lib_directory,
1965 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.so", .{lib_name}),1965 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.so", .{lib_name}),
1966 };1966 };
1967 try checked_paths.writer(gpa).print("\n {}", .{test_path});1967 try checked_paths.print(gpa, "\n {}", .{test_path});
1968 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {1968 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
1969 error.FileNotFound => break :so,1969 error.FileNotFound => break :so,
1970 else => |e| fatal("unable to search for so library '{}': {s}", .{1970 else => |e| fatal("unable to search for so library '{}': {s}", .{
...@@ -1982,7 +1982,7 @@ fn resolveLibInput(...@@ -1982,7 +1982,7 @@ fn resolveLibInput(
1982 .root_dir = lib_directory,1982 .root_dir = lib_directory,
1983 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.a", .{lib_name}),1983 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.a", .{lib_name}),
1984 };1984 };
1985 try checked_paths.writer(gpa).print("\n {}", .{test_path});1985 try checked_paths.print(gpa, "\n {}", .{test_path});
1986 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {1986 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
1987 error.FileNotFound => break :mingw,1987 error.FileNotFound => break :mingw,
1988 else => |e| fatal("unable to search for static library '{}': {s}", .{ test_path, @errorName(e) }),1988 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 {...@@ -103,9 +103,7 @@ const PrintZon = struct {
103103
104 fn newline(pz: *PrintZon) !void {104 fn newline(pz: *PrintZon) !void {
105 try pz.w.writeByte('\n');105 try pz.w.writeByte('\n');
106 for (0..pz.indent) |_| {106 try pz.w.splatByteAll(' ', 2 * pz.indent);
107 try pz.w.writeByteNTimes(' ', 2);
108 }
109 }107 }
110};108};
111109