pub const AccumBuffer = struct buf: ArrayList(u8)Buffer
memlimit: usizeBuffer memory limit
len: usizeTotal number of bytes sent through the buffer
pub fn init(memlimit: usize) AccumBufferpub fn appendByte(self: *AccumBuffer, allocator: Allocator, byte: u8) !voidpub fn reset(self: *AccumBuffer, writer: *Writer) !voidReset the internal dictionary
pub fn lastOr(self: AccumBuffer, lit: u8) u8Retrieve the last byte or return a default
pub fn lastN(self: AccumBuffer, dist: usize) !u8Retrieve the n-th last byte
pub fn appendLiteral(self: *AccumBuffer, allocator: Allocator, lit: u8, writer: *Writer) !voidAppend a literal
pub fn appendLz(self: *AccumBuffer, allocator: Allocator, len: usize, dist: usize, writer: *Writer) !voidFetch an LZ sequence (length, distance) from inside the buffer
pub fn finish(self: *AccumBuffer, writer: *Writer) !voidpub fn deinit(self: *AccumBuffer, allocator: Allocator) void9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106pub const AccumBuffer = struct {
/// Buffer
buf: ArrayList(u8),
/// Buffer memory limit
memlimit: usize,
/// Total number of bytes sent through the buffer
len: usize,
pub fn init(memlimit: usize) AccumBuffer {
return .{
.buf = .empty,
.memlimit = memlimit,
.len = 0,
};
}
pub fn appendByte(self: *AccumBuffer, allocator: Allocator, byte: u8) !void {
try self.buf.append(allocator, byte);
self.len += 1;
}
/// Reset the internal dictionary
pub fn reset(self: *AccumBuffer, writer: *Writer) !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: AccumBuffer, 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: AccumBuffer, 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: *AccumBuffer,
allocator: Allocator,
lit: u8,
writer: *Writer,
) !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: *AccumBuffer,
allocator: Allocator,
len: usize,
dist: usize,
writer: *Writer,
) !void {
_ = writer;
const buf_len = self.buf.items.len;
if (dist > buf_len) return error.CorruptInput;
try self.buf.ensureUnusedCapacity(allocator, len);
const buffer = self.buf.allocatedSlice();
const src = buffer[buf_len - dist ..][0..len];
const dst = buffer[buf_len..][0..len];
// This is not a @memmove; it intentionally repeats patterns caused by
// iterating one byte at a time.
for (dst, src) |*d, s| d.* = s;
self.buf.items.len = buf_len + len;
self.len += len;
}
pub fn finish(self: *AccumBuffer, writer: *Writer) !void {
try writer.writeAll(self.buf.items);
self.buf.clearRetainingCapacity();
}
pub fn deinit(self: *AccumBuffer, allocator: Allocator) void {
self.buf.deinit(allocator);
self.* = undefined;
}
}
An accumulating buffer for LZ sequences