Skip to content

Commit dbf80ce

Browse files
committed
moving zig printing to a seprate file
1 parent b9aef74 commit dbf80ce

File tree

25 files changed

+86
-125
lines changed

25 files changed

+86
-125
lines changed

bench/algorithm/binarytrees/1.zig

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const print = @import("../../include/zig/print.zig");
23
const builtin = @import("builtin");
34
const math = std.math;
45
const Allocator = std.mem.Allocator;
@@ -14,7 +15,7 @@ pub fn main() !void {
1415
const stretch_depth = max_depth + 1;
1516
const stretch_tree = Node.make(stretch_depth, global_allocator).?;
1617
defer stretch_tree.deinit();
17-
try printFmt("stretch tree of depth {d}\t check: {d}\n", .{ stretch_depth, stretch_tree.check() });
18+
try print.printFmt("stretch tree of depth {d}\t check: {d}\n", .{ stretch_depth, stretch_tree.check() });
1819
}
1920
const long_lived_tree = Node.make(max_depth, global_allocator).?;
2021
defer long_lived_tree.deinit();
@@ -29,10 +30,10 @@ pub fn main() !void {
2930
defer tree.deinit();
3031
sum += tree.check();
3132
}
32-
try printFmt("{d}\t trees of depth {d}\t check: {d}\n", .{ iterations, depth, sum });
33+
try print.printFmt("{d}\t trees of depth {d}\t check: {d}\n", .{ iterations, depth, sum });
3334
}
3435

35-
try printFmt("long lived tree of depth {d}\t check: {d}\n", .{ max_depth, long_lived_tree.check() });
36+
try print.printFmt("long lived tree of depth {d}\t check: {d}\n", .{ max_depth, long_lived_tree.check() });
3637
}
3738

3839
fn get_n() !usize {
@@ -42,12 +43,6 @@ fn get_n() !usize {
4243
return @as(usize, @intCast(try std.fmt.parseInt(u32, arg, 10)));
4344
}
4445

45-
fn printFmt(comptime fmt: []const u8, args: anytype) !void {
46-
var buf: [256]u8 = undefined;
47-
const out = try std.fmt.bufPrint(&buf, fmt, args);
48-
_ = try std.posix.write(std.posix.STDOUT_FILENO, out);
49-
}
50-
5146
const Node = struct {
5247
const Self = @This();
5348

bench/algorithm/coro-prime-sieve/1.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// https://gist.github.com/SpexGuy/953e5780cd2d2c524cba6a79f13076e6
33

44
const std = @import("std");
5+
const print = @import("../../include/zig/print.zig");
56

67
const Channel = struct {
78
value: u32,
@@ -34,7 +35,7 @@ fn filter(out_channel: *Channel, in_channel: *Channel, prime: u32) void {
3435

3536
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
3637
const global_allocator = arena.allocator();
37-
const stdout = std.io.getStdOut().writer();
38+
3839

3940
pub fn main() !void {
4041
defer arena.deinit();
@@ -52,7 +53,7 @@ pub fn main() !void {
5253
while (i < n) : (i += 1) {
5354
resume ch.frame;
5455
const prime = ch.value;
55-
try stdout.print("{}\n", .{prime});
56+
try print.printFmt("{}\n", .{prime});
5657
if (i >= n - 1) break;
5758
const ch1 = try global_allocator.create(Channel);
5859
const frame = try global_allocator.create(@Frame(filter));

bench/algorithm/edigits/1.zig

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const print = @import("../../include/zig/print.zig");
23
const bigint = std.math.big.int;
34
const math = std.math;
45
const global_allocator = std.heap.c_allocator;
@@ -43,19 +44,14 @@ pub fn main() !void {
4344
j += 1;
4445
}
4546
if (i + 10 <= n_usize) {
46-
try printFmt("{s}\t:{d}\n", .{ sb, i + 10 });
47+
try print.printFmt("{s}\t:{d}\n", .{ sb, i + 10 });
4748
} else {
48-
try printFmt("{s}\t:{d}\n", .{ sb, n });
49+
try print.printFmt("{s}\t:{d}\n", .{ sb, n });
4950
}
5051
i += 10;
5152
}
5253
}
5354

54-
fn printFmt(comptime fmt: []const u8, args: anytype) !void {
55-
var buf: [256]u8 = undefined;
56-
const out = try std.fmt.bufPrint(&buf, fmt, args);
57-
_ = try std.posix.write(std.posix.STDOUT_FILENO, out);
58-
}
5955

6056
fn sum_terms(a: i32, b: i32) anyerror!Pair {
6157
if (b == a + 1) {

bench/algorithm/fannkuch-redux/1.zig

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
// From https://github.com/tiehuis/zig-benchmarks-game/blob/master/src/fannkuch-redux.zig
22

33
const std = @import("std");
4+
const print = @import("../../include/zig/print.zig");
45

56
const global_allocator = std.heap.c_allocator;
67

78
var buffer: [1024]u8 = undefined;
89
var fixed_allocator = std.heap.FixedBufferAllocator.init(buffer[0..]);
910
var allocator = fixed_allocator.allocator();
1011

11-
fn printFmt(comptime fmt: []const u8, args: anytype) !void {
12-
var buf: [256]u8 = undefined;
13-
const out = try std.fmt.bufPrint(&buf, fmt, args);
14-
_ = try std.posix.write(std.posix.STDOUT_FILENO, out);
15-
}
1612

1713
pub fn main() !void {
1814
const n = try get_n();
@@ -88,7 +84,7 @@ pub fn main() !void {
8884
}
8985
}
9086

91-
try printFmt("{d}\nPfannkuchen({d}) = {d}\n", .{ checksum, n, max_flips_count });
87+
try print.printFmt("{d}\nPfannkuchen({d}) = {d}\n", .{ checksum, n, max_flips_count });
9288
}
9389

9490
fn get_n() !usize {

bench/algorithm/fannkuch-redux/2-m.zig

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const print = @import("../../include/zig/print.zig");
23

34
const max_n = 12;
45
const Vec = @Vector(max_n, u8);
@@ -128,14 +129,9 @@ pub fn main() !void {
128129
const perms_count = factorialComptime(n);
129130
try runInParallel(tasks, perms_count, pfannkuchenStats, .{ n, &stats });
130131

131-
try printFmt("{d}\nPfannkuchen({d}) = {d}\n", .{ stats.checksum, n, stats.max_flips });
132+
try print.printFmt("{d}\nPfannkuchen({d}) = {d}\n", .{ stats.checksum, n, stats.max_flips });
132133
}
133134

134-
fn printFmt(comptime fmt: []const u8, args: anytype) !void {
135-
var buf: [256]u8 = undefined;
136-
const out = try std.fmt.bufPrint(&buf, fmt, args);
137-
_ = try std.posix.write(std.posix.STDOUT_FILENO, out);
138-
}
139135

140136
fn get_n() !u8 {
141137
var arg_it = std.process.args();

bench/algorithm/fannkuch-redux/2.zig

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const print = @import("../../include/zig/print.zig");
23

34
const Vec = @Vector(16, u8);
45

@@ -62,14 +63,9 @@ pub fn main() !void {
6263
for (count[1..r], 0..) |*v, i| v.* = @intCast(i + 2);
6364
}
6465

65-
try printFmt("{d}\nPfannkuchen({d}) = {d}\n", .{ checksum, n, max_flip_count });
66+
try print.printFmt("{d}\nPfannkuchen({d}) = {d}\n", .{ checksum, n, max_flip_count });
6667
}
6768

68-
fn printFmt(comptime fmt: []const u8, args: anytype) !void {
69-
var buf: [256]u8 = undefined;
70-
const out = try std.fmt.bufPrint(&buf, fmt, args);
71-
_ = try std.posix.write(std.posix.STDOUT_FILENO, out);
72-
}
7369

7470
fn get_n() !u8 {
7571
var arg_it = std.process.args();

bench/algorithm/fannkuch-redux/3-i.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
//
2424

2525
const std = @import("std");
26+
const print = @import("../../include/zig/print.zig");
27+
28+
2629
const u8x16 = @Vector(16, u8);
2730

2831
fn get_n() !u4 {
@@ -35,9 +38,7 @@ fn get_n() !u4 {
3538
pub fn main() !void {
3639
const n = try get_n();
3740
const x = fannkuchRedux(n);
38-
var buf: [128]u8 = undefined;
39-
const out = try std.fmt.bufPrint(&buf, "{}\nPfannkuchen({}) = {}\n", .{ x[0], n, x[1] });
40-
_ = try std.posix.write(std.posix.STDOUT_FILENO, out);
41+
try print.printFmt("{}\nPfannkuchen({}) = {}\n", .{ x[0], n, x[1] });
4142
}
4243

4344
inline fn shuffle_epi8(x: u8x16, mask: u8x16) u8x16 {

bench/algorithm/helloworld/1.zig

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
const std = @import("std");
2+
const print = @import("../../include/zig/print.zig");
23
const global_allocator = std.heap.c_allocator;
34

4-
fn printFmt(comptime fmt: []const u8, args: anytype) !void {
5-
var buf: [256]u8 = undefined;
6-
const out = try std.fmt.bufPrint(&buf, fmt, args);
7-
_ = try std.posix.write(std.posix.STDOUT_FILENO, out);
8-
}
9-
105
pub fn main() !void {
116
const args = try std.process.argsAlloc(global_allocator);
127
defer std.process.argsFree(global_allocator, args);
138
if (args.len > 1) {
14-
try printFmt("Hello world {s}!\n", .{args[1]});
9+
try print.printFmt("Hello world {s}!\n", .{args[1]});
1510
} else {
16-
try printFmt("Hello world!\n", .{});
11+
try print.printFmt("Hello world!\n", .{});
1712
}
1813
}

bench/algorithm/json-serde/1.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const std = @import("std");
2+
const print = @import("../../include/zig/print.zig");
23
const json = std.json;
34

45
const global_allocator = std.heap.c_allocator;
56

7+
68
pub fn main() !void {
79
const args = try std.process.argsAlloc(global_allocator);
810
defer std.process.argsFree(global_allocator, args);
@@ -48,9 +50,7 @@ fn printHash(bytes: []const u8) !void {
4850
const Md5 = std.crypto.hash.Md5;
4951
var hash: [Md5.digest_length]u8 = undefined;
5052
Md5.hash(bytes, &hash, .{});
51-
var buf: [256]u8 = undefined;
52-
const out = try std.fmt.bufPrint(&buf, "{s}\n", .{std.fmt.fmtSliceHexLower(&hash)});
53-
_ = try std.posix.write(std.posix.STDOUT_FILENO, out);
53+
try print.printFmt("{s}\n", .{std.fmt.fmtSliceHexLower(&hash)});
5454
}
5555

5656
const GeoData = struct {

bench/algorithm/knucleotide/1-m.zig

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
const std = @import("std");
2+
const print = @import("../../include/zig/print.zig");
23

34
const gpa = std.heap.c_allocator;
45

5-
fn printFmt(comptime fmt: []const u8, args: anytype) !void {
6-
var buf: [256]u8 = undefined;
7-
const out = try std.fmt.bufPrint(&buf, fmt, args);
8-
_ = try std.posix.write(std.posix.STDOUT_FILENO, out);
9-
}
106

117
const Code = struct {
128
data: u64,
@@ -134,12 +130,12 @@ fn printMap(frame: usize, maps: []const Map) !void {
134130
std.mem.sort(CountCode, cc_storage[0..cc_len], {}, CountCode.dsc);
135131

136132
for (cc_storage[0..cc_len]) |c| {
137-
try printFmt("{!s} {d:.3}\n", .{
133+
try print.printFmt("{!s} {d:.3}\n", .{
138134
c.code.toString(frame),
139135
@as(f32, @floatFromInt(c.count)) / @as(f32, @floatFromInt(total)) * 100.0,
140136
});
141137
}
142-
try printFmt("\n", .{});
138+
try print.printFmt("\n", .{});
143139
}
144140

145141
fn printOcc(occ: []const u8, maps: []const Map) !void {
@@ -148,7 +144,7 @@ fn printOcc(occ: []const u8, maps: []const Map) !void {
148144
for (maps) |m| {
149145
if (m.get(code)) |count| total += count;
150146
}
151-
try printFmt("{}\t{s}\n", .{ total, occ });
147+
try print.printFmt("{}\t{s}\n", .{ total, occ });
152148
}
153149

154150
fn runInParallel(task_count: usize, len: usize, comptime f: anytype, args: anytype) !void {

0 commit comments

Comments
 (0)