| author | |
| committer | |
| log | 7d950210a64f51cba6c4edaacbd9c67f12e72604 |
| tree | 7ea4e9ff643b94afd0865a6d28de4003b412e23c |
| parent | a72b9d403d036ac0da64ed826c535fff0c142e6a |
3 files changed, 31 insertions(+), 25 deletions(-)
lib/std/io/auto_indenting_stream.zig+14-9| ... | ... | @@ -3,13 +3,15 @@ const io = std.io; |
| 3 | 3 | const mem = std.mem; |
| 4 | 4 | const assert = std.debug.assert; |
| 5 | 5 | |
| 6 | pub 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 | |
| 8 | pub fn AutoIndentingStream(comptime indent_delta: u8, comptime WriterType: type) type { | |
| 7 | 9 | return struct { |
| 8 | 10 | 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); | |
| 11 | 13 | |
| 12 | out_stream: *OutStreamType, | |
| 14 | writer_pointer: *WriterType, | |
| 13 | 15 | current_line_empty: bool = true, |
| 14 | 16 | indent_stack: [255]u8 = undefined, |
| 15 | 17 | indent_stack_top: u8 = 0, |
| ... | ... | @@ -17,11 +19,11 @@ pub fn AutoIndentingStream(comptime indent_delta: u8, comptime OutStreamType: ty |
| 17 | 19 | applied_indent: u8 = 0, // the most recently applied indent |
| 18 | 20 | indent_next_line: u8 = 0, // not used until the next line |
| 19 | 21 | |
| 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 }; | |
| 22 | 24 | } |
| 23 | 25 | |
| 24 | pub fn writer(self: *Self) OutStream { | |
| 26 | pub fn writer(self: *Self) Writer { | |
| 25 | 27 | return .{ .context = self }; |
| 26 | 28 | } |
| 27 | 29 | |
| ... | ... | @@ -34,7 +36,10 @@ pub fn AutoIndentingStream(comptime indent_delta: u8, comptime OutStreamType: ty |
| 34 | 36 | } |
| 35 | 37 | |
| 36 | 38 | 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); | |
| 38 | 43 | if (bytes[bytes.len - 1] == '\n') |
| 39 | 44 | self.resetLine(); |
| 40 | 45 | return bytes.len; |
| ... | ... | @@ -98,7 +103,7 @@ pub fn AutoIndentingStream(comptime indent_delta: u8, comptime OutStreamType: ty |
| 98 | 103 | fn applyIndent(self: *Self) Error!void { |
| 99 | 104 | const current_indent = self.currentIndent(); |
| 100 | 105 | 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); | |
| 102 | 107 | self.applied_indent = current_indent; |
| 103 | 108 | } |
| 104 | 109 |
lib/std/io/change_detection_stream.zig+9-8| ... | ... | @@ -3,26 +3,27 @@ const io = std.io; |
| 3 | 3 | const mem = std.mem; |
| 4 | 4 | const assert = std.debug.assert; |
| 5 | 5 | |
| 6 | pub fn ChangeDetectionStream(comptime OutStreamType: type) type { | |
| 6 | /// Used to detect if the data written to a stream differs from a source buffer | |
| 7 | pub fn ChangeDetectionStream(comptime WriterType: type) type { | |
| 7 | 8 | return struct { |
| 8 | 9 | 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); | |
| 11 | 12 | |
| 12 | 13 | anything_changed: bool = false, |
| 13 | out_stream: *OutStreamType, | |
| 14 | writer_pointer: *WriterType, | |
| 14 | 15 | source_index: usize, |
| 15 | 16 | source: []const u8, |
| 16 | 17 | |
| 17 | pub fn init(source: []const u8, out_stream: *OutStreamType) Self { | |
| 18 | pub fn init(source: []const u8, writer_pointer: *WriterType) Self { | |
| 18 | 19 | return Self{ |
| 19 | .out_stream = out_stream, | |
| 20 | .writer_pointer = writer_pointer, | |
| 20 | 21 | .source_index = 0, |
| 21 | 22 | .source = source, |
| 22 | 23 | }; |
| 23 | 24 | } |
| 24 | 25 | |
| 25 | pub fn outStream(self: *Self) OutStream { | |
| 26 | pub fn writer(self: *Self) Writer { | |
| 26 | 27 | return .{ .context = self }; |
| 27 | 28 | } |
| 28 | 29 | |
| ... | ... | @@ -40,7 +41,7 @@ pub fn ChangeDetectionStream(comptime OutStreamType: type) type { |
| 40 | 41 | } |
| 41 | 42 | } |
| 42 | 43 | |
| 43 | return self.out_stream.write(bytes); | |
| 44 | return self.writer_pointer.write(bytes); | |
| 44 | 45 | } |
| 45 | 46 | |
| 46 | 47 | 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"); |
| 2 | 2 | const io = std.io; |
| 3 | 3 | const assert = std.debug.assert; |
| 4 | 4 | |
| 5 | // An OutStream that returns whether the given character has been written to it. | |
| 6 | // The contents are not written to anything. | |
| 7 | pub 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. | |
| 7 | pub fn FindByteOutStream(comptime WriterType: type) type { | |
| 8 | 8 | return struct { |
| 9 | 9 | const Self = @This(); |
| 10 | pub const Error = OutStreamType.Error; | |
| 10 | pub const Error = WriterType.Error; | |
| 11 | 11 | pub const OutStream = io.OutStream(*Self, Error, write); |
| 12 | 12 | |
| 13 | out_stream: *OutStreamType, | |
| 13 | writer_pointer: *WriterType, | |
| 14 | 14 | byte_found: bool, |
| 15 | 15 | byte: u8, |
| 16 | 16 | |
| 17 | pub fn init(byte: u8, out_stream: *OutStreamType) Self { | |
| 17 | pub fn init(byte: u8, writer_pointer: *WriterType) Self { | |
| 18 | 18 | return Self{ |
| 19 | .out_stream = out_stream, | |
| 19 | .writer_pointer = writer_pointer, | |
| 20 | 20 | .byte = byte, |
| 21 | 21 | .byte_found = false, |
| 22 | 22 | }; |
| ... | ... | @@ -34,7 +34,7 @@ pub fn FindByteOutStream(comptime OutStreamType: type) type { |
| 34 | 34 | break :blk false; |
| 35 | 35 | }; |
| 36 | 36 | } |
| 37 | return self.out_stream.writer().write(bytes); | |
| 37 | return self.writer_pointer.writer().write(bytes); | |
| 38 | 38 | } |
| 39 | 39 | }; |
| 40 | 40 | } |