Type Function BufferedReader [src]

Alias for std.io.buffered_reader.BufferedReader

Prototype

pub fn BufferedReader(comptime buffer_size: usize, comptime ReaderType: type) type

Parameters

buffer_size: usizeReaderType: type

Source

pub fn BufferedReader(comptime buffer_size: usize, comptime ReaderType: type) type { return struct { unbuffered_reader: ReaderType, buf: [buffer_size]u8 = undefined, start: usize = 0, end: usize = 0, pub const Error = ReaderType.Error; pub const Reader = io.Reader(*Self, Error, read); const Self = @This(); pub fn read(self: *Self, dest: []u8) Error!usize { // First try reading from the already buffered data onto the destination. const current = self.buf[self.start..self.end]; if (current.len != 0) { const to_transfer = @min(current.len, dest.len); @memcpy(dest[0..to_transfer], current[0..to_transfer]); self.start += to_transfer; return to_transfer; } // If dest is large, read from the unbuffered reader directly into the destination. if (dest.len >= buffer_size) { return self.unbuffered_reader.read(dest); } // If dest is small, read from the unbuffered reader into our own internal buffer, // and then transfer to destination. self.end = try self.unbuffered_reader.read(&self.buf); const to_transfer = @min(self.end, dest.len); @memcpy(dest[0..to_transfer], self.buf[0..to_transfer]); self.start = to_transfer; return to_transfer; } pub fn reader(self: *Self) Reader { return .{ .context = self }; } }; }