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 {...@@ -193,16 +193,14 @@ fn recvReadInt(ws: *WebSocket, comptime I: type) !I {
193 };193 };
194}194}
195195
196pub const WriteError = std.http.Server.Response.WriteError;196pub fn writeMessage(ws: *WebSocket, message: []const u8, opcode: Opcode) anyerror!void {
197
198pub fn writeMessage(ws: *WebSocket, message: []const u8, opcode: Opcode) WriteError!void {
199 const iovecs: [1]std.posix.iovec_const = .{197 const iovecs: [1]std.posix.iovec_const = .{
200 .{ .base = message.ptr, .len = message.len },198 .{ .base = message.ptr, .len = message.len },
201 };199 };
202 return writeMessagev(ws, &iovecs, opcode);200 return writeMessagev(ws, &iovecs, opcode);
203}201}
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 {
206 const total_len = l: {204 const total_len = l: {
207 var total_len: u64 = 0;205 var total_len: u64 = 0;
208 for (message) |iovec| total_len += iovec.len;206 for (message) |iovec| total_len += iovec.len;
lib/std/zig/render.zig+5-4
...@@ -1576,7 +1576,7 @@ fn renderBuiltinCall(...@@ -1576,7 +1576,7 @@ fn renderBuiltinCall(
1576 defer r.gpa.free(new_string);1576 defer r.gpa.free(new_string);
15771577
1578 try renderToken(r, builtin_token + 1, .none); // (1578 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)});
1580 return renderToken(r, str_lit_token + 1, space); // )1580 return renderToken(r, str_lit_token + 1, space); // )
1581 }1581 }
1582 }1582 }
...@@ -2888,7 +2888,7 @@ fn renderIdentifierContents(ais: *AutoIndentingStream, bytes: []const u8) !void...@@ -2888,7 +2888,7 @@ fn renderIdentifierContents(ais: *AutoIndentingStream, bytes: []const u8) !void
2888 .success => |codepoint| {2888 .success => |codepoint| {
2889 if (codepoint <= 0x7f) {2889 if (codepoint <= 0x7f) {
2890 const buf = [1]u8{@as(u8, @intCast(codepoint))};2890 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)});
2892 } else {2892 } else {
2893 try ais.writeAll(escape_sequence);2893 try ais.writeAll(escape_sequence);
2894 }2894 }
...@@ -2900,7 +2900,7 @@ fn renderIdentifierContents(ais: *AutoIndentingStream, bytes: []const u8) !void...@@ -2900,7 +2900,7 @@ fn renderIdentifierContents(ais: *AutoIndentingStream, bytes: []const u8) !void
2900 },2900 },
2901 0x00...('\\' - 1), ('\\' + 1)...0x7f => {2901 0x00...('\\' - 1), ('\\' + 1)...0x7f => {
2902 const buf = [1]u8{byte};2902 const buf = [1]u8{byte};
2903 try ais.print("{}", .{std.zig.fmtEscapes(&buf)});2903 try ais.print("{f}", .{std.zig.fmtEscapes(&buf)});
2904 pos += 1;2904 pos += 1;
2905 },2905 },
2906 0x80...0xff => {2906 0x80...0xff => {
...@@ -3305,8 +3305,9 @@ const AutoIndentingStream = struct {...@@ -3305,8 +3305,9 @@ const AutoIndentingStream = struct {
3305 if (bytes[bytes.len - 1] == '\n') ais.resetLine();3305 if (bytes[bytes.len - 1] == '\n') ais.resetLine();
3306 }3306 }
33073307
3308 /// Assumes that if the printed data ends with a newline, it is directly
3309 /// contained in the format string.
3308 pub fn print(ais: *AutoIndentingStream, comptime format: []const u8, args: anytype) anyerror!void {3310 pub fn print(ais: *AutoIndentingStream, comptime format: []const u8, args: anytype) anyerror!void {
3309 comptime assert(format[format.len - 1] != '}');
3310 try ais.applyIndent();3311 try ais.applyIndent();
3311 if (ais.disabled_offset == null) try ais.underlying_writer.print(format, args);3312 if (ais.disabled_offset == null) try ais.underlying_writer.print(format, args);
3312 if (format[format.len - 1] == '\n') ais.resetLine();3313 if (format[format.len - 1] == '\n') ais.resetLine();
src/Package/Fetch/git.zig+3-3
...@@ -122,9 +122,9 @@ pub const Oid = union(Format) {...@@ -122,9 +122,9 @@ pub const Oid = union(Format) {
122 pub fn format(122 pub fn format(
123 oid: Oid,123 oid: Oid,
124 comptime fmt: []const u8,124 comptime fmt: []const u8,
125 options: std.fmt.FormatOptions,125 options: std.fmt.Options,
126 writer: anytype,126 writer: *std.io.BufferedWriter,
127 ) @TypeOf(writer).Error!void {127 ) anyerror!void {
128 _ = fmt;128 _ = fmt;
129 _ = options;129 _ = options;
130 try writer.print("{x}", .{oid.slice()});130 try writer.print("{x}", .{oid.slice()});
src/fmt.zig+36-18
...@@ -26,16 +26,13 @@ const Fmt = struct {...@@ -26,16 +26,13 @@ const Fmt = struct {
26 color: Color,26 color: Color,
27 gpa: Allocator,27 gpa: Allocator,
28 arena: Allocator,28 arena: Allocator,
29 out_buffer: std.ArrayList(u8),29 out_buffer: std.ArrayListUnmanaged(u8),
30 stdout: *std.io.BufferedWriter,
3031
31 const SeenMap = std.AutoHashMap(fs.File.INode, void);32 const SeenMap = std.AutoHashMap(fs.File.INode, void);
32};33};
3334
34pub fn run(35pub fn run(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
35 gpa: Allocator,
36 arena: Allocator,
37 args: []const []const u8,
38) !void {
39 var color: Color = .auto;36 var color: Color = .auto;
40 var stdin_flag = false;37 var stdin_flag = false;
41 var check_flag = false;38 var check_flag = false;
...@@ -52,8 +49,7 @@ pub fn run(...@@ -52,8 +49,7 @@ pub fn run(
52 const arg = args[i];49 const arg = args[i];
53 if (mem.startsWith(u8, arg, "-")) {50 if (mem.startsWith(u8, arg, "-")) {
54 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {51 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
55 const stdout = std.io.getStdOut().writer();52 try std.io.getStdOut().writeAll(usage_fmt);
56 try stdout.writeAll(usage_fmt);
57 return process.cleanExit();53 return process.cleanExit();
58 } else if (mem.eql(u8, arg, "--color")) {54 } else if (mem.eql(u8, arg, "--color")) {
59 if (i + 1 >= args.len) {55 if (i + 1 >= args.len) {
...@@ -138,8 +134,11 @@ pub fn run(...@@ -138,8 +134,11 @@ pub fn run(
138 try std.zig.printAstErrorsToStderr(gpa, tree, "<stdin>", color);134 try std.zig.printAstErrorsToStderr(gpa, tree, "<stdin>", color);
139 process.exit(2);135 process.exit(2);
140 }136 }
141 const formatted = try tree.render(gpa);137 var aw: std.io.AllocatingWriter = undefined;
142 defer gpa.free(formatted);138 const bw = aw.init(gpa);
139 defer aw.deinit();
140 try tree.render(gpa, bw, .{});
141 const formatted = aw.getWritten();
143142
144 if (check_flag) {143 if (check_flag) {
145 const code: u8 = @intFromBool(mem.eql(u8, formatted, source_code));144 const code: u8 = @intFromBool(mem.eql(u8, formatted, source_code));
...@@ -153,6 +152,12 @@ pub fn run(...@@ -153,6 +152,12 @@ pub fn run(
153 fatal("expected at least one source file argument", .{});152 fatal("expected at least one source file argument", .{});
154 }153 }
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
156 var fmt: Fmt = .{161 var fmt: Fmt = .{
157 .gpa = gpa,162 .gpa = gpa,
158 .arena = arena,163 .arena = arena,
...@@ -161,10 +166,11 @@ pub fn run(...@@ -161,10 +166,11 @@ pub fn run(
161 .check_ast = check_ast_flag,166 .check_ast = check_ast_flag,
162 .force_zon = force_zon,167 .force_zon = force_zon,
163 .color = color,168 .color = color,
164 .out_buffer = std.ArrayList(u8).init(gpa),169 .out_buffer = .empty,
170 .stdout = &stdout,
165 };171 };
166 defer fmt.seen.deinit();172 defer fmt.seen.deinit();
167 defer fmt.out_buffer.deinit();173 defer fmt.out_buffer.deinit(gpa);
168174
169 // Mark any excluded files/directories as already seen,175 // Mark any excluded files/directories as already seen,
170 // so that they are skipped later during actual processing176 // so that they are skipped later during actual processing
...@@ -322,15 +328,19 @@ fn fmtPathFile(...@@ -322,15 +328,19 @@ fn fmtPathFile(
322328
323 // As a heuristic, we make enough capacity for the same as the input source.329 // As a heuristic, we make enough capacity for the same as the input source.
324 fmt.out_buffer.shrinkRetainingCapacity(0);330 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 }
328 if (mem.eql(u8, fmt.out_buffer.items, source_code))339 if (mem.eql(u8, fmt.out_buffer.items, source_code))
329 return;340 return;
330341
331 if (check_mode) {342 if (check_mode) {
332 const stdout = std.io.getStdOut().writer();343 try fmt.stdout.print("{s}\n", .{file_path});
333 try stdout.print("{s}\n", .{file_path});
334 fmt.any_error = true;344 fmt.any_error = true;
335 } else {345 } else {
336 var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode });346 var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode });
...@@ -338,8 +348,7 @@ fn fmtPathFile(...@@ -338,8 +348,7 @@ fn fmtPathFile(
338348
339 try af.file.writeAll(fmt.out_buffer.items);349 try af.file.writeAll(fmt.out_buffer.items);
340 try af.finish();350 try af.finish();
341 const stdout = std.io.getStdOut().writer();351 try fmt.stdout.print("{s}\n", .{file_path});
342 try stdout.print("{s}\n", .{file_path});
343 }352 }
344}353}
345354
...@@ -350,3 +359,12 @@ const process = std.process;...@@ -350,3 +359,12 @@ const process = std.process;
350const Allocator = std.mem.Allocator;359const Allocator = std.mem.Allocator;
351const Color = std.zig.Color;360const Color = std.zig.Color;
352const fatal = std.process.fatal;361const 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}