Skip to content

Merge PeekStream and BufferedInStream #4878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions lib/std/io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ pub const bufferedOutStream = @import("io/buffered_out_stream.zig").bufferedOutS
pub const BufferedInStream = @import("io/buffered_in_stream.zig").BufferedInStream;
pub const bufferedInStream = @import("io/buffered_in_stream.zig").bufferedInStream;

pub const PeekStream = @import("io/peek_stream.zig").PeekStream;
pub const peekStream = @import("io/peek_stream.zig").peekStream;

pub const FixedBufferStream = @import("io/fixed_buffer_stream.zig").FixedBufferStream;
pub const fixedBufferStream = @import("io/fixed_buffer_stream.zig").fixedBufferStream;

Expand Down Expand Up @@ -151,7 +148,6 @@ test "" {
_ = @import("io/fixed_buffer_stream.zig");
_ = @import("io/in_stream.zig");
_ = @import("io/out_stream.zig");
_ = @import("io/peek_stream.zig");
_ = @import("io/seekable_stream.zig");
_ = @import("io/serialization.zig");
_ = @import("io/stream_source.zig");
Expand Down
88 changes: 83 additions & 5 deletions lib/std/io/buffered_in_stream.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,50 @@ const io = std.io;
const assert = std.debug.assert;
const testing = std.testing;

pub fn BufferedInStream(comptime buffer_size: usize, comptime InStreamType: type) type {
/// Creates a readable stream which buffers data
/// It also supports 'un-reading' data, so that it can be read again.
/// This makes look-ahead style parsing much easier.
pub fn BufferedInStream(comptime buffer_type: std.fifo.LinearFifoBufferType, comptime InStreamType: type) type {
return struct {
unbuffered_in_stream: InStreamType,
fifo: FifoType = FifoType.init(),
fifo: FifoType,

pub const Error = InStreamType.Error;
pub const InStream = io.InStream(*Self, Error, read);

const Self = @This();
const FifoType = std.fifo.LinearFifo(u8, std.fifo.LinearFifoBufferType{ .Static = buffer_size });
const FifoType = std.fifo.LinearFifo(u8, buffer_type);

pub usingnamespace switch (buffer_type) {
.Static => struct {
pub fn init(unbuffered_in_stream: InStreamType) Self {
return .{
.unbuffered_in_stream = unbuffered_in_stream,
.fifo = FifoType.init(),
};
}
},
.Slice => struct {
pub fn init(unbuffered_in_stream: InStreamType, buf: []u8) Self {
return .{
.unbuffered_in_stream = unbuffered_in_stream,
.fifo = FifoType.init(buf),
};
}
},
.Dynamic => struct {
pub fn init(unbuffered_in_stream: InStreamType, allocator: *mem.Allocator) Self {
return .{
.unbuffered_in_stream = unbuffered_in_stream,
.fifo = FifoType.init(allocator),
};
}
},
};

pub fn deinit(self: Self) void {
self.fifo.deinit();
}

pub fn read(self: *Self, dest: []u8) Error!usize {
var dest_index: usize = 0;
Expand All @@ -35,14 +69,24 @@ pub fn BufferedInStream(comptime buffer_size: usize, comptime InStreamType: type
return dest.len;
}

pub fn putBackByte(self: *Self, byte: u8) !void {
try self.putBack(&[_]u8{byte});
}

pub fn putBack(self: *Self, bytes: []const u8) !void {
try self.fifo.unget(bytes);
}

pub fn inStream(self: *Self) InStream {
return .{ .context = self };
}
};
}

pub fn bufferedInStream(underlying_stream: var) BufferedInStream(4096, @TypeOf(underlying_stream)) {
return .{ .unbuffered_in_stream = underlying_stream };
/// A buffering wrapper around another stream
/// Uses a statically allocated buffer; no need to .deinit().
pub fn bufferedInStream(underlying_stream: var) BufferedInStream(.{ .Static = 4096 }, @TypeOf(underlying_stream)) {
return BufferedInStream(.{ .Static = 4096 }, @TypeOf(underlying_stream)).init(underlying_stream);
}

test "io.BufferedInStream" {
Expand Down Expand Up @@ -84,3 +128,37 @@ test "io.BufferedInStream" {
defer testing.allocator.free(res);
testing.expectEqualSlices(u8, str, res);
}

test "io.BufferedInStream.putBack*" {
const bytes = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
var fbs = io.fixedBufferStream(&bytes);
var ps = BufferedInStream(.{ .Static = 2 }, @TypeOf(fbs.inStream())).init(fbs.inStream());
defer ps.deinit();

var dest: [4]u8 = undefined;

try ps.putBackByte(9);
try ps.putBackByte(10);

var read = try ps.inStream().read(dest[0..4]);
testing.expectEqual(@as(usize, 4), read);
testing.expectEqual(@as(u8, 10), dest[0]);
testing.expectEqual(@as(u8, 9), dest[1]);
testing.expectEqualSlices(u8, bytes[0..2], dest[2..4]);

read = try ps.inStream().read(dest[0..4]);
testing.expectEqual(@as(usize, 4), read);
testing.expectEqualSlices(u8, bytes[2..6], dest[0..4]);

read = try ps.inStream().read(dest[0..4]);
testing.expectEqual(@as(usize, 2), read);
testing.expectEqualSlices(u8, bytes[6..8], dest[0..2]);

try ps.putBackByte(11);
try ps.putBackByte(12);

read = try ps.inStream().read(dest[0..4]);
testing.expectEqual(@as(usize, 2), read);
testing.expectEqual(@as(u8, 12), dest[0]);
testing.expectEqual(@as(u8, 11), dest[1]);
}
112 changes: 0 additions & 112 deletions lib/std/io/peek_stream.zig

This file was deleted.