| ... | ... | @@ -196,7 +196,7 @@ test "io.BufferedInStream" { |
| 196 | 196 | |
| 197 | 197 | /// Creates a stream which supports 'un-reading' data, so that it can be read again. |
| 198 | 198 | /// This makes look-ahead style parsing much easier. |
| 199 | | pub fn PeekStream(comptime buffer_type: usize, comptime InStreamError: type) type { |
| 199 | pub fn PeekStream(comptime buffer_type: std.fifo.LinearFifoBufferType, comptime InStreamError: type) type { |
| 200 | 200 | return struct { |
| 201 | 201 | const Self = @This(); |
| 202 | 202 | pub const Error = InStreamError; |
| ... | ... | @@ -205,18 +205,38 @@ pub fn PeekStream(comptime buffer_type: usize, comptime InStreamError: type) typ |
| 205 | 205 | stream: Stream, |
| 206 | 206 | base: *Stream, |
| 207 | 207 | |
| 208 | | // Right now the look-ahead space is statically allocated, but a version with dynamic allocation |
| 209 | | // is not too difficult to derive from this. |
| 210 | | const FifoType = std.fifo.LinearFifo(u8, .{ .Static = buffer_size }); |
| 208 | const FifoType = std.fifo.LinearFifo(u8, buffer_type); |
| 211 | 209 | fifo: FifoType, |
| 212 | 210 | |
| 213 | | pub fn init(base: *Stream) Self { |
| 214 | | return .{ |
| 215 | | .base = base, |
| 216 | | .fifo = FifoType.init(), |
| 217 | | .stream = Stream{ .readFn = readFn }, |
| 218 | | }; |
| 219 | | } |
| 211 | pub usingnamespace switch (buffer_type) { |
| 212 | .Static => struct { |
| 213 | pub fn init(base: *Stream) Self { |
| 214 | return .{ |
| 215 | .base = base, |
| 216 | .fifo = FifoType.init(), |
| 217 | .stream = Stream{ .readFn = readFn }, |
| 218 | }; |
| 219 | } |
| 220 | }, |
| 221 | .Slice => struct { |
| 222 | pub fn init(base: *Stream, buf: []u8) Self { |
| 223 | return .{ |
| 224 | .base = base, |
| 225 | .fifo = FifoType.init(buf), |
| 226 | .stream = Stream{ .readFn = readFn }, |
| 227 | }; |
| 228 | } |
| 229 | }, |
| 230 | .Dynamic => struct { |
| 231 | pub fn init(base: *Stream, allocator: *mem.Allocator) Self { |
| 232 | return .{ |
| 233 | .base = base, |
| 234 | .fifo = FifoType.init(allocator), |
| 235 | .stream = Stream{ .readFn = readFn }, |
| 236 | }; |
| 237 | } |
| 238 | }, |
| 239 | }; |
| 220 | 240 | |
| 221 | 241 | pub fn putBackByte(self: *Self, byte: u8) !void { |
| 222 | 242 | try self.putBack(@ptrCast([*]const u8, &byte)[0..1]); |