diff options
| author | Lachlan Easton <lachlan@lakebythewoods.xyz> | 2020-08-29 23:07:47 +1000 |
|---|---|---|
| committer | Lachlan Easton <lachlan@lakebythewoods.xyz> | 2020-08-30 10:28:17 +1000 |
| commit | 7d950210a64f51cba6c4edaacbd9c67f12e72604 (patch) | |
| tree | 7ea4e9ff643b94afd0865a6d28de4003b412e23c /lib/std | |
| parent | a72b9d403d036ac0da64ed826c535fff0c142e6a (diff) | |
| download | zig-7d950210a64f51cba6c4edaacbd9c67f12e72604.tar.gz zig-7d950210a64f51cba6c4edaacbd9c67f12e72604.zip | |
zig fmt review comments
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/io/auto_indenting_stream.zig | 23 | ||||
| -rw-r--r-- | lib/std/io/change_detection_stream.zig | 17 | ||||
| -rw-r--r-- | lib/std/io/find_byte_out_stream.zig | 16 |
3 files changed, 31 insertions, 25 deletions
diff --git a/lib/std/io/auto_indenting_stream.zig b/lib/std/io/auto_indenting_stream.zig index e7657c1f91..227dd616a1 100644 --- a/lib/std/io/auto_indenting_stream.zig +++ b/lib/std/io/auto_indenting_stream.zig @@ -3,13 +3,15 @@ const io = std.io; const mem = std.mem; const assert = std.debug.assert; -pub fn AutoIndentingStream(comptime indent_delta: u8, comptime OutStreamType: type) type { +/// Automatically inserts indentation of written data by keeping +/// track of the current indentation level +pub fn AutoIndentingStream(comptime indent_delta: u8, comptime WriterType: type) type { return struct { const Self = @This(); - pub const Error = OutStreamType.Error; - pub const OutStream = io.Writer(*Self, Error, write); + pub const Error = WriterType.Error; + pub const Writer = io.Writer(*Self, Error, write); - out_stream: *OutStreamType, + writer_pointer: *WriterType, current_line_empty: bool = true, indent_stack: [255]u8 = undefined, indent_stack_top: u8 = 0, @@ -17,11 +19,11 @@ pub fn AutoIndentingStream(comptime indent_delta: u8, comptime OutStreamType: ty applied_indent: u8 = 0, // the most recently applied indent indent_next_line: u8 = 0, // not used until the next line - pub fn init(out_stream: *OutStreamType) Self { - return Self{ .out_stream = out_stream }; + pub fn init(writer_pointer: *WriterType) Self { + return Self{ .writer_pointer = writer_pointer }; } - pub fn writer(self: *Self) OutStream { + pub fn writer(self: *Self) Writer { return .{ .context = self }; } @@ -34,7 +36,10 @@ pub fn AutoIndentingStream(comptime indent_delta: u8, comptime OutStreamType: ty } fn writeNoIndent(self: *Self, bytes: []const u8) Error!usize { - try self.out_stream.outStream().writeAll(bytes); + if (bytes.len == 0) + return @as(usize, 0); + + try self.writer_pointer.outStream().writeAll(bytes); if (bytes[bytes.len - 1] == '\n') self.resetLine(); return bytes.len; @@ -98,7 +103,7 @@ pub fn AutoIndentingStream(comptime indent_delta: u8, comptime OutStreamType: ty fn applyIndent(self: *Self) Error!void { const current_indent = self.currentIndent(); if (self.current_line_empty and current_indent > 0) { - try self.out_stream.outStream().writeByteNTimes(' ', current_indent); + try self.writer_pointer.outStream().writeByteNTimes(' ', current_indent); self.applied_indent = current_indent; } diff --git a/lib/std/io/change_detection_stream.zig b/lib/std/io/change_detection_stream.zig index 941569320c..98c8130b44 100644 --- a/lib/std/io/change_detection_stream.zig +++ b/lib/std/io/change_detection_stream.zig @@ -3,26 +3,27 @@ const io = std.io; const mem = std.mem; const assert = std.debug.assert; -pub fn ChangeDetectionStream(comptime OutStreamType: type) type { +/// Used to detect if the data written to a stream differs from a source buffer +pub fn ChangeDetectionStream(comptime WriterType: type) type { return struct { const Self = @This(); - pub const Error = OutStreamType.Error; - pub const OutStream = io.OutStream(*Self, Error, write); + pub const Error = WriterType.Error; + pub const Writer = io.Writer(*Self, Error, write); anything_changed: bool = false, - out_stream: *OutStreamType, + writer_pointer: *WriterType, source_index: usize, source: []const u8, - pub fn init(source: []const u8, out_stream: *OutStreamType) Self { + pub fn init(source: []const u8, writer_pointer: *WriterType) Self { return Self{ - .out_stream = out_stream, + .writer_pointer = writer_pointer, .source_index = 0, .source = source, }; } - pub fn outStream(self: *Self) OutStream { + pub fn writer(self: *Self) Writer { return .{ .context = self }; } @@ -40,7 +41,7 @@ pub fn ChangeDetectionStream(comptime OutStreamType: type) type { } } - return self.out_stream.write(bytes); + return self.writer_pointer.write(bytes); } pub fn changeDetected(self: *Self) bool { diff --git a/lib/std/io/find_byte_out_stream.zig b/lib/std/io/find_byte_out_stream.zig index e835cbd584..b316a98549 100644 --- a/lib/std/io/find_byte_out_stream.zig +++ b/lib/std/io/find_byte_out_stream.zig @@ -2,21 +2,21 @@ const std = @import("../std.zig"); const io = std.io; const assert = std.debug.assert; -// An OutStream that returns whether the given character has been written to it. -// The contents are not written to anything. -pub fn FindByteOutStream(comptime OutStreamType: type) type { +/// An OutStream that returns whether the given character has been written to it. +/// The contents are not written to anything. +pub fn FindByteOutStream(comptime WriterType: type) type { return struct { const Self = @This(); - pub const Error = OutStreamType.Error; + pub const Error = WriterType.Error; pub const OutStream = io.OutStream(*Self, Error, write); - out_stream: *OutStreamType, + writer_pointer: *WriterType, byte_found: bool, byte: u8, - pub fn init(byte: u8, out_stream: *OutStreamType) Self { + pub fn init(byte: u8, writer_pointer: *WriterType) Self { return Self{ - .out_stream = out_stream, + .writer_pointer = writer_pointer, .byte = byte, .byte_found = false, }; @@ -34,7 +34,7 @@ pub fn FindByteOutStream(comptime OutStreamType: type) type { break :blk false; }; } - return self.out_stream.writer().write(bytes); + return self.writer_pointer.writer().write(bytes); } }; } |
