| ... | ... | @@ -331,6 +331,104 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type) |
| 331 | 331 | }; |
| 332 | 332 | } |
| 333 | 333 | |
| 334 | /// Creates a stream which supports 'un-reading' data, so that it can be read again. |
| 335 | /// This makes look-ahead style parsing much easier. |
| 336 | pub fn PeekStream(comptime buffer_size: usize, comptime InStreamError: type) type { |
| 337 | return struct { |
| 338 | const Self = this; |
| 339 | pub const Error = InStreamError; |
| 340 | pub const Stream = InStream(Error); |
| 341 | |
| 342 | pub stream: Stream, |
| 343 | base: *Stream, |
| 344 | |
| 345 | // Right now the look-ahead space is statically allocated, but a version with dynamic allocation |
| 346 | // is not too difficult to derive from this. |
| 347 | buffer: [buffer_size]u8, |
| 348 | index: usize, |
| 349 | at_end: bool, |
| 350 | |
| 351 | pub fn init(base: *Stream) Self { |
| 352 | return Self{ |
| 353 | .base = base, |
| 354 | .buffer = undefined, |
| 355 | .index = 0, |
| 356 | .at_end = false, |
| 357 | .stream = Stream{ .readFn = readFn }, |
| 358 | }; |
| 359 | } |
| 360 | |
| 361 | pub fn putBackByte(self: *Self, byte: u8) void { |
| 362 | self.buffer[self.index] = byte; |
| 363 | self.index += 1; |
| 364 | } |
| 365 | |
| 366 | pub fn putBack(self: *Self, bytes: []const u8) void { |
| 367 | var pos = bytes.len; |
| 368 | while (pos != 0) { |
| 369 | pos -= 1; |
| 370 | self.putBackByte(bytes[pos]); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | fn readFn(in_stream: *Stream, dest: []u8) Error!usize { |
| 375 | const self = @fieldParentPtr(Self, "stream", in_stream); |
| 376 | |
| 377 | // copy over anything putBack()'d |
| 378 | var pos: usize = 0; |
| 379 | while (pos < dest.len and self.index != 0) { |
| 380 | dest[pos] = self.buffer[self.index - 1]; |
| 381 | self.index -= 1; |
| 382 | pos += 1; |
| 383 | } |
| 384 | |
| 385 | if (pos == dest.len or self.at_end) { |
| 386 | return pos; |
| 387 | } |
| 388 | |
| 389 | // ask the backing stream for more |
| 390 | const left = dest.len - pos; |
| 391 | const read = try self.base.read(dest[pos..]); |
| 392 | assert(read <= left); |
| 393 | |
| 394 | self.at_end = (read < left); |
| 395 | return pos + read; |
| 396 | } |
| 397 | |
| 398 | }; |
| 399 | } |
| 400 | |
| 401 | pub const SliceStream = struct { |
| 402 | const Self = this; |
| 403 | pub const Error = error { }; |
| 404 | pub const Stream = InStream(Error); |
| 405 | |
| 406 | pub stream: Stream, |
| 407 | |
| 408 | pos: usize, |
| 409 | slice: []const u8, |
| 410 | |
| 411 | pub fn init(slice: []const u8) Self { |
| 412 | return Self{ |
| 413 | .slice = slice, |
| 414 | .pos = 0, |
| 415 | .stream = Stream{ .readFn = readFn }, |
| 416 | }; |
| 417 | } |
| 418 | |
| 419 | fn readFn(in_stream: *Stream, dest: []u8) Error!usize { |
| 420 | const self = @fieldParentPtr(Self, "stream", in_stream); |
| 421 | const size = math.min(dest.len, self.slice.len - self.pos); |
| 422 | const end = self.pos + size; |
| 423 | |
| 424 | mem.copy(u8, dest[0..size], self.slice[self.pos..end]); |
| 425 | self.pos = end; |
| 426 | |
| 427 | return size; |
| 428 | } |
| 429 | |
| 430 | }; |
| 431 | |
| 334 | 432 | pub fn BufferedOutStream(comptime Error: type) type { |
| 335 | 433 | return BufferedOutStreamCustom(os.page_size, Error); |
| 336 | 434 | } |