struct LzAccumBuffer [src]

An accumulating buffer for LZ sequences

Fields

buf: ArrayListUnmanaged(u8)Buffer
memlimit: usizeBuffer memory limit
len: usizeTotal number of bytes sent through the buffer

Members

Source

pub const LzAccumBuffer = struct { /// Buffer buf: ArrayListUnmanaged(u8), /// Buffer memory limit memlimit: usize, /// Total number of bytes sent through the buffer len: usize, const Self = @This(); pub fn init(memlimit: usize) Self { return Self{ .buf = .{}, .memlimit = memlimit, .len = 0, }; } pub fn appendByte(self: *Self, allocator: Allocator, byte: u8) !void { try self.buf.append(allocator, byte); self.len += 1; } /// Reset the internal dictionary pub fn reset(self: *Self, writer: anytype) !void { try writer.writeAll(self.buf.items); self.buf.clearRetainingCapacity(); self.len = 0; } /// Retrieve the last byte or return a default pub fn lastOr(self: Self, lit: u8) u8 { const buf_len = self.buf.items.len; return if (buf_len == 0) lit else self.buf.items[buf_len - 1]; } /// Retrieve the n-th last byte pub fn lastN(self: Self, dist: usize) !u8 { const buf_len = self.buf.items.len; if (dist > buf_len) { return error.CorruptInput; } return self.buf.items[buf_len - dist]; } /// Append a literal pub fn appendLiteral( self: *Self, allocator: Allocator, lit: u8, writer: anytype, ) !void { _ = writer; if (self.len >= self.memlimit) { return error.CorruptInput; } try self.buf.append(allocator, lit); self.len += 1; } /// Fetch an LZ sequence (length, distance) from inside the buffer pub fn appendLz( self: *Self, allocator: Allocator, len: usize, dist: usize, writer: anytype, ) !void { _ = writer; const buf_len = self.buf.items.len; if (dist > buf_len) { return error.CorruptInput; } var offset = buf_len - dist; var i: usize = 0; while (i < len) : (i += 1) { const x = self.buf.items[offset]; try self.buf.append(allocator, x); offset += 1; } self.len += len; } pub fn finish(self: *Self, writer: anytype) !void { try writer.writeAll(self.buf.items); self.buf.clearRetainingCapacity(); } pub fn deinit(self: *Self, allocator: Allocator) void { self.buf.deinit(allocator); self.* = undefined; } }