| ... | @@ -392,7 +392,7 @@ pub fn readAll(r: *Reader, w: *Writer, limit: Limit) StreamError!void { | ... | @@ -392,7 +392,7 @@ pub fn readAll(r: *Reader, w: *Writer, limit: Limit) StreamError!void { |
| 392 | } | 392 | } |
| 393 | } | 393 | } |
| 394 | | 394 | |
| 395 | /// Returns the next `len` bytes from `unbuffered_reader`, filling the buffer as | 395 | /// Returns the next `len` bytes from the stream, filling the buffer as |
| 396 | /// necessary. | 396 | /// necessary. |
| 397 | /// | 397 | /// |
| 398 | /// Invalidates previously returned values from `peek`. | 398 | /// Invalidates previously returned values from `peek`. |
| ... | @@ -411,8 +411,8 @@ pub fn peek(r: *Reader, n: usize) Error![]u8 { | ... | @@ -411,8 +411,8 @@ pub fn peek(r: *Reader, n: usize) Error![]u8 { |
| 411 | return r.buffer[r.seek..][0..n]; | 411 | return r.buffer[r.seek..][0..n]; |
| 412 | } | 412 | } |
| 413 | | 413 | |
| 414 | /// Returns all the next buffered bytes from `unbuffered_reader`, after filling | 414 | /// Returns all the next buffered bytes, after filling the buffer to ensure it |
| 415 | /// the buffer to ensure it contains at least `n` bytes. | 415 | /// contains at least `n` bytes. |
| 416 | /// | 416 | /// |
| 417 | /// Invalidates previously returned values from `peek` and `peekGreedy`. | 417 | /// Invalidates previously returned values from `peek` and `peekGreedy`. |
| 418 | /// | 418 | /// |
| ... | @@ -461,8 +461,8 @@ pub fn take(r: *Reader, n: usize) Error![]u8 { | ... | @@ -461,8 +461,8 @@ pub fn take(r: *Reader, n: usize) Error![]u8 { |
| 461 | return result; | 461 | return result; |
| 462 | } | 462 | } |
| 463 | | 463 | |
| 464 | /// Returns the next `n` bytes from `unbuffered_reader` as an array, filling | 464 | /// Returns the next `n` bytes from the stream as an array, filling the buffer |
| 465 | /// the buffer as necessary and advancing the seek position `n` bytes. | 465 | /// as necessary and advancing the seek position `n` bytes. |
| 466 | /// | 466 | /// |
| 467 | /// Asserts that the `Reader` was initialized with a buffer capacity at | 467 | /// Asserts that the `Reader` was initialized with a buffer capacity at |
| 468 | /// least as big as `n`. | 468 | /// least as big as `n`. |
| ... | @@ -476,8 +476,8 @@ pub fn takeArray(r: *Reader, comptime n: usize) Error!*[n]u8 { | ... | @@ -476,8 +476,8 @@ pub fn takeArray(r: *Reader, comptime n: usize) Error!*[n]u8 { |
| 476 | return (try r.take(n))[0..n]; | 476 | return (try r.take(n))[0..n]; |
| 477 | } | 477 | } |
| 478 | | 478 | |
| 479 | /// Returns the next `n` bytes from `unbuffered_reader` as an array, filling | 479 | /// Returns the next `n` bytes from the stream as an array, filling the buffer |
| 480 | /// the buffer as necessary, without advancing the seek position. | 480 | /// as necessary, without advancing the seek position. |
| 481 | /// | 481 | /// |
| 482 | /// Asserts that the `Reader` was initialized with a buffer capacity at | 482 | /// Asserts that the `Reader` was initialized with a buffer capacity at |
| 483 | /// least as big as `n`. | 483 | /// least as big as `n`. |
| ... | @@ -538,7 +538,7 @@ pub fn discardShort(r: *Reader, n: usize) ShortError!usize { | ... | @@ -538,7 +538,7 @@ pub fn discardShort(r: *Reader, n: usize) ShortError!usize { |
| 538 | r.end = 0; | 538 | r.end = 0; |
| 539 | r.seek = 0; | 539 | r.seek = 0; |
| 540 | while (true) { | 540 | while (true) { |
| 541 | const discard_len = r.unbuffered_reader.discard(.limited(remaining)) catch |err| switch (err) { | 541 | const discard_len = r.vtable.discard(r, .limited(remaining)) catch |err| switch (err) { |
| 542 | error.EndOfStream => return n - remaining, | 542 | error.EndOfStream => return n - remaining, |
| 543 | error.ReadFailed => return error.ReadFailed, | 543 | error.ReadFailed => return error.ReadFailed, |
| 544 | }; | 544 | }; |
| ... | @@ -586,7 +586,28 @@ pub fn readSliceShort(r: *Reader, buffer: []u8) ShortError!usize { | ... | @@ -586,7 +586,28 @@ pub fn readSliceShort(r: *Reader, buffer: []u8) ShortError!usize { |
| 586 | r.seek = 0; | 586 | r.seek = 0; |
| 587 | while (true) { | 587 | while (true) { |
| 588 | const remaining = buffer[i..]; | 588 | const remaining = buffer[i..]; |
| 589 | const n = r.unbuffered_reader.readVec(&.{ remaining, r.buffer }) catch |err| switch (err) { | 589 | var wrapper: Writer.VectorWrapper = .{ |
| | 590 | .it = .{ |
| | 591 | .first = remaining, |
| | 592 | .last = r.buffer, |
| | 593 | }, |
| | 594 | .writer = .{ |
| | 595 | .context = &Writer.VectorWrapper.unique_address, |
| | 596 | .buffer = if (remaining.len >= r.buffer.len) remaining else r.buffer, |
| | 597 | .vtable = &.{ .drain = Writer.fixedDrain }, |
| | 598 | }, |
| | 599 | }; |
| | 600 | const n = r.vtable.stream(r, &wrapper.writer, .unlimited) catch |err| switch (err) { |
| | 601 | error.WriteFailed => { |
| | 602 | if (wrapper.writer.buffer.ptr != remaining.ptr) { |
| | 603 | assert(r.seek == 0); |
| | 604 | r.seek = remaining.len; |
| | 605 | r.end = wrapper.writer.end; |
| | 606 | @memcpy(remaining, r.buffer[0..remaining.len]); |
| | 607 | return buffer.len; |
| | 608 | } |
| | 609 | return buffer.len; |
| | 610 | }, |
| 590 | error.EndOfStream => return i, | 611 | error.EndOfStream => return i, |
| 591 | error.ReadFailed => return error.ReadFailed, | 612 | error.ReadFailed => return error.ReadFailed, |
| 592 | }; | 613 | }; |