Let std/lzw mix automatic and manual flushes

This commit is contained in:
Nigel Tao committed 2018-12-29 14:23:27 +11:00
1 parent c4cc0f4177
commit 314b1209ed
2 files changed
+29 -23

No files matched your search

+12 -11
View File
@@ -33,6 +33,7 @@ pub struct decoder?(
width base.u32[..12],
bits base.u32,
n_bits base.u32[..31],
output_ri base.u32[..8191],
output_wi base.u32[..8191],
// read_from return value. The read_from method effectively returns a
@@ -48,8 +49,8 @@ pub struct decoder?(
// table in this decoder. See std/lzw/README.md for more detail.
lm1s array[4096] base.u16[..4095],
// output[:output_wi] is the buffered output, connecting read_from with
// write_to and flush.
// output[output_ri:output_wi] is the buffered output, connecting read_from
// with write_to and flush.
output array[8192 + 7] base.u8,
)
@@ -72,6 +73,7 @@ pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
this.width = this.literal_width + 1
this.bits = 0
this.n_bits = 0
this.output_ri = 0
this.output_wi = 0
i = 0
while i < this.clear_code {
@@ -310,33 +312,32 @@ pri func decoder.read_from!(src base.io_reader) {
}
pri func decoder.write_to?(dst base.io_writer) {
var i base.u32[..8191]
var s slice base.u8
var n base.u64
while this.output_wi > 0 {
if i > this.output_wi {
if this.output_ri > this.output_wi {
return "?internal error: inconsistent I/O"
}
s = this.output[i:this.output_wi]
s = this.output[this.output_ri:this.output_wi]
n = args.dst.copy_from_slice!(s:s)
if n == s.length() {
this.output_ri = 0
this.output_wi = 0
return ok
}
i = (i + ((n & 8191) as base.u32)) & 8191
this.output_ri = (this.output_ri ~mod+ ((n & 0xFFFFFFFF) as base.u32)) & 8191
yield base."$short write"
}
}
// TODO: enforce that flush is only valid to call if decode was always passed a
// 0-length dst base.io_writer? The (non-obvious) hard part is to enforce that
// without regressing benchmarks.
pub func decoder.flush!() slice base.u8 {
var s slice base.u8
s = this.output[:this.output_wi]
if this.output_ri <= this.output_wi {
s = this.output[this.output_ri:this.output_wi]
}
this.output_ri = 0
this.output_wi = 0
return s
}