Skip to content

Commit 88e6082

Browse files
committed
Refactor stdout handling in Zig main function to use streaming writer
- Updated stdout initialization to use writerStreaming for improved performance. - Changed usage message output from std.debug.print to stdout.print for consistency. - Removed unnecessary flush calls to streamline the code.
1 parent c5cd368 commit 88e6082

File tree

5 files changed

+13
-23
lines changed

5 files changed

+13
-23
lines changed

compiled_starters/zig/src/main.zig

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const std = @import("std");
2-
var stdout_buffer: [1024]u8 = undefined;
3-
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
4-
const stdout = &stdout_writer.interface;
2+
var stdout_reader = std.fs.File.stdout().writerStreaming(&.{});
3+
const stdout = &stdout_reader.interface;
54

65
pub fn main() !void {
76
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@@ -12,7 +11,7 @@ pub fn main() !void {
1211
defer std.process.argsFree(allocator, args);
1312

1413
if (args.len < 3) {
15-
std.debug.print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
14+
try stdout.print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
1615
std.process.exit(1);
1716
}
1817

@@ -32,6 +31,5 @@ pub fn main() !void {
3231
// _ = try file.read(&buf);
3332
// const page_size = std.mem.readInt(u16, &buf, .big);
3433
// try stdout.print("database page size: {}\n", .{page_size});
35-
// try stdout.flush();
3634
}
3735
}

solutions/zig/01-dr6/code/src/main.zig

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const std = @import("std");
2-
var stdout_buffer: [1024]u8 = undefined;
3-
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
4-
const stdout = &stdout_writer.interface;
2+
var stdout_reader = std.fs.File.stdout().writerStreaming(&.{});
3+
const stdout = &stdout_reader.interface;
54

65
pub fn main() !void {
76
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@@ -12,7 +11,7 @@ pub fn main() !void {
1211
defer std.process.argsFree(allocator, args);
1312

1413
if (args.len < 3) {
15-
std.debug.print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
14+
try stdout.print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
1615
std.process.exit(1);
1716
}
1817

@@ -28,6 +27,5 @@ pub fn main() !void {
2827
_ = try file.read(&buf);
2928
const page_size = std.mem.readInt(u16, &buf, .big);
3029
try stdout.print("database page size: {}\n", .{page_size});
31-
try stdout.flush();
3230
}
3331
}

solutions/zig/01-dr6/diff/src/main.zig.diff

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
@@ -1,37 +1,33 @@
1+
@@ -1,35 +1,31 @@
22
const std = @import("std");
3-
var stdout_buffer: [1024]u8 = undefined;
4-
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
5-
const stdout = &stdout_writer.interface;
3+
var stdout_reader = std.fs.File.stdout().writerStreaming(&.{});
4+
const stdout = &stdout_reader.interface;
65

76
pub fn main() !void {
87
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@@ -13,7 +12,7 @@
1312
defer std.process.argsFree(allocator, args);
1413

1514
if (args.len < 3) {
16-
std.debug.print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
15+
try stdout.print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
1716
std.process.exit(1);
1817
}
1918

@@ -33,12 +32,10 @@
3332
- // _ = try file.read(&buf);
3433
- // const page_size = std.mem.readInt(u16, &buf, .big);
3534
- // try stdout.print("database page size: {}\n", .{page_size});
36-
- // try stdout.flush();
3735
+ var buf: [2]u8 = undefined;
3836
+ _ = try file.seekTo(16);
3937
+ _ = try file.read(&buf);
4038
+ const page_size = std.mem.readInt(u16, &buf, .big);
4139
+ try stdout.print("database page size: {}\n", .{page_size});
42-
+ try stdout.flush();
4340
}
4441
}

solutions/zig/01-dr6/explanation.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ _ = try file.seekTo(16);
99
_ = try file.read(&buf);
1010
const page_size = std.mem.readInt(u16, &buf, .big);
1111
try stdout.print("database page size: {}\n", .{page_size});
12-
try stdout.flush();
1312
```
1413

1514
Push your changes to pass the first stage:

starter_templates/zig/code/src/main.zig

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const std = @import("std");
2-
var stdout_buffer: [1024]u8 = undefined;
3-
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
4-
const stdout = &stdout_writer.interface;
2+
var stdout_reader = std.fs.File.stdout().writerStreaming(&.{});
3+
const stdout = &stdout_reader.interface;
54

65
pub fn main() !void {
76
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@@ -12,7 +11,7 @@ pub fn main() !void {
1211
defer std.process.argsFree(allocator, args);
1312

1413
if (args.len < 3) {
15-
std.debug.print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
14+
try stdout.print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
1615
std.process.exit(1);
1716
}
1817

@@ -32,6 +31,5 @@ pub fn main() !void {
3231
// _ = try file.read(&buf);
3332
// const page_size = std.mem.readInt(u16, &buf, .big);
3433
// try stdout.print("database page size: {}\n", .{page_size});
35-
// try stdout.flush();
3634
}
3735
}

0 commit comments

Comments
 (0)