| ... | ... | @@ -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_size: usize, comptime InStreamError: type) type { |
| 199 | pub fn PeekStream(comptime buffer_type: usize, comptime InStreamError: type) type { |
| 200 | 200 | return struct { |
| 201 | 201 | const Self = @This(); |
| 202 | 202 | pub const Error = InStreamError; |
| ... | ... | @@ -207,55 +207,35 @@ pub fn PeekStream(comptime buffer_size: usize, comptime InStreamError: type) typ |
| 207 | 207 | |
| 208 | 208 | // Right now the look-ahead space is statically allocated, but a version with dynamic allocation |
| 209 | 209 | // is not too difficult to derive from this. |
| 210 | | buffer: [buffer_size]u8, |
| 211 | | index: usize, |
| 212 | | at_end: bool, |
| 210 | const FifoType = std.fifo.LinearFifo(u8, .{ .Static = buffer_size }); |
| 211 | fifo: FifoType, |
| 213 | 212 | |
| 214 | 213 | pub fn init(base: *Stream) Self { |
| 215 | | return Self{ |
| 214 | return .{ |
| 216 | 215 | .base = base, |
| 217 | | .buffer = undefined, |
| 218 | | .index = 0, |
| 219 | | .at_end = false, |
| 216 | .fifo = FifoType.init(), |
| 220 | 217 | .stream = Stream{ .readFn = readFn }, |
| 221 | 218 | }; |
| 222 | 219 | } |
| 223 | 220 | |
| 224 | | pub fn putBackByte(self: *Self, byte: u8) void { |
| 225 | | self.buffer[self.index] = byte; |
| 226 | | self.index += 1; |
| 221 | pub fn putBackByte(self: *Self, byte: u8) !void { |
| 222 | try self.putBack(@ptrCast([*]const u8, &byte)[0..1]); |
| 227 | 223 | } |
| 228 | 224 | |
| 229 | | pub fn putBack(self: *Self, bytes: []const u8) void { |
| 230 | | var pos = bytes.len; |
| 231 | | while (pos != 0) { |
| 232 | | pos -= 1; |
| 233 | | self.putBackByte(bytes[pos]); |
| 234 | | } |
| 225 | pub fn putBack(self: *Self, bytes: []const u8) !void { |
| 226 | try self.fifo.unget(bytes); |
| 235 | 227 | } |
| 236 | 228 | |
| 237 | 229 | fn readFn(in_stream: *Stream, dest: []u8) Error!usize { |
| 238 | 230 | const self = @fieldParentPtr(Self, "stream", in_stream); |
| 239 | 231 | |
| 240 | 232 | // copy over anything putBack()'d |
| 241 | | var pos: usize = 0; |
| 242 | | while (pos < dest.len and self.index != 0) { |
| 243 | | dest[pos] = self.buffer[self.index - 1]; |
| 244 | | self.index -= 1; |
| 245 | | pos += 1; |
| 246 | | } |
| 247 | | |
| 248 | | if (pos == dest.len or self.at_end) { |
| 249 | | return pos; |
| 250 | | } |
| 233 | var dest_index = self.fifo.read(dest); |
| 234 | if (dest_index == dest.len) return dest_index; |
| 251 | 235 | |
| 252 | 236 | // ask the backing stream for more |
| 253 | | const left = dest.len - pos; |
| 254 | | const read = try self.base.read(dest[pos..]); |
| 255 | | assert(read <= left); |
| 256 | | |
| 257 | | self.at_end = (read < left); |
| 258 | | return pos + read; |
| 237 | dest_index += try self.base.read(dest[dest_index..]); |
| 238 | return dest_index; |
| 259 | 239 | } |
| 260 | 240 | }; |
| 261 | 241 | } |