Function readSliceShort [src]
Fill buffer with the next buffer.len bytes from the stream, advancing
the seek position.
Invalidates previously returned values from peek.
Returns the number of bytes read, which is less than buffer.len if and
only if the stream reached the end.
See also:
readSliceAll
Prototype
pub fn readSliceShort(r: *Reader, buffer: []u8) ShortError!usize
Parameters
r: *Reader
buffer: []u8
Possible Errors
See the Reader
implementation for detailed diagnostics.
Example
test readSliceShort {
var r: Reader = .fixed("HelloFren");
var buf: [5]u8 = undefined;
try testing.expectEqual(5, try r.readSliceShort(&buf));
try testing.expectEqualStrings("Hello", buf[0..5]);
try testing.expectEqual(4, try r.readSliceShort(&buf));
try testing.expectEqualStrings("Fren", buf[0..4]);
try testing.expectEqual(0, try r.readSliceShort(&buf));
}
Source
pub fn readSliceShort(r: *Reader, buffer: []u8) ShortError!usize {
const contents = r.buffer[r.seek..r.end];
const copy_len = @min(buffer.len, contents.len);
@memcpy(buffer[0..copy_len], contents[0..copy_len]);
r.seek += copy_len;
if (buffer.len - copy_len == 0) {
@branchHint(.likely);
return buffer.len;
}
var i: usize = copy_len;
var data: [1][]u8 = undefined;
while (true) {
data[0] = buffer[i..];
i += readVec(r, &data) catch |err| switch (err) {
error.EndOfStream => return i,
error.ReadFailed => return error.ReadFailed,
};
if (buffer.len - i == 0) return buffer.len;
}
}