Skip to content

Commit 956243d

Browse files
committed
std: merge io.PeekStream and io.BufferedInStreamCustom
1 parent e29495e commit 956243d

File tree

3 files changed

+44
-115
lines changed

3 files changed

+44
-115
lines changed

lib/std/io.zig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ pub const bufferedOutStream = @import("io/buffered_out_stream.zig").bufferedOutS
9898
pub const BufferedInStream = @import("io/buffered_in_stream.zig").BufferedInStream;
9999
pub const bufferedInStream = @import("io/buffered_in_stream.zig").bufferedInStream;
100100

101-
pub const PeekStream = @import("io/peek_stream.zig").PeekStream;
102-
pub const peekStream = @import("io/peek_stream.zig").peekStream;
103-
104101
pub const FixedBufferStream = @import("io/fixed_buffer_stream.zig").FixedBufferStream;
105102
pub const fixedBufferStream = @import("io/fixed_buffer_stream.zig").fixedBufferStream;
106103

lib/std/io/buffered_in_stream.zig

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ const io = std.io;
33
const assert = std.debug.assert;
44
const testing = std.testing;
55

6+
/// Creates a readable stream which buffers data
7+
/// It also supports 'un-reading' data, so that it can be read again.
8+
/// This makes look-ahead style parsing much easier.
69
pub fn BufferedInStream(comptime buffer_type: std.fifo.LinearFifoBufferType, comptime InStreamType: type) type {
710
return struct {
811
unbuffered_in_stream: InStreamType,
@@ -62,6 +65,14 @@ pub fn BufferedInStream(comptime buffer_type: std.fifo.LinearFifoBufferType, com
6265
return dest.len;
6366
}
6467

68+
pub fn putBackByte(self: *Self, byte: u8) !void {
69+
try self.putBack(&[_]u8{byte});
70+
}
71+
72+
pub fn putBack(self: *Self, bytes: []const u8) !void {
73+
try self.fifo.unget(bytes);
74+
}
75+
6576
pub fn inStream(self: *Self) InStream {
6677
return .{ .context = self };
6778
}
@@ -111,3 +122,36 @@ test "io.BufferedInStream" {
111122
defer testing.allocator.free(res);
112123
testing.expectEqualSlices(u8, str, res);
113124
}
125+
126+
test "io.BufferedInStream.putBack*" {
127+
const bytes = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
128+
var fbs = io.fixedBufferStream(&bytes);
129+
var ps = BufferedInStream(.{ .Static = 2 }, @TypeOf(fbs.inStream())).init(fbs.inStream());
130+
131+
var dest: [4]u8 = undefined;
132+
133+
try ps.putBackByte(9);
134+
try ps.putBackByte(10);
135+
136+
var read = try ps.inStream().read(dest[0..4]);
137+
testing.expectEqual(@as(usize, 4), read);
138+
testing.expectEqual(@as(u8, 10), dest[0]);
139+
testing.expectEqual(@as(u8, 9), dest[1]);
140+
testing.expectEqualSlices(u8, bytes[0..2], dest[2..4]);
141+
142+
read = try ps.inStream().read(dest[0..4]);
143+
testing.expectEqual(@as(usize, 4), read);
144+
testing.expectEqualSlices(u8, bytes[2..6], dest[0..4]);
145+
146+
read = try ps.inStream().read(dest[0..4]);
147+
testing.expectEqual(@as(usize, 2), read);
148+
testing.expectEqualSlices(u8, bytes[6..8], dest[0..2]);
149+
150+
try ps.putBackByte(11);
151+
try ps.putBackByte(12);
152+
153+
read = try ps.inStream().read(dest[0..4]);
154+
testing.expectEqual(@as(usize, 2), read);
155+
testing.expectEqual(@as(u8, 12), dest[0]);
156+
testing.expectEqual(@as(u8, 11), dest[1]);
157+
}

lib/std/io/peek_stream.zig

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)