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