authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-06-05 20:08:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-01 16:35:29-07:00
logf04bc71ed7b7e8ced8c3b6a8b130b16c8978de78
tree5befce768fccabd812f749d277e75f4fac39fd7c
parent0027cc1b1a2fb7233ccb2f820b58365d9ebf1ea4

std.io.Reader: update readSliceShort to new API

no more readVec

1 files changed, 30 insertions(+), 9 deletions(-)

lib/std/io/Reader.zig+30-9
...@@ -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}
394394
395/// Returns the next `len` bytes from `unbuffered_reader`, filling the buffer as395/// 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}
413413
414/// Returns all the next buffered bytes from `unbuffered_reader`, after filling414/// 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}
463463
464/// Returns the next `n` bytes from `unbuffered_reader` as an array, filling464/// 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 at467/// 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}
478478
479/// Returns the next `n` bytes from `unbuffered_reader` as an array, filling479/// 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 at482/// 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 };