authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-19 02:11:13-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-01 16:35:26-07:00
log4aed226e07a911be88ed6e2f349796364d3327f7
tree61b1731987bc23a19e5f7c6f22ab57463a1044c9
parent5ada2e39371202f7bbd454c37be31a602f534136

zig fmt compiling


4 files changed, 46 insertions(+), 29 deletions(-)

lib/std/http/WebSocket.zig+2-4
......@@ -193,16 +193,14 @@ fn recvReadInt(ws: *WebSocket, comptime I: type) !I {
193193 };
194194}
195195
196pub const WriteError = std.http.Server.Response.WriteError;
197
198pub fn writeMessage(ws: *WebSocket, message: []const u8, opcode: Opcode) WriteError!void {
196pub fn writeMessage(ws: *WebSocket, message: []const u8, opcode: Opcode) anyerror!void {
199197 const iovecs: [1]std.posix.iovec_const = .{
200198 .{ .base = message.ptr, .len = message.len },
201199 };
202200 return writeMessagev(ws, &iovecs, opcode);
203201}
204202
205pub fn writeMessagev(ws: *WebSocket, message: []const std.posix.iovec_const, opcode: Opcode) WriteError!void {
203pub fn writeMessagev(ws: *WebSocket, message: []const std.posix.iovec_const, opcode: Opcode) anyerror!void {
206204 const total_len = l: {
207205 var total_len: u64 = 0;
208206 for (message) |iovec| total_len += iovec.len;
lib/std/zig/render.zig+5-4
......@@ -1576,7 +1576,7 @@ fn renderBuiltinCall(
15761576 defer r.gpa.free(new_string);
15771577
15781578 try renderToken(r, builtin_token + 1, .none); // (
1579 try ais.print("\"{}\"", .{std.zig.fmtEscapes(new_string)});
1579 try ais.print("\"{f}\"", .{std.zig.fmtEscapes(new_string)});
15801580 return renderToken(r, str_lit_token + 1, space); // )
15811581 }
15821582 }
......@@ -2888,7 +2888,7 @@ fn renderIdentifierContents(ais: *AutoIndentingStream, bytes: []const u8) !void
28882888 .success => |codepoint| {
28892889 if (codepoint <= 0x7f) {
28902890 const buf = [1]u8{@as(u8, @intCast(codepoint))};
2891 try ais.print("{}", .{std.zig.fmtEscapes(&buf)});
2891 try ais.print("{f}", .{std.zig.fmtEscapes(&buf)});
28922892 } else {
28932893 try ais.writeAll(escape_sequence);
28942894 }
......@@ -2900,7 +2900,7 @@ fn renderIdentifierContents(ais: *AutoIndentingStream, bytes: []const u8) !void
29002900 },
29012901 0x00...('\\' - 1), ('\\' + 1)...0x7f => {
29022902 const buf = [1]u8{byte};
2903 try ais.print("{}", .{std.zig.fmtEscapes(&buf)});
2903 try ais.print("{f}", .{std.zig.fmtEscapes(&buf)});
29042904 pos += 1;
29052905 },
29062906 0x80...0xff => {
......@@ -3305,8 +3305,9 @@ const AutoIndentingStream = struct {
33053305 if (bytes[bytes.len - 1] == '\n') ais.resetLine();
33063306 }
33073307
3308 /// Assumes that if the printed data ends with a newline, it is directly
3309 /// contained in the format string.
33083310 pub fn print(ais: *AutoIndentingStream, comptime format: []const u8, args: anytype) anyerror!void {
3309 comptime assert(format[format.len - 1] != '}');
33103311 try ais.applyIndent();
33113312 if (ais.disabled_offset == null) try ais.underlying_writer.print(format, args);
33123313 if (format[format.len - 1] == '\n') ais.resetLine();
src/Package/Fetch/git.zig+3-3
......@@ -122,9 +122,9 @@ pub const Oid = union(Format) {
122122 pub fn format(
123123 oid: Oid,
124124 comptime fmt: []const u8,
125 options: std.fmt.FormatOptions,
126 writer: anytype,
127 ) @TypeOf(writer).Error!void {
125 options: std.fmt.Options,
126 writer: *std.io.BufferedWriter,
127 ) anyerror!void {
128128 _ = fmt;
129129 _ = options;
130130 try writer.print("{x}", .{oid.slice()});
src/fmt.zig+36-18
......@@ -26,16 +26,13 @@ const Fmt = struct {
2626 color: Color,
2727 gpa: Allocator,
2828 arena: Allocator,
29 out_buffer: std.ArrayList(u8),
29 out_buffer: std.ArrayListUnmanaged(u8),
30 stdout: *std.io.BufferedWriter,
3031
3132 const SeenMap = std.AutoHashMap(fs.File.INode, void);
3233};
3334
34pub fn run(
35 gpa: Allocator,
36 arena: Allocator,
37 args: []const []const u8,
38) !void {
35pub fn run(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
3936 var color: Color = .auto;
4037 var stdin_flag = false;
4138 var check_flag = false;
......@@ -52,8 +49,7 @@ pub fn run(
5249 const arg = args[i];
5350 if (mem.startsWith(u8, arg, "-")) {
5451 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
55 const stdout = std.io.getStdOut().writer();
56 try stdout.writeAll(usage_fmt);
52 try std.io.getStdOut().writeAll(usage_fmt);
5753 return process.cleanExit();
5854 } else if (mem.eql(u8, arg, "--color")) {
5955 if (i + 1 >= args.len) {
......@@ -138,8 +134,11 @@ pub fn run(
138134 try std.zig.printAstErrorsToStderr(gpa, tree, "<stdin>", color);
139135 process.exit(2);
140136 }
141 const formatted = try tree.render(gpa);
142 defer gpa.free(formatted);
137 var aw: std.io.AllocatingWriter = undefined;
138 const bw = aw.init(gpa);
139 defer aw.deinit();
140 try tree.render(gpa, bw, .{});
141 const formatted = aw.getWritten();
143142
144143 if (check_flag) {
145144 const code: u8 = @intFromBool(mem.eql(u8, formatted, source_code));
......@@ -153,6 +152,12 @@ pub fn run(
153152 fatal("expected at least one source file argument", .{});
154153 }
155154
155 var stdout_buffer: [4096]u8 = undefined;
156 var stdout: std.io.BufferedWriter = .{
157 .buffer = &stdout_buffer,
158 .unbuffered_writer = std.io.getStdOut().writer(),
159 };
160
156161 var fmt: Fmt = .{
157162 .gpa = gpa,
158163 .arena = arena,
......@@ -161,10 +166,11 @@ pub fn run(
161166 .check_ast = check_ast_flag,
162167 .force_zon = force_zon,
163168 .color = color,
164 .out_buffer = std.ArrayList(u8).init(gpa),
169 .out_buffer = .empty,
170 .stdout = &stdout,
165171 };
166172 defer fmt.seen.deinit();
167 defer fmt.out_buffer.deinit();
173 defer fmt.out_buffer.deinit(gpa);
168174
169175 // Mark any excluded files/directories as already seen,
170176 // so that they are skipped later during actual processing
......@@ -322,15 +328,19 @@ fn fmtPathFile(
322328
323329 // As a heuristic, we make enough capacity for the same as the input source.
324330 fmt.out_buffer.shrinkRetainingCapacity(0);
325 try fmt.out_buffer.ensureTotalCapacity(source_code.len);
331 try fmt.out_buffer.ensureTotalCapacity(gpa, source_code.len);
326332
327 try tree.renderToArrayList(&fmt.out_buffer, .{});
333 {
334 var aw: std.io.AllocatingWriter = undefined;
335 const bw = aw.fromArrayList(gpa, &fmt.out_buffer);
336 defer fmt.out_buffer = aw.toArrayList();
337 try tree.render(gpa, bw, .{});
338 }
328339 if (mem.eql(u8, fmt.out_buffer.items, source_code))
329340 return;
330341
331342 if (check_mode) {
332 const stdout = std.io.getStdOut().writer();
333 try stdout.print("{s}\n", .{file_path});
343 try fmt.stdout.print("{s}\n", .{file_path});
334344 fmt.any_error = true;
335345 } else {
336346 var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode });
......@@ -338,8 +348,7 @@ fn fmtPathFile(
338348
339349 try af.file.writeAll(fmt.out_buffer.items);
340350 try af.finish();
341 const stdout = std.io.getStdOut().writer();
342 try stdout.print("{s}\n", .{file_path});
351 try fmt.stdout.print("{s}\n", .{file_path});
343352 }
344353}
345354
......@@ -350,3 +359,12 @@ const process = std.process;
350359const Allocator = std.mem.Allocator;
351360const Color = std.zig.Color;
352361const fatal = std.process.fatal;
362
363/// Provided for debugging/testing purposes; unused by the compiler.
364pub fn main() !void {
365 const gpa = std.heap.smp_allocator;
366 var arena_instance = std.heap.ArenaAllocator.init(gpa);
367 const arena = arena_instance.allocator();
368 const args = try process.argsAlloc(arena);
369 return run(gpa, arena, args[1..]);
370}