diff --git a/lib/std/Io/Reader.zig b/lib/std/Io/Reader.zig index 5059b7cff8f612a4a05825c6ffe6905e42072301..7e5a70518da53bb20d8723d401b98c82eec7dd20 100644 --- a/lib/std/Io/Reader.zig +++ b/lib/std/Io/Reader.zig @@ -226,10 +226,16 @@ pub fn streamExact64(r: *Reader, w: *Writer, n: u64) StreamError!void { /// "Pump" exactly `n` bytes from the reader to the writer. /// -/// When draining `w`, ensures that at least `preserve_len` bytes remain -/// buffered. +/// On success, at least `preserve_len` bytes will remain buffered if there are +/// enough buffered bytes to do so. +/// The amount buffered by the writer after the call will only be less than +/// `preserve_len` if `w.end + n` is less than `preserve_len` before the call. +/// The intentionally preserved bytes will include up to `preserve_len -| n` bytes from +/// the previously buffered bytes, plus `@min(n, preserve_len)` of the newly +/// "pumped" bytes. /// -/// Asserts `Writer.buffer` capacity exceeds `preserve_len`. +/// Asserts `Writer.buffer` capacity is at least `preserve_len`. +/// `n` can be greater than the `Writer.buffer` capacity. pub fn streamExactPreserve(r: *Reader, w: *Writer, preserve_len: usize, n: usize) StreamError!void { if (w.end + n <= w.buffer.len) { @branchHint(.likely); diff --git a/lib/std/Io/Writer.zig b/lib/std/Io/Writer.zig index 1c2f5f46e635ce64053c0406fa987fc91dd094a5..0da4d552d58d3939e27387313d0f12d76dc3d7b2 100644 --- a/lib/std/Io/Writer.zig +++ b/lib/std/Io/Writer.zig @@ -417,7 +417,8 @@ pub fn writableSliceGreedyPreserve(w: *Writer, preserve: usize, minimum_len: usi return w.buffer[w.end..]; } -/// Asserts the provided buffer has total capacity enough for `len`. +/// Asserts the provided buffer has total capacity enough for `len` +/// and `preserve` combined. /// /// Advances the buffer end position by `len`. /// @@ -755,8 +756,14 @@ pub fn writeByte(w: *Writer, byte: u8) Error!void { } } -/// When draining the buffer, ensures that at least `preserve` bytes -/// remain buffered. +/// On success, at least `preserve` bytes will remain buffered if there are +/// enough buffered bytes to do so. +/// The amount buffered by the writer after the call will only be less than +/// `preserve` if `w.end + 1` is less than `preserve` before the call. +/// The intentionally preserved bytes will include up to `preserve -| 1` bytes from +/// the previously buffered bytes, plus the newly written byte. +/// +/// Asserts buffer capacity is at least `preserve`. pub fn writeBytePreserve(w: *Writer, preserve: usize, byte: u8) Error!void { if (w.buffer.len - w.end != 0) { @branchHint(.likely); @@ -784,6 +791,19 @@ test splatByteAll { try testing.expectEqualStrings(&@as([45]u8, @splat('7')), aw.writer.buffered()); } +/// Writes the same byte many times, performing the underlying write call as +/// many times as necessary. +/// +/// On success, at least `preserve` bytes will remain buffered if there are +/// enough buffered bytes to do so. +/// The amount buffered by the writer after the call will only be less than +/// `preserve` if `w.end + n` is less than `preserve` before the call. +/// The intentionally preserved bytes will include up to `preserve -| n` bytes from +/// the previously buffered bytes, plus `@min(n, preserve_len)` of the newly +/// written bytes. +/// +/// Asserts buffer capacity is at least `preserve`. +/// `n` can be greater than the buffer capacity. pub fn splatBytePreserve(w: *Writer, preserve: usize, byte: u8, n: usize) Error!void { const new_end = w.end + n; if (new_end <= w.buffer.len) {