| ... | ... | @@ -1,116 +0,0 @@ |
| 1 | | const std = @import("../std.zig"); |
| 2 | | const assert = std.debug.assert; |
| 3 | | const io = std.io; |
| 4 | | const mem = std.mem; |
| 5 | | const testing = std.testing; |
| 6 | | |
| 7 | | /// Creates a stream which supports 'un-reading' data, so that it can be read again. |
| 8 | | /// This makes look-ahead style parsing much easier. |
| 9 | | /// TODO merge this with `std.io.BufferedReader`: https://github.com/ziglang/zig/issues/4501 |
| 10 | | pub fn PeekStream( |
| 11 | | comptime buffer_type: std.fifo.LinearFifoBufferType, |
| 12 | | comptime ReaderType: type, |
| 13 | | ) type { |
| 14 | | return struct { |
| 15 | | unbuffered_reader: ReaderType, |
| 16 | | fifo: FifoType, |
| 17 | | |
| 18 | | pub const Error = ReaderType.Error; |
| 19 | | pub const Reader = io.Reader(*Self, Error, read); |
| 20 | | |
| 21 | | const Self = @This(); |
| 22 | | const FifoType = std.fifo.LinearFifo(u8, buffer_type); |
| 23 | | |
| 24 | | pub const init = switch (buffer_type) { |
| 25 | | .Static => initStatic, |
| 26 | | .Slice => initSlice, |
| 27 | | .Dynamic => initDynamic, |
| 28 | | }; |
| 29 | | |
| 30 | | fn initStatic(base: ReaderType) Self { |
| 31 | | comptime assert(buffer_type == .Static); |
| 32 | | return .{ |
| 33 | | .unbuffered_reader = base, |
| 34 | | .fifo = FifoType.init(), |
| 35 | | }; |
| 36 | | } |
| 37 | | |
| 38 | | fn initSlice(base: ReaderType, buf: []u8) Self { |
| 39 | | comptime assert(buffer_type == .Slice); |
| 40 | | return .{ |
| 41 | | .unbuffered_reader = base, |
| 42 | | .fifo = FifoType.init(buf), |
| 43 | | }; |
| 44 | | } |
| 45 | | |
| 46 | | fn initDynamic(base: ReaderType, allocator: mem.Allocator) Self { |
| 47 | | comptime assert(buffer_type == .Dynamic); |
| 48 | | return .{ |
| 49 | | .unbuffered_reader = base, |
| 50 | | .fifo = FifoType.init(allocator), |
| 51 | | }; |
| 52 | | } |
| 53 | | |
| 54 | | pub fn putBackByte(self: *Self, byte: u8) !void { |
| 55 | | try self.putBack(&[_]u8{byte}); |
| 56 | | } |
| 57 | | |
| 58 | | pub fn putBack(self: *Self, bytes: []const u8) !void { |
| 59 | | try self.fifo.unget(bytes); |
| 60 | | } |
| 61 | | |
| 62 | | pub fn read(self: *Self, dest: []u8) Error!usize { |
| 63 | | // copy over anything putBack()'d |
| 64 | | var dest_index = self.fifo.read(dest); |
| 65 | | if (dest_index == dest.len) return dest_index; |
| 66 | | |
| 67 | | // ask the backing stream for more |
| 68 | | dest_index += try self.unbuffered_reader.read(dest[dest_index..]); |
| 69 | | return dest_index; |
| 70 | | } |
| 71 | | |
| 72 | | pub fn reader(self: *Self) Reader { |
| 73 | | return .{ .context = self }; |
| 74 | | } |
| 75 | | }; |
| 76 | | } |
| 77 | | |
| 78 | | pub fn peekStream( |
| 79 | | comptime lookahead: comptime_int, |
| 80 | | underlying_stream: anytype, |
| 81 | | ) PeekStream(.{ .Static = lookahead }, @TypeOf(underlying_stream)) { |
| 82 | | return PeekStream(.{ .Static = lookahead }, @TypeOf(underlying_stream)).init(underlying_stream); |
| 83 | | } |
| 84 | | |
| 85 | | test "PeekStream" { |
| 86 | | const bytes = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 87 | | var fbs = io.fixedBufferStream(&bytes); |
| 88 | | var ps = peekStream(2, fbs.reader()); |
| 89 | | |
| 90 | | var dest: [4]u8 = undefined; |
| 91 | | |
| 92 | | try ps.putBackByte(9); |
| 93 | | try ps.putBackByte(10); |
| 94 | | |
| 95 | | var read = try ps.reader().read(dest[0..4]); |
| 96 | | try testing.expect(read == 4); |
| 97 | | try testing.expect(dest[0] == 10); |
| 98 | | try testing.expect(dest[1] == 9); |
| 99 | | try testing.expect(mem.eql(u8, dest[2..4], bytes[0..2])); |
| 100 | | |
| 101 | | read = try ps.reader().read(dest[0..4]); |
| 102 | | try testing.expect(read == 4); |
| 103 | | try testing.expect(mem.eql(u8, dest[0..4], bytes[2..6])); |
| 104 | | |
| 105 | | read = try ps.reader().read(dest[0..4]); |
| 106 | | try testing.expect(read == 2); |
| 107 | | try testing.expect(mem.eql(u8, dest[0..2], bytes[6..8])); |
| 108 | | |
| 109 | | try ps.putBackByte(11); |
| 110 | | try ps.putBackByte(12); |
| 111 | | |
| 112 | | read = try ps.reader().read(dest[0..4]); |
| 113 | | try testing.expect(read == 2); |
| 114 | | try testing.expect(dest[0] == 12); |
| 115 | | try testing.expect(dest[1] == 11); |
| 116 | | } |