authorgravatar for lachlan@lakebythewoods.xyzLachlan Easton <lachlan@lakebythewoods.xyz> 2020-08-29 23:07:47+10:00
committergravatar for lachlan@lakebythewoods.xyzLachlan Easton <lachlan@lakebythewoods.xyz> 2020-08-30 10:28:17+10:00
log7d950210a64f51cba6c4edaacbd9c67f12e72604
tree7ea4e9ff643b94afd0865a6d28de4003b412e23c
parenta72b9d403d036ac0da64ed826c535fff0c142e6a

zig fmt review comments


3 files changed, 31 insertions(+), 25 deletions(-)

lib/std/io/auto_indenting_stream.zig+14-9
......@@ -3,13 +3,15 @@ const io = std.io;
33const mem = std.mem;
44const assert = std.debug.assert;
55
6pub fn AutoIndentingStream(comptime indent_delta: u8, comptime OutStreamType: type) type {
6/// Automatically inserts indentation of written data by keeping
7/// track of the current indentation level
8pub fn AutoIndentingStream(comptime indent_delta: u8, comptime WriterType: type) type {
79 return struct {
810 const Self = @This();
9 pub const Error = OutStreamType.Error;
10 pub const OutStream = io.Writer(*Self, Error, write);
11 pub const Error = WriterType.Error;
12 pub const Writer = io.Writer(*Self, Error, write);
1113
12 out_stream: *OutStreamType,
14 writer_pointer: *WriterType,
1315 current_line_empty: bool = true,
1416 indent_stack: [255]u8 = undefined,
1517 indent_stack_top: u8 = 0,
......@@ -17,11 +19,11 @@ pub fn AutoIndentingStream(comptime indent_delta: u8, comptime OutStreamType: ty
1719 applied_indent: u8 = 0, // the most recently applied indent
1820 indent_next_line: u8 = 0, // not used until the next line
1921
20 pub fn init(out_stream: *OutStreamType) Self {
21 return Self{ .out_stream = out_stream };
22 pub fn init(writer_pointer: *WriterType) Self {
23 return Self{ .writer_pointer = writer_pointer };
2224 }
2325
24 pub fn writer(self: *Self) OutStream {
26 pub fn writer(self: *Self) Writer {
2527 return .{ .context = self };
2628 }
2729
......@@ -34,7 +36,10 @@ pub fn AutoIndentingStream(comptime indent_delta: u8, comptime OutStreamType: ty
3436 }
3537
3638 fn writeNoIndent(self: *Self, bytes: []const u8) Error!usize {
37 try self.out_stream.outStream().writeAll(bytes);
39 if (bytes.len == 0)
40 return @as(usize, 0);
41
42 try self.writer_pointer.outStream().writeAll(bytes);
3843 if (bytes[bytes.len - 1] == '\n')
3944 self.resetLine();
4045 return bytes.len;
......@@ -98,7 +103,7 @@ pub fn AutoIndentingStream(comptime indent_delta: u8, comptime OutStreamType: ty
98103 fn applyIndent(self: *Self) Error!void {
99104 const current_indent = self.currentIndent();
100105 if (self.current_line_empty and current_indent > 0) {
101 try self.out_stream.outStream().writeByteNTimes(' ', current_indent);
106 try self.writer_pointer.outStream().writeByteNTimes(' ', current_indent);
102107 self.applied_indent = current_indent;
103108 }
104109
lib/std/io/change_detection_stream.zig+9-8
......@@ -3,26 +3,27 @@ const io = std.io;
33const mem = std.mem;
44const assert = std.debug.assert;
55
6pub fn ChangeDetectionStream(comptime OutStreamType: type) type {
6/// Used to detect if the data written to a stream differs from a source buffer
7pub fn ChangeDetectionStream(comptime WriterType: type) type {
78 return struct {
89 const Self = @This();
9 pub const Error = OutStreamType.Error;
10 pub const OutStream = io.OutStream(*Self, Error, write);
10 pub const Error = WriterType.Error;
11 pub const Writer = io.Writer(*Self, Error, write);
1112
1213 anything_changed: bool = false,
13 out_stream: *OutStreamType,
14 writer_pointer: *WriterType,
1415 source_index: usize,
1516 source: []const u8,
1617
17 pub fn init(source: []const u8, out_stream: *OutStreamType) Self {
18 pub fn init(source: []const u8, writer_pointer: *WriterType) Self {
1819 return Self{
19 .out_stream = out_stream,
20 .writer_pointer = writer_pointer,
2021 .source_index = 0,
2122 .source = source,
2223 };
2324 }
2425
25 pub fn outStream(self: *Self) OutStream {
26 pub fn writer(self: *Self) Writer {
2627 return .{ .context = self };
2728 }
2829
......@@ -40,7 +41,7 @@ pub fn ChangeDetectionStream(comptime OutStreamType: type) type {
4041 }
4142 }
4243
43 return self.out_stream.write(bytes);
44 return self.writer_pointer.write(bytes);
4445 }
4546
4647 pub fn changeDetected(self: *Self) bool {
lib/std/io/find_byte_out_stream.zig+8-8
......@@ -2,21 +2,21 @@ const std = @import("../std.zig");
22const io = std.io;
33const assert = std.debug.assert;
44
5// An OutStream that returns whether the given character has been written to it.
6// The contents are not written to anything.
7pub fn FindByteOutStream(comptime OutStreamType: type) type {
5/// An OutStream that returns whether the given character has been written to it.
6/// The contents are not written to anything.
7pub fn FindByteOutStream(comptime WriterType: type) type {
88 return struct {
99 const Self = @This();
10 pub const Error = OutStreamType.Error;
10 pub const Error = WriterType.Error;
1111 pub const OutStream = io.OutStream(*Self, Error, write);
1212
13 out_stream: *OutStreamType,
13 writer_pointer: *WriterType,
1414 byte_found: bool,
1515 byte: u8,
1616
17 pub fn init(byte: u8, out_stream: *OutStreamType) Self {
17 pub fn init(byte: u8, writer_pointer: *WriterType) Self {
1818 return Self{
19 .out_stream = out_stream,
19 .writer_pointer = writer_pointer,
2020 .byte = byte,
2121 .byte_found = false,
2222 };
......@@ -34,7 +34,7 @@ pub fn FindByteOutStream(comptime OutStreamType: type) type {
3434 break :blk false;
3535 };
3636 }
37 return self.out_stream.writer().write(bytes);
37 return self.writer_pointer.writer().write(bytes);
3838 }
3939 };
4040}