authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-04-10 22:22:56-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-01 16:35:26-07:00
log60854795b85d075ffc5418f9009c832f6a06a84c
tree02f69ffcb3b1a81ef81312f007916d84923056cc
parent646454beb5026ba50ae6bf4d75d018d1c67fe77a

array list tests passing again


7 files changed, 187 insertions(+), 234 deletions(-)

lib/std/Uri.zig+22-27
......@@ -40,17 +40,16 @@ pub const Component = union(enum) {
4040 };
4141 }
4242
43 pub fn format(component: Component, bw: *std.io.BufferedWriter, comptime fmt: []const u8) anyerror!usize {
44 var n: usize = 0;
43 pub fn format(component: Component, bw: *std.io.BufferedWriter, comptime fmt: []const u8) anyerror!void {
4544 if (fmt.len == 0) {
46 n += try bw.printCount("std.Uri.Component{{ .{s} = \"{}\" }}", .{
45 try bw.print("std.Uri.Component{{ .{s} = \"{}\" }}", .{
4746 @tagName(component),
4847 std.zig.fmtEscapes(switch (component) {
4948 .raw, .percent_encoded => |string| string,
5049 }),
5150 });
5251 } else if (comptime std.mem.eql(u8, fmt, "raw")) switch (component) {
53 .raw => |raw| n += try bw.writeAllCount(raw),
52 .raw => |raw| try bw.writeAll(raw),
5453 .percent_encoded => |percent_encoded| {
5554 var start: usize = 0;
5655 var index: usize = 0;
......@@ -59,55 +58,51 @@ pub const Component = union(enum) {
5958 if (percent_encoded.len - index < 2) continue;
6059 const percent_encoded_char =
6160 std.fmt.parseInt(u8, percent_encoded[index..][0..2], 16) catch continue;
62 n += try bw.printCount("{s}{c}", .{
61 try bw.print("{s}{c}", .{
6362 percent_encoded[start..percent],
6463 percent_encoded_char,
6564 });
6665 start = percent + 3;
6766 index = percent + 3;
6867 }
69 n += try bw.writeAllCount(percent_encoded[start..]);
68 try bw.writeAll(percent_encoded[start..]);
7069 },
7170 } else if (comptime std.mem.eql(u8, fmt, "%")) switch (component) {
72 .raw => |raw| n += try percentEncode(bw, raw, isUnreserved),
73 .percent_encoded => |percent_encoded| n += try bw.writeAllCount(percent_encoded),
71 .raw => |raw| try percentEncode(bw, raw, isUnreserved),
72 .percent_encoded => |percent_encoded| try bw.writeAll(percent_encoded),
7473 } else if (comptime std.mem.eql(u8, fmt, "user")) switch (component) {
75 .raw => |raw| n += try percentEncode(bw, raw, isUserChar),
76 .percent_encoded => |percent_encoded| n += try bw.writeAllCount(percent_encoded),
74 .raw => |raw| try percentEncode(bw, raw, isUserChar),
75 .percent_encoded => |percent_encoded| try bw.writeAll(percent_encoded),
7776 } else if (comptime std.mem.eql(u8, fmt, "password")) switch (component) {
78 .raw => |raw| n += try percentEncode(bw, raw, isPasswordChar),
79 .percent_encoded => |percent_encoded| n += try bw.writeAllCount(percent_encoded),
77 .raw => |raw| try percentEncode(bw, raw, isPasswordChar),
78 .percent_encoded => |percent_encoded| try bw.writeAll(percent_encoded),
8079 } else if (comptime std.mem.eql(u8, fmt, "host")) switch (component) {
81 .raw => |raw| n += try percentEncode(bw, raw, isHostChar),
82 .percent_encoded => |percent_encoded| n += try bw.writeAllCount(percent_encoded),
80 .raw => |raw| try percentEncode(bw, raw, isHostChar),
81 .percent_encoded => |percent_encoded| try bw.writeAll(percent_encoded),
8382 } else if (comptime std.mem.eql(u8, fmt, "path")) switch (component) {
84 .raw => |raw| n += try percentEncode(bw, raw, isPathChar),
85 .percent_encoded => |percent_encoded| n += try bw.writeAllCount(percent_encoded),
83 .raw => |raw| try percentEncode(bw, raw, isPathChar),
84 .percent_encoded => |percent_encoded| try bw.writeAll(percent_encoded),
8685 } else if (comptime std.mem.eql(u8, fmt, "query")) switch (component) {
87 .raw => |raw| n += try percentEncode(bw, raw, isQueryChar),
88 .percent_encoded => |percent_encoded| n += try bw.writeAllCount(percent_encoded),
86 .raw => |raw| try percentEncode(bw, raw, isQueryChar),
87 .percent_encoded => |percent_encoded| try bw.writeAll(percent_encoded),
8988 } else if (comptime std.mem.eql(u8, fmt, "fragment")) switch (component) {
90 .raw => |raw| n += try percentEncode(bw, raw, isFragmentChar),
91 .percent_encoded => |percent_encoded| n += try bw.writeAllCount(percent_encoded),
89 .raw => |raw| try percentEncode(bw, raw, isFragmentChar),
90 .percent_encoded => |percent_encoded| try bw.writeAll(percent_encoded),
9291 } else @compileError("invalid format string '" ++ fmt ++ "'");
93
94 return n;
9592 }
9693
9794 pub fn percentEncode(
9895 bw: *std.io.BufferedWriter,
9996 raw: []const u8,
10097 comptime isValidChar: fn (u8) bool,
101 ) anyerror!usize {
102 var n: usize = 0;
98 ) anyerror!void {
10399 var start: usize = 0;
104100 for (raw, 0..) |char, index| {
105101 if (isValidChar(char)) continue;
106 n += try bw.printCount("{s}%{X:0>2}", .{ raw[start..index], char });
102 try bw.print("{s}%{X:0>2}", .{ raw[start..index], char });
107103 start = index + 1;
108104 }
109 n += try bw.writeAllCount(raw[start..]);
110 return n;
105 try bw.writeAll(raw[start..]);
111106 }
112107};
113108
lib/std/builtin.zig+5-7
......@@ -34,8 +34,8 @@ pub const StackTrace = struct {
3434 index: usize,
3535 instruction_addresses: []usize,
3636
37 pub fn format(st: StackTrace, bw: *std.io.BufferedWriter, comptime fmt: []const u8) anyerror!usize {
38 comptime std.debug.assert(fmt.len == 0);
37 pub fn format(st: StackTrace, bw: *std.io.BufferedWriter, comptime fmt: []const u8) anyerror!void {
38 comptime if (fmt.len != 0) unreachable;
3939
4040 // TODO: re-evaluate whether to use format() methods at all.
4141 // Until then, avoid an error when using DebugAllocator with WebAssembly
......@@ -43,17 +43,15 @@ pub const StackTrace = struct {
4343 if (builtin.os.tag == .freestanding) return 0;
4444
4545 const debug_info = std.debug.getSelfDebugInfo() catch |err| {
46 return bw.printCount("\nUnable to print stack trace: Unable to open debug info: {s}\n", .{
46 return bw.print("\nUnable to print stack trace: Unable to open debug info: {s}\n", .{
4747 @errorName(err),
4848 });
4949 };
5050 const tty_config = std.io.tty.detectConfig(.stderr());
51 var n: usize = 0;
52 n += try bw.writeAllCount("\n");
53 n += std.debug.writeStackTrace(st, bw, debug_info, tty_config) catch |err| {
51 try bw.writeAll("\n");
52 std.debug.writeStackTrace(st, bw, debug_info, tty_config) catch |err| {
5453 try bw.print("Unable to print stack trace: {s}\n", .{@errorName(err)});
5554 };
56 return n;
5755 }
5856};
5957
lib/std/debug.zig+6-13
......@@ -733,28 +733,26 @@ pub fn writeStackTrace(
733733 writer: *std.io.BufferedWriter,
734734 debug_info: *SelfInfo,
735735 tty_config: io.tty.Config,
736) !usize {
736) !void {
737737 if (builtin.strip_debug_info) return error.MissingDebugInfo;
738738 var frame_index: usize = 0;
739739 var frames_left: usize = @min(stack_trace.index, stack_trace.instruction_addresses.len);
740 var n: usize = 0;
741740
742741 while (frames_left != 0) : ({
743742 frames_left -= 1;
744743 frame_index = (frame_index + 1) % stack_trace.instruction_addresses.len;
745744 }) {
746745 const return_address = stack_trace.instruction_addresses[frame_index];
747 n += try printSourceAtAddress(debug_info, writer, return_address - 1, tty_config);
746 try printSourceAtAddress(debug_info, writer, return_address - 1, tty_config);
748747 }
749748
750749 if (stack_trace.index > stack_trace.instruction_addresses.len) {
751750 const dropped_frames = stack_trace.index - stack_trace.instruction_addresses.len;
752751
753 n += tty_config.setColor(writer, .bold) catch {};
754 n += try writer.printCount("({d} additional stack frames skipped...)\n", .{dropped_frames});
755 n += tty_config.setColor(writer, .reset) catch {};
752 tty_config.setColor(writer, .bold) catch {};
753 try writer.print("({d} additional stack frames skipped...)\n", .{dropped_frames});
754 tty_config.setColor(writer, .reset) catch {};
756755 }
757 return n;
758756}
759757
760758pub const UnwindError = if (have_ucontext)
......@@ -1102,12 +1100,7 @@ fn printUnwindError(debug_info: *SelfInfo, writer: *std.io.BufferedWriter, addre
11021100 try tty_config.setColor(writer, .reset);
11031101}
11041102
1105pub fn printSourceAtAddress(
1106 debug_info: *SelfInfo,
1107 writer: *std.io.BufferedWriter,
1108 address: usize,
1109 tty_config: io.tty.Config,
1110) !void {
1103pub fn printSourceAtAddress(debug_info: *SelfInfo, writer: *std.io.BufferedWriter, address: usize, tty_config: io.tty.Config) !void {
11111104 const module = debug_info.getModuleForAddress(address) catch |err| switch (err) {
11121105 error.MissingDebugInfo, error.InvalidDebugInfo => return printUnknownSource(debug_info, writer, address, tty_config),
11131106 else => return err,
lib/std/fmt.zig+5-7
......@@ -91,7 +91,7 @@ pub const Options = struct {
9191/// A user type may be a `struct`, `vector`, `union` or `enum` type.
9292///
9393/// To print literal curly braces, escape them by writing them twice, e.g. `{{` or `}}`.
94pub fn format(bw: *std.io.BufferedWriter, comptime fmt: []const u8, args: anytype) anyerror!usize {
94pub fn format(bw: *std.io.BufferedWriter, comptime fmt: []const u8, args: anytype) anyerror!void {
9595 const ArgsType = @TypeOf(args);
9696 const args_type_info = @typeInfo(ArgsType);
9797 if (args_type_info != .@"struct") {
......@@ -107,7 +107,6 @@ pub fn format(bw: *std.io.BufferedWriter, comptime fmt: []const u8, args: anytyp
107107 comptime var arg_state: ArgState = .{ .args_len = fields_info.len };
108108 comptime var i = 0;
109109 comptime var literal: []const u8 = "";
110 var bytes_written: usize = 0;
111110 inline while (true) {
112111 const start_index = i;
113112
......@@ -137,7 +136,7 @@ pub fn format(bw: *std.io.BufferedWriter, comptime fmt: []const u8, args: anytyp
137136
138137 // Write out the literal
139138 if (literal.len != 0) {
140 bytes_written += try bw.writeAllCount(literal);
139 try bw.writeAll(literal);
141140 literal = "";
142141 }
143142
......@@ -197,7 +196,7 @@ pub fn format(bw: *std.io.BufferedWriter, comptime fmt: []const u8, args: anytyp
197196 const arg_to_print = comptime arg_state.nextArg(arg_pos) orelse
198197 @compileError("too few arguments");
199198
200 bytes_written += try bw.printValue(
199 try bw.printValue(
201200 placeholder.specifier_arg,
202201 .{
203202 .fill = placeholder.fill,
......@@ -218,8 +217,6 @@ pub fn format(bw: *std.io.BufferedWriter, comptime fmt: []const u8, args: anytyp
218217 else => @compileError(comptimePrint("{d}", .{missing_count}) ++ " unused arguments in '" ++ fmt ++ "'"),
219218 }
220219 }
221
222 return bytes_written;
223220}
224221
225222fn cacheString(str: anytype) []const u8 {
......@@ -858,7 +855,8 @@ pub fn bufPrintZ(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintErr
858855pub fn count(comptime fmt: []const u8, args: anytype) usize {
859856 var buffer: [std.atomic.cache_line]u8 = undefined;
860857 var bw = std.io.Writer.null.buffered(&buffer);
861 return bw.printCount(fmt, args) catch unreachable;
858 bw.print(fmt, args) catch unreachable;
859 return bw.bytes_written;
862860}
863861
864862pub const AllocPrintError = error{OutOfMemory};
lib/std/io/BufferedWriter.zig+135-163
......@@ -19,6 +19,10 @@ buffer: std.ArrayListUnmanaged(u8),
1919/// equals number of bytes provided. This property is exploited by
2020/// `std.io.AllocatingWriter` for example.
2121unbuffered_writer: Writer,
22/// Tracks total number of bytes written to this `BufferedWriter`. This value
23/// only increases. In the case of fixed mode, this value always equals
24/// `buffer.items.len`.
25bytes_written: usize = 0,
2226
2327/// Number of slices to store on the stack, when trying to send as many byte
2428/// vectors through the underlying write calls as possible.
......@@ -106,6 +110,7 @@ pub fn advance(bw: *BufferedWriter, n: usize) void {
106110 const list = &bw.buffer;
107111 list.items.len += n;
108112 assert(list.items.len <= list.capacity);
113 bw.bytes_written += n;
109114}
110115
111116/// The `data` parameter is mutable because this function needs to mutate the
......@@ -114,8 +119,9 @@ pub fn writevAll(bw: *BufferedWriter, data: [][]const u8) anyerror!void {
114119 var i: usize = 0;
115120 while (true) {
116121 var n = try passthru_writeSplat(bw, data[i..], 1);
117 while (n >= data[i].len) {
118 n -= data[i].len;
122 const len = data[i].len;
123 while (n >= len) {
124 n -= len;
119125 i += 1;
120126 if (i >= data.len) return;
121127 }
......@@ -147,7 +153,7 @@ fn passthru_writeSplat(context: ?*anyopaque, data: []const []const u8, splat: us
147153 end = new_end;
148154 continue;
149155 }
150 if (end == 0) return bw.unbuffered_writer.writeSplat(data, splat);
156 if (end == 0) return track(&bw.bytes_written, try bw.unbuffered_writer.writeSplat(data, splat));
151157 buffers[0] = buffer[0..end];
152158 const remaining_data = data[i..];
153159 const remaining_buffers = buffers[1..];
......@@ -163,10 +169,10 @@ fn passthru_writeSplat(context: ?*anyopaque, data: []const []const u8, splat: us
163169 const remainder = buffer[n..end];
164170 std.mem.copyForwards(u8, buffer[0..remainder.len], remainder);
165171 list.items.len = remainder.len;
166 return end - start_end;
172 return track(&bw.bytes_written, end - start_end);
167173 }
168174 list.items.len = 0;
169 return n - start_end;
175 return track(&bw.bytes_written, n - start_end);
170176 }
171177 const n = try bw.unbuffered_writer.writeSplat(send_buffers, 1);
172178 if (n < end) {
......@@ -174,10 +180,10 @@ fn passthru_writeSplat(context: ?*anyopaque, data: []const []const u8, splat: us
174180 const remainder = buffer[n..end];
175181 std.mem.copyForwards(u8, buffer[0..remainder.len], remainder);
176182 list.items.len = remainder.len;
177 return end - start_end;
183 return track(&bw.bytes_written, end - start_end);
178184 }
179185 list.items.len = 0;
180 return n - start_end;
186 return track(&bw.bytes_written, n - start_end);
181187 }
182188
183189 const pattern = data[data.len - 1];
......@@ -187,7 +193,7 @@ fn passthru_writeSplat(context: ?*anyopaque, data: []const []const u8, splat: us
187193 // It was added in the loop above; undo it here.
188194 end -= pattern.len;
189195 list.items.len = end;
190 return end - start_end;
196 return track(&bw.bytes_written, end - start_end);
191197 }
192198
193199 const remaining_splat = splat - 1;
......@@ -195,7 +201,7 @@ fn passthru_writeSplat(context: ?*anyopaque, data: []const []const u8, splat: us
195201 switch (pattern.len) {
196202 0 => {
197203 list.items.len = end;
198 return end - start_end;
204 return track(&bw.bytes_written, end - start_end);
199205 },
200206 1 => {
201207 const new_end = end + remaining_splat;
......@@ -203,7 +209,7 @@ fn passthru_writeSplat(context: ?*anyopaque, data: []const []const u8, splat: us
203209 @branchHint(.likely);
204210 @memset(buffer[end..new_end], pattern[0]);
205211 list.items.len = new_end;
206 return new_end - start_end;
212 return track(&bw.bytes_written, new_end - start_end);
207213 }
208214 buffers[0] = buffer[0..end];
209215 buffers[1] = pattern;
......@@ -213,10 +219,10 @@ fn passthru_writeSplat(context: ?*anyopaque, data: []const []const u8, splat: us
213219 const remainder = buffer[n..end];
214220 std.mem.copyForwards(u8, buffer[0..remainder.len], remainder);
215221 list.items.len = remainder.len;
216 return end - start_end;
222 return track(&bw.bytes_written, end - start_end);
217223 }
218224 list.items.len = 0;
219 return n - start_end;
225 return track(&bw.bytes_written, n - start_end);
220226 },
221227 else => {
222228 const new_end = end + pattern.len * remaining_splat;
......@@ -226,7 +232,7 @@ fn passthru_writeSplat(context: ?*anyopaque, data: []const []const u8, splat: us
226232 @memcpy(buffer[end..][0..pattern.len], pattern);
227233 }
228234 list.items.len = new_end;
229 return new_end - start_end;
235 return track(&bw.bytes_written, new_end - start_end);
230236 }
231237 buffers[0] = buffer[0..end];
232238 buffers[1] = pattern;
......@@ -236,14 +242,19 @@ fn passthru_writeSplat(context: ?*anyopaque, data: []const []const u8, splat: us
236242 const remainder = buffer[n..end];
237243 std.mem.copyForwards(u8, buffer[0..remainder.len], remainder);
238244 list.items.len = remainder.len;
239 return end - start_end;
245 return track(&bw.bytes_written, end - start_end);
240246 }
241247 list.items.len = 0;
242 return n - start_end;
248 return track(&bw.bytes_written, n - start_end);
243249 },
244250 }
245251}
246252
253fn track(bytes_written: *usize, n: usize) usize {
254 bytes_written.* += n;
255 return n;
256}
257
247258/// When this function is called it means the buffer got full, so it's time
248259/// to return an error. However, we still need to make sure all of the
249260/// available buffer has been filled.
......@@ -256,6 +267,7 @@ fn fixed_writeSplat(context: ?*anyopaque, data: []const []const u8, splat: usize
256267 const len = @min(bytes.len, dest.len);
257268 @memcpy(dest[0..len], bytes[0..len]);
258269 list.items.len += len;
270 bw.bytes_written = list.items.len;
259271 }
260272 const pattern = data[data.len - 1];
261273 const dest = list.unusedCapacitySlice();
......@@ -265,6 +277,7 @@ fn fixed_writeSplat(context: ?*anyopaque, data: []const []const u8, splat: usize
265277 else => for (0..splat - 1) |i| @memcpy(dest[i * pattern.len ..][0..pattern.len], pattern),
266278 }
267279 list.items.len = list.capacity;
280 bw.bytes_written = list.items.len;
268281 return error.NoSpaceLeft;
269282}
270283
......@@ -284,17 +297,11 @@ pub fn write(bw: *BufferedWriter, bytes: []const u8) anyerror!usize {
284297 return 0;
285298 }
286299 list.items.len = 0;
287 return n - end;
300 return track(&bw.bytes_written, n - end);
288301 }
289302 @memcpy(buffer[end..new_end], bytes);
290303 list.items.len = new_end;
291 return bytes.len;
292}
293
294/// Convenience function that calls `writeAll` and then returns `bytes.len`.
295pub fn writeAllCount(bw: *BufferedWriter, bytes: []const u8) anyerror!usize {
296 try writeAll(bw, bytes);
297 return bytes.len;
304 return track(&bw.bytes_written, bytes.len);
298305}
299306
300307/// Calls `write` as many times as necessary such that all of `bytes` are
......@@ -305,17 +312,7 @@ pub fn writeAll(bw: *BufferedWriter, bytes: []const u8) anyerror!void {
305312}
306313
307314pub fn print(bw: *BufferedWriter, comptime format: []const u8, args: anytype) anyerror!void {
308 _ = try std.fmt.format(bw, format, args);
309}
310
311pub fn printCount(bw: *BufferedWriter, comptime format: []const u8, args: anytype) anyerror!usize {
312 return std.fmt.format(bw, format, args);
313}
314
315/// Returns 0 or 1 indicating how many bytes were written.
316pub fn writeByteCount(bw: *BufferedWriter, byte: u8) anyerror!usize {
317 try writeByte(bw, byte);
318 return 1;
315 try std.fmt.format(bw, format, args);
319316}
320317
321318pub fn writeByte(bw: *BufferedWriter, byte: u8) anyerror!void {
......@@ -325,6 +322,7 @@ pub fn writeByte(bw: *BufferedWriter, byte: u8) anyerror!void {
325322 @branchHint(.likely);
326323 buffer.ptr[buffer.len] = byte;
327324 list.items.len = buffer.len + 1;
325 bw.bytes_written += 1;
328326 return;
329327 }
330328 var buffers: [2][]const u8 = .{ buffer, &.{byte} };
......@@ -333,7 +331,9 @@ pub fn writeByte(bw: *BufferedWriter, byte: u8) anyerror!void {
333331 if (n == 0) {
334332 @branchHint(.unlikely);
335333 continue;
336 } else if (n >= buffer.len) {
334 }
335 bw.bytes_written += 1;
336 if (n >= buffer.len) {
337337 @branchHint(.likely);
338338 if (n > buffer.len) {
339339 @branchHint(.likely);
......@@ -353,12 +353,6 @@ pub fn writeByte(bw: *BufferedWriter, byte: u8) anyerror!void {
353353 }
354354}
355355
356/// Convenience function that calls `splatByteAll` and then returns `n`.
357pub fn splatByteAllCount(bw: *BufferedWriter, byte: u8, n: usize) anyerror!usize {
358 try splatByteAll(bw, byte, n);
359 return n;
360}
361
362356/// Writes the same byte many times, performing the underlying write call as
363357/// many times as necessary.
364358pub fn splatByteAll(bw: *BufferedWriter, byte: u8, n: usize) anyerror!void {
......@@ -438,7 +432,10 @@ fn passthru_writeFile(
438432 const bw: *BufferedWriter = @alignCast(@ptrCast(context));
439433 const list = &bw.buffer;
440434 const buffer = list.allocatedSlice();
441 if (buffer.len == 0) return bw.unbuffered_writer.writeFile(file, offset, len, headers_and_trailers, headers_len);
435 if (buffer.len == 0) return track(
436 &bw.bytes_written,
437 try bw.unbuffered_writer.writeFile(file, offset, len, headers_and_trailers, headers_len),
438 );
442439 const start_end = list.items.len;
443440 const headers = headers_and_trailers[0..headers_len];
444441 const trailers = headers_and_trailers[headers_len..];
......@@ -470,10 +467,10 @@ fn passthru_writeFile(
470467 const remainder = buffer[n..end];
471468 std.mem.copyForwards(u8, buffer[0..remainder.len], remainder);
472469 list.items.len = remainder.len;
473 return end - start_end;
470 return track(&bw.bytes_written, end - start_end);
474471 }
475472 list.items.len = 0;
476 return n - start_end;
473 return track(&bw.bytes_written, n - start_end);
477474 }
478475 // Have not made it past the headers yet; must call `writev`.
479476 const n = try bw.unbuffered_writer.writev(buffers[0 .. buffers_len + 1]);
......@@ -482,10 +479,10 @@ fn passthru_writeFile(
482479 const remainder = buffer[n..end];
483480 std.mem.copyForwards(u8, buffer[0..remainder.len], remainder);
484481 list.items.len = remainder.len;
485 return end - start_end;
482 return track(&bw.bytes_written, end - start_end);
486483 }
487484 list.items.len = 0;
488 return n - start_end;
485 return track(&bw.bytes_written, n - start_end);
489486 }
490487 // All headers written to buffer.
491488 buffers[0] = buffer[0..end];
......@@ -500,10 +497,10 @@ fn passthru_writeFile(
500497 const remainder = buffer[n..end];
501498 std.mem.copyForwards(u8, buffer[0..remainder.len], remainder);
502499 list.items.len = remainder.len;
503 return end - start_end;
500 return track(&bw.bytes_written, end - start_end);
504501 }
505502 list.items.len = 0;
506 return n - start_end;
503 return track(&bw.bytes_written, n - start_end);
507504}
508505
509506pub const WriteFileOptions = struct {
......@@ -583,54 +580,51 @@ pub fn alignBuffer(
583580 width: usize,
584581 alignment: std.fmt.Alignment,
585582 fill: u8,
586) anyerror!usize {
583) anyerror!void {
587584 const padding = if (buffer.len < width) width - buffer.len else 0;
588585 if (padding == 0) {
589586 @branchHint(.likely);
590 return bw.writeAllCount(buffer);
587 return bw.writeAll(buffer);
591588 }
592 var n: usize = 0;
593589 switch (alignment) {
594590 .left => {
595 n += try bw.writeAllCount(buffer);
596 n += try bw.splatByteAllCount(fill, padding);
591 try bw.writeAll(buffer);
592 try bw.splatByteAll(fill, padding);
597593 },
598594 .center => {
599595 const left_padding = padding / 2;
600596 const right_padding = (padding + 1) / 2;
601 n += try bw.splatByteAllCount(fill, left_padding);
602 n += try bw.writeAllCount(buffer);
603 n += try bw.splatByteAllCount(fill, right_padding);
597 try bw.splatByteAll(fill, left_padding);
598 try bw.writeAll(buffer);
599 try bw.splatByteAll(fill, right_padding);
604600 },
605601 .right => {
606 n += try bw.splatByteAllCount(fill, padding);
607 n += try bw.writeAllCount(buffer);
602 try bw.splatByteAll(fill, padding);
603 try bw.writeAll(buffer);
608604 },
609605 }
610 return n;
611606}
612607
613pub fn alignBufferOptions(bw: *BufferedWriter, buffer: []const u8, options: std.fmt.Options) anyerror!usize {
608pub fn alignBufferOptions(bw: *BufferedWriter, buffer: []const u8, options: std.fmt.Options) anyerror!void {
614609 return alignBuffer(bw, buffer, options.width orelse buffer.len, options.alignment, options.fill);
615610}
616611
617pub fn printAddress(bw: *BufferedWriter, value: anytype) anyerror!usize {
612pub fn printAddress(bw: *BufferedWriter, value: anytype) anyerror!void {
618613 const T = @TypeOf(value);
619 var n: usize = 0;
620614 switch (@typeInfo(T)) {
621615 .pointer => |info| {
622 n += try bw.writeAllCount(@typeName(info.child) ++ "@");
616 try bw.writeAll(@typeName(info.child) ++ "@");
623617 if (info.size == .slice)
624 n += try printIntOptions(bw, @intFromPtr(value.ptr), 16, .lower, .{})
618 try printIntOptions(bw, @intFromPtr(value.ptr), 16, .lower, .{})
625619 else
626 n += try printIntOptions(bw, @intFromPtr(value), 16, .lower, .{});
627 return n;
620 try printIntOptions(bw, @intFromPtr(value), 16, .lower, .{});
621 return;
628622 },
629623 .optional => |info| {
630624 if (@typeInfo(info.child) == .pointer) {
631 n += try bw.writeAll(@typeName(info.child) ++ "@");
632 n += try printIntOptions(bw, @intFromPtr(value), 16, .lower, .{});
633 return n;
625 try bw.writeAll(@typeName(info.child) ++ "@");
626 try printIntOptions(bw, @intFromPtr(value), 16, .lower, .{});
627 return;
634628 }
635629 },
636630 else => {},
......@@ -645,7 +639,7 @@ pub fn printValue(
645639 options: std.fmt.Options,
646640 value: anytype,
647641 max_depth: usize,
648) anyerror!usize {
642) anyerror!void {
649643 const T = @TypeOf(value);
650644 const actual_fmt = comptime if (std.mem.eql(u8, fmt, ANY))
651645 defaultFormatString(T)
......@@ -700,104 +694,96 @@ pub fn printValue(
700694 },
701695 .error_set => {
702696 if (actual_fmt.len > 0 and actual_fmt[0] == 's') {
703 return bw.writeAllCount(@errorName(value));
697 return bw.writeAll(@errorName(value));
704698 } else if (actual_fmt.len != 0) {
705699 invalidFmtError(fmt, value);
706700 } else {
707 var n: usize = 0;
708 n += try bw.writeAllCount("error.");
709 n += try bw.writeAllCount(@errorName(value));
710 return n;
701 try bw.writeAll("error.");
702 try bw.writeAll(@errorName(value));
711703 }
712704 },
713705 .@"enum" => |enum_info| {
714 var n: usize = 0;
715 n += try bw.writeAllCount(@typeName(T));
706 try bw.writeAll(@typeName(T));
716707 if (enum_info.is_exhaustive) {
717708 if (actual_fmt.len != 0) invalidFmtError(fmt, value);
718 n += try bw.writeAllCount(".");
719 n += try bw.writeAllCount(@tagName(value));
720 return n;
709 try bw.writeAll(".");
710 try bw.writeAll(@tagName(value));
711 return;
721712 }
722713
723714 // Use @tagName only if value is one of known fields
724715 @setEvalBranchQuota(3 * enum_info.fields.len);
725716 inline for (enum_info.fields) |enumField| {
726717 if (@intFromEnum(value) == enumField.value) {
727 n += try bw.writeAllCount(".");
728 n += try bw.writeAllCount(@tagName(value));
718 try bw.writeAll(".");
719 try bw.writeAll(@tagName(value));
729720 return;
730721 }
731722 }
732723
733 n += try bw.writeByteCount('(');
734 n += try printValue(bw, actual_fmt, options, @intFromEnum(value), max_depth);
735 n += try bw.writeByteCount(')');
736 return n;
724 try bw.writeByteCount('(');
725 try printValue(bw, actual_fmt, options, @intFromEnum(value), max_depth);
726 try bw.writeByteCount(')');
737727 },
738728 .@"union" => |info| {
739729 if (actual_fmt.len != 0) invalidFmtError(fmt, value);
740 var n: usize = 0;
741 n += try bw.writeAllCount(@typeName(T));
730 try bw.writeAll(@typeName(T));
742731 if (max_depth == 0) {
743 n += bw.writeAllCount("{ ... }");
744 return n;
732 bw.writeAll("{ ... }");
733 return;
745734 }
746735 if (info.tag_type) |UnionTagType| {
747 n += try bw.writeAllCount("{ .");
748 n += try bw.writeAllCount(@tagName(@as(UnionTagType, value)));
749 n += try bw.writeAllCount(" = ");
736 try bw.writeAll("{ .");
737 try bw.writeAll(@tagName(@as(UnionTagType, value)));
738 try bw.writeAll(" = ");
750739 inline for (info.fields) |u_field| {
751740 if (value == @field(UnionTagType, u_field.name)) {
752 n += try printValue(bw, ANY, options, @field(value, u_field.name), max_depth - 1);
741 try printValue(bw, ANY, options, @field(value, u_field.name), max_depth - 1);
753742 }
754743 }
755 n += try bw.writeAllCount(" }");
744 try bw.writeAll(" }");
756745 } else {
757 n += try bw.writeByte('@');
758 n += try bw.printIntOptions(@intFromPtr(&value), 16, .lower);
746 try bw.writeByte('@');
747 try bw.printIntOptions(@intFromPtr(&value), 16, .lower);
759748 }
760 return n;
761749 },
762750 .@"struct" => |info| {
763751 if (actual_fmt.len != 0) invalidFmtError(fmt, value);
764 var n: usize = 0;
765752 if (info.is_tuple) {
766753 // Skip the type and field names when formatting tuples.
767754 if (max_depth == 0) {
768 n += try bw.writeAllCount("{ ... }");
769 return n;
755 try bw.writeAll("{ ... }");
756 return;
770757 }
771 n += try bw.writeAllCount("{");
758 try bw.writeAll("{");
772759 inline for (info.fields, 0..) |f, i| {
773760 if (i == 0) {
774 n += try bw.writeAllCount(" ");
761 try bw.writeAll(" ");
775762 } else {
776 n += try bw.writeAllCount(", ");
763 try bw.writeAll(", ");
777764 }
778 n += try printValue(bw, ANY, options, @field(value, f.name), max_depth - 1);
765 try printValue(bw, ANY, options, @field(value, f.name), max_depth - 1);
779766 }
780 n += try bw.writeAllCount(" }");
781 return n;
767 try bw.writeAll(" }");
768 return;
782769 }
783 n += try bw.writeAllCount(@typeName(T));
770 try bw.writeAll(@typeName(T));
784771 if (max_depth == 0) {
785 n += try bw.writeAllCount("{ ... }");
786 return n;
772 try bw.writeAll("{ ... }");
773 return;
787774 }
788 n += try bw.writeAllCount("{");
775 try bw.writeAll("{");
789776 inline for (info.fields, 0..) |f, i| {
790777 if (i == 0) {
791 n += try bw.writeAllCount(" .");
778 try bw.writeAll(" .");
792779 } else {
793 n += try bw.writeAllCount(", .");
780 try bw.writeAll(", .");
794781 }
795 n += try bw.writeAllCount(f.name);
796 n += try bw.writeAllCount(" = ");
797 n += try printValue(bw, ANY, options, @field(value, f.name), max_depth - 1);
782 try bw.writeAll(f.name);
783 try bw.writeAll(" = ");
784 try printValue(bw, ANY, options, @field(value, f.name), max_depth - 1);
798785 }
799 n += try bw.writeAllCount(" }");
800 return n;
786 try bw.writeAll(" }");
801787 },
802788 .pointer => |ptr_info| switch (ptr_info.size) {
803789 .one => switch (@typeInfo(ptr_info.child)) {
......@@ -806,10 +792,9 @@ pub fn printValue(
806792 },
807793 else => {
808794 var buffers: [2][]const u8 = .{ @typeName(ptr_info.child), "@" };
809 var n: usize = 0;
810 n += try writevAll(bw, &buffers);
811 n += try printIntOptions(bw, @intFromPtr(value), 16, .lower, options);
812 return n;
795 try writevAll(bw, &buffers);
796 try printIntOptions(bw, @intFromPtr(value), 16, .lower, options);
797 return;
813798 },
814799 },
815800 .many, .c => {
......@@ -827,7 +812,7 @@ pub fn printValue(
827812 if (actual_fmt.len == 0)
828813 @compileError("cannot format slice without a specifier (i.e. {s}, {x}, {b64}, or {any})");
829814 if (max_depth == 0) {
830 return bw.writeAllCount("{ ... }");
815 return bw.writeAll("{ ... }");
831816 }
832817 if (ptr_info.child == u8) switch (actual_fmt.len) {
833818 1 => switch (actual_fmt[0]) {
......@@ -841,23 +826,21 @@ pub fn printValue(
841826 },
842827 else => {},
843828 };
844 var n: usize = 0;
845 n += try bw.writeAllCount("{ ");
829 try bw.writeAll("{ ");
846830 for (value, 0..) |elem, i| {
847 n += try printValue(bw, actual_fmt, options, elem, max_depth - 1);
831 try printValue(bw, actual_fmt, options, elem, max_depth - 1);
848832 if (i != value.len - 1) {
849 n += try bw.writeAllCount(", ");
833 try bw.writeAll(", ");
850834 }
851835 }
852 n += try bw.writeAllCount(" }");
853 return n;
836 try bw.writeAll(" }");
854837 },
855838 },
856839 .array => |info| {
857840 if (actual_fmt.len == 0)
858841 @compileError("cannot format array without a specifier (i.e. {s} or {any})");
859842 if (max_depth == 0) {
860 return bw.writeAllCount("{ ... }");
843 return bw.writeAll("{ ... }");
861844 }
862845 if (info.child == u8) {
863846 if (actual_fmt[0] == 's') {
......@@ -868,32 +851,28 @@ pub fn printValue(
868851 return printHex(bw, &value, .upper);
869852 }
870853 }
871 var n: usize = 0;
872 n += try bw.writeAllCount("{ ");
854 try bw.writeAll("{ ");
873855 for (value, 0..) |elem, i| {
874 n += try printValue(bw, actual_fmt, options, elem, max_depth - 1);
856 try printValue(bw, actual_fmt, options, elem, max_depth - 1);
875857 if (i < value.len - 1) {
876 n += try bw.writeAllCount(", ");
858 try bw.writeAll(", ");
877859 }
878860 }
879 n += try bw.writeAllCount(" }");
880 return n;
861 try bw.writeAll(" }");
881862 },
882863 .vector => |info| {
883864 if (max_depth == 0) {
884 return bw.writeAllCount("{ ... }");
865 return bw.writeAll("{ ... }");
885866 }
886 var n: usize = 0;
887 n += try bw.writeAllCount("{ ");
867 try bw.writeAll("{ ");
888868 var i: usize = 0;
889869 while (i < info.len) : (i += 1) {
890 n += try printValue(bw, actual_fmt, options, value[i], max_depth - 1);
870 try printValue(bw, actual_fmt, options, value[i], max_depth - 1);
891871 if (i < info.len - 1) {
892 n += try bw.writeAllCount(", ");
872 try bw.writeAll(", ");
893873 }
894874 }
895 n += try bw.writeAllCount(" }");
896 return n;
875 try bw.writeAll(" }");
897876 },
898877 .@"fn" => @compileError("unable to format function body type, use '*const " ++ @typeName(T) ++ "' for a function pointer type"),
899878 .type => {
......@@ -918,7 +897,7 @@ pub fn printInt(
918897 comptime fmt: []const u8,
919898 options: std.fmt.Options,
920899 value: anytype,
921) anyerror!usize {
900) anyerror!void {
922901 const int_value = if (@TypeOf(value) == comptime_int) blk: {
923902 const Int = std.math.IntFittingRange(value, value);
924903 break :blk @as(Int, value);
......@@ -962,15 +941,15 @@ pub fn printInt(
962941 comptime unreachable;
963942}
964943
965pub fn printAsciiChar(bw: *BufferedWriter, c: u8, options: std.fmt.Options) anyerror!usize {
944pub fn printAsciiChar(bw: *BufferedWriter, c: u8, options: std.fmt.Options) anyerror!void {
966945 return alignBufferOptions(bw, @as(*const [1]u8, &c), options);
967946}
968947
969pub fn printAscii(bw: *BufferedWriter, bytes: []const u8, options: std.fmt.Options) anyerror!usize {
948pub fn printAscii(bw: *BufferedWriter, bytes: []const u8, options: std.fmt.Options) anyerror!void {
970949 return alignBufferOptions(bw, bytes, options);
971950}
972951
973pub fn printUnicodeCodepoint(bw: *BufferedWriter, c: u21, options: std.fmt.Options) anyerror!usize {
952pub fn printUnicodeCodepoint(bw: *BufferedWriter, c: u21, options: std.fmt.Options) anyerror!void {
974953 var buf: [4]u8 = undefined;
975954 const len = try std.unicode.utf8Encode(c, &buf);
976955 return alignBufferOptions(bw, buf[0..len], options);
......@@ -982,7 +961,7 @@ pub fn printIntOptions(
982961 base: u8,
983962 case: std.fmt.Case,
984963 options: std.fmt.Options,
985) anyerror!usize {
964) anyerror!void {
986965 assert(base >= 2);
987966
988967 const int_value = if (@TypeOf(value) == comptime_int) blk: {
......@@ -1049,7 +1028,7 @@ pub fn printFloat(
10491028 comptime fmt: []const u8,
10501029 options: std.fmt.Options,
10511030 value: anytype,
1052) anyerror!usize {
1031) anyerror!void {
10531032 var buf: [std.fmt.float.bufferSize(.decimal, f64)]u8 = undefined;
10541033
10551034 if (fmt.len > 1) invalidFmtError(fmt, value);
......@@ -1337,7 +1316,7 @@ pub fn printDuration(bw: *BufferedWriter, nanoseconds: anytype, options: std.fmt
13371316 return alignBufferOptions(bw, sub_bw.getWritten(), options);
13381317}
13391318
1340pub fn printHex(bw: *BufferedWriter, bytes: []const u8, case: std.fmt.Case) anyerror!usize {
1319pub fn printHex(bw: *BufferedWriter, bytes: []const u8, case: std.fmt.Case) anyerror!void {
13411320 const charset = switch (case) {
13421321 .upper => "0123456789ABCDEF",
13431322 .lower => "0123456789abcdef",
......@@ -1346,21 +1325,18 @@ pub fn printHex(bw: *BufferedWriter, bytes: []const u8, case: std.fmt.Case) anye
13461325 try writeByte(bw, charset[c >> 4]);
13471326 try writeByte(bw, charset[c & 15]);
13481327 }
1349 return bytes.len * 2;
13501328}
13511329
1352pub fn printBase64(bw: *BufferedWriter, bytes: []const u8) anyerror!usize {
1330pub fn printBase64(bw: *BufferedWriter, bytes: []const u8) anyerror!void {
13531331 var chunker = std.mem.window(u8, bytes, 3, 3);
13541332 var temp: [5]u8 = undefined;
1355 var n: usize = 0;
13561333 while (chunker.next()) |chunk| {
1357 n += try bw.writeAllCount(std.base64.standard.Encoder.encode(&temp, chunk));
1334 try bw.writeAll(std.base64.standard.Encoder.encode(&temp, chunk));
13581335 }
1359 return n;
13601336}
13611337
13621338/// Write a single unsigned integer as unsigned LEB128 to the given writer.
1363pub fn writeUleb128(bw: *std.io.BufferedWriter, arg: anytype) anyerror!usize {
1339pub fn writeUleb128(bw: *std.io.BufferedWriter, arg: anytype) anyerror!void {
13641340 const Arg = @TypeOf(arg);
13651341 const Int = switch (Arg) {
13661342 comptime_int => std.math.IntFittingRange(arg, arg),
......@@ -1368,23 +1344,21 @@ pub fn writeUleb128(bw: *std.io.BufferedWriter, arg: anytype) anyerror!usize {
13681344 };
13691345 const Value = if (@typeInfo(Int).int.bits < 8) u8 else Int;
13701346 var value: Value = arg;
1371 var n: usize = 0;
13721347
13731348 while (true) {
13741349 const byte: u8 = @truncate(value & 0x7f);
13751350 value >>= 7;
13761351 if (value == 0) {
13771352 try bw.writeByte(byte);
1378 return n + 1;
1353 return;
13791354 } else {
13801355 try bw.writeByte(byte | 0x80);
1381 n += 1;
13821356 }
13831357 }
13841358}
13851359
13861360/// Write a single signed integer as signed LEB128 to the given writer.
1387pub fn writeIleb128(bw: *std.io.BufferedWriter, arg: anytype) anyerror!usize {
1361pub fn writeIleb128(bw: *std.io.BufferedWriter, arg: anytype) anyerror!void {
13881362 const Arg = @TypeOf(arg);
13891363 const Int = switch (Arg) {
13901364 comptime_int => std.math.IntFittingRange(-@abs(arg), @abs(arg)),
......@@ -1393,7 +1367,6 @@ pub fn writeIleb128(bw: *std.io.BufferedWriter, arg: anytype) anyerror!usize {
13931367 const Signed = if (@typeInfo(Int).int.bits < 8) i8 else Int;
13941368 const Unsigned = std.meta.Int(.unsigned, @typeInfo(Signed).int.bits);
13951369 var value: Signed = arg;
1396 var n: usize = 0;
13971370
13981371 while (true) {
13991372 const unsigned: Unsigned = @bitCast(value);
......@@ -1401,11 +1374,10 @@ pub fn writeIleb128(bw: *std.io.BufferedWriter, arg: anytype) anyerror!usize {
14011374 value >>= 6;
14021375 if (value == -1 or value == 0) {
14031376 try bw.writeByte(byte & 0x7F);
1404 return n + 1;
1377 return;
14051378 } else {
14061379 value >>= 1;
14071380 try bw.writeByte(byte | 0x80);
1408 n += 1;
14091381 }
14101382 }
14111383}
lib/std/zig/ErrorBundle.zig+8-7
......@@ -190,21 +190,22 @@ fn renderErrorMessageToWriter(
190190) anyerror!void {
191191 const ttyconf = options.ttyconf;
192192 const err_msg = eb.getErrorMessage(err_msg_index);
193 // This is the length of the part before the error message:
194 // e.g. "file.zig:4:5: error: "
195 var prefix_len: usize = 0;
193 const prefix_start = bw.bytes_written;
196194 if (err_msg.src_loc != .none) {
197195 const src = eb.extraData(SourceLocation, @intFromEnum(err_msg.src_loc));
198 prefix_len += try bw.splatByteAllCount(' ', indent);
196 try bw.splatByteAll(' ', indent);
199197 try ttyconf.setColor(bw, .bold);
200 prefix_len += try bw.printCount("{s}:{d}:{d}: ", .{
198 try bw.print("{s}:{d}:{d}: ", .{
201199 eb.nullTerminatedString(src.data.src_path),
202200 src.data.line + 1,
203201 src.data.column + 1,
204202 });
205203 try ttyconf.setColor(bw, color);
206 prefix_len += try bw.writeAllCount(kind);
207 prefix_len += try bw.writeAllCount(": ");
204 try bw.writeAll(kind);
205 try bw.writeAll(": ");
206 // This is the length of the part before the error message:
207 // e.g. "file.zig:4:5: error: "
208 const prefix_len = bw.bytes_written - prefix_start;
208209 try ttyconf.setColor(bw, .reset);
209210 try ttyconf.setColor(bw, .bold);
210211 if (err_msg.count == 1) {
lib/ubsan_rt.zig+6-10
......@@ -119,28 +119,24 @@ const Value = extern struct {
119119 }
120120 }
121121
122 pub fn format(
123 value: Value,
124 bw: *std.io.BufferedWriter,
125 comptime fmt: []const u8,
126 ) anyerror!usize {
122 pub fn format(value: Value, bw: *std.io.BufferedWriter, comptime fmt: []const u8) anyerror!void {
127123 comptime assert(fmt.len == 0);
128124
129125 // Work around x86_64 backend limitation.
130126 if (builtin.zig_backend == .stage2_x86_64 and builtin.os.tag == .windows) {
131 return bw.writeAllCount("(unknown)");
127 return bw.writeAll("(unknown)");
132128 }
133129
134130 switch (value.td.kind) {
135131 .integer => {
136132 if (value.td.isSigned()) {
137 return bw.printCount("{d}", .{value.getSignedInteger()});
133 return bw.print("{d}", .{value.getSignedInteger()});
138134 } else {
139 return bw.printCount("{d}", .{value.getUnsignedInteger()});
135 return bw.print("{d}", .{value.getUnsignedInteger()});
140136 }
141137 },
142 .float => return bw.printCount("{d}", .{value.getFloat()}),
143 .unknown => return bw.writeAllCount("(unknown)"),
138 .float => return bw.print("{d}", .{value.getFloat()}),
139 .unknown => return bw.writeAll("(unknown)"),
144140 }
145141 }
146142};