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 {
392392 }
393393}
394394
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
396396/// necessary.
397397///
398398/// Invalidates previously returned values from `peek`.
......@@ -411,8 +411,8 @@ pub fn peek(r: *Reader, n: usize) Error![]u8 {
411411 return r.buffer[r.seek..][0..n];
412412}
413413
414/// Returns all the next buffered bytes from `unbuffered_reader`, after filling
415/// the buffer to ensure it contains at least `n` bytes.
414/// Returns all the next buffered bytes, after filling the buffer to ensure it
415/// contains at least `n` bytes.
416416///
417417/// Invalidates previously returned values from `peek` and `peekGreedy`.
418418///
......@@ -461,8 +461,8 @@ pub fn take(r: *Reader, n: usize) Error![]u8 {
461461 return result;
462462}
463463
464/// Returns the next `n` bytes from `unbuffered_reader` as an array, filling
465/// the buffer as necessary and advancing the seek position `n` bytes.
464/// Returns the next `n` bytes from the stream as an array, filling the buffer
465/// as necessary and advancing the seek position `n` bytes.
466466///
467467/// Asserts that the `Reader` was initialized with a buffer capacity at
468468/// least as big as `n`.
......@@ -476,8 +476,8 @@ pub fn takeArray(r: *Reader, comptime n: usize) Error!*[n]u8 {
476476 return (try r.take(n))[0..n];
477477}
478478
479/// Returns the next `n` bytes from `unbuffered_reader` as an array, filling
480/// the buffer as necessary, without advancing the seek position.
479/// Returns the next `n` bytes from the stream as an array, filling the buffer
480/// as necessary, without advancing the seek position.
481481///
482482/// Asserts that the `Reader` was initialized with a buffer capacity at
483483/// least as big as `n`.
......@@ -538,7 +538,7 @@ pub fn discardShort(r: *Reader, n: usize) ShortError!usize {
538538 r.end = 0;
539539 r.seek = 0;
540540 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) {
542542 error.EndOfStream => return n - remaining,
543543 error.ReadFailed => return error.ReadFailed,
544544 };
......@@ -586,7 +586,28 @@ pub fn readSliceShort(r: *Reader, buffer: []u8) ShortError!usize {
586586 r.seek = 0;
587587 while (true) {
588588 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 },
590611 error.EndOfStream => return i,
591612 error.ReadFailed => return error.ReadFailed,
592613 };