fn writeMatch(w: *Writer, length: u16, distance: u16) !void w: *Writerlength: u16distance: u16523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544fn writeMatch(w: *Writer, length: u16, distance: u16) !void {
if (w.end < distance) return error.InvalidMatch;
assert(length > 0);
assert(length <= token.max_length);
assert(distance >= token.min_distance);
assert(distance <= token.max_distance);
// This is not a @memmove; it intentionally repeats patterns caused by
// iterating one byte at a time.
const dest = try w.writableSlicePreserve(flate.history_len, length);
const end = dest.ptr - w.buffer.ptr;
const src = w.buffer[end - distance ..][0..length];
if (distance >= length) {
@memcpy(dest, src);
} else if (distance == 1) {
// Repeating copy of single byte
@memset(dest, src[0]);
} else {
// Repeating copy of multiple bytes
for (dest, src) |*d, s| d.* = s;
}
}
Write match (back-reference to the same data slice) starting at
distanceback from current write position, andlengthof bytes.lengthmay be less than the minimum match length to allow for writing partial matches, but must be greater than zero.