| ... | ... | @@ -79,23 +79,18 @@ pub const AccumBuffer = struct { |
| 79 | 79 | _ = writer; |
| 80 | 80 | |
| 81 | 81 | const buf_len = self.buf.items.len; |
| 82 | | if (dist > buf_len) { |
| 83 | | return error.CorruptInput; |
| 84 | | } |
| 82 | if (dist > buf_len) return error.CorruptInput; |
| 85 | 83 | |
| 86 | | // ensure we have enough capacity for all new bytes. |
| 87 | | // This prevents the buffer's memory from being reallocated (and freed) |
| 88 | | // while we are still reading from it. |
| 89 | | try self.buf.ensureTotalCapacity(allocator, buf_len + len); |
| 90 | | |
| 91 | | var offset = buf_len - dist; |
| 92 | | var i: usize = 0; |
| 93 | | while (i < len) : (i += 1) { |
| 94 | | const x = self.buf.items[offset]; |
| 95 | | // Since capacity is guaranteed, it's safe to use appendAssumeCapacity. |
| 96 | | self.buf.appendAssumeCapacity(x); |
| 97 | | offset += 1; |
| 98 | | } |
| 84 | try self.buf.ensureUnusedCapacity(allocator, len); |
| 85 | const buffer = self.buf.allocatedSlice(); |
| 86 | const src = buffer[buf_len - dist ..][0..len]; |
| 87 | const dst = buffer[buf_len..][0..len]; |
| 88 | |
| 89 | // This is not a @memmove; it intentionally repeats patterns caused by |
| 90 | // iterating one byte at a time. |
| 91 | for (dst, src) |*d, s| d.* = s; |
| 92 | |
| 93 | self.buf.items.len = buf_len + len; |
| 99 | 94 | self.len += len; |
| 100 | 95 | } |
| 101 | 96 | |