Added in version 1.2 of package binaryio-lib.
This module provides encoding and decoding support using prefix codes (aka prefix-free codes), including Huffman codes. See binaryio/huffman for support for computing such codes.
procedure
input[ msf?#:padpad-sbv])input:sequence?
If msf? is #t (the default), then bits within each byte are added in most significant first order. If msf? is #f, then bits within each byte are added in least significant first order.
#"234ε264εPu<36ε312ε$376ε?"
74
procedure
encode-tableinputinput:sequence?
procedure
( prefixcode-build-decode-tree encode-table)→any/c
The representation is not specified, but if all values in the table are readable (or quotable), then the representation of the decoder tree is readable (or quotable).
procedure
bs[ start-bit-indexend-bit-indexmsf?#:endend-codedecode-tree:any/cbs:bytes?end-bit-index : exact-nonnegative-integer?handle-error :any )
Each value represented by decode-tree must be a byte, character, byte string, or character string. Each decoded value is accumulated into a byte string, which is the result of successful decoding.
(handle-error'badbad-start-indexbad-end-indexbad-code)
(handle-error'incompleteincomplete-start-indexend-bit-indexincomplete-code)
Note that if handle-error returns normally, its result is discarded, so it is recommended that handle-error escape (for example, by raising an exception).
#"hello world!"
#"hello world!"
#"hello world!"
procedure
decode-treebs[ start-bit-indexend-bit-indexmsf?#:endend-code#:handle-errorhandle-error])decode-tree:any/cbs:bytes?end-bit-index : exact-nonnegative-integer?handle-error :any )
If output is an output port, then a decoded value must be a byte, character, byte string, or character string, and the value is emitted by writing it to the port. If output is a procedure, then any value is allowed, and the value is emitted by calling output on it.
If decoding completes successfully, the result is (void ); otherwise, it is the result of the call to handle-error.
#"hello world!"
#"hello world!"
'(incomplete 74 80 4128774)
procedure
bs[ start-bit-indexend-bit-indexmsf?#:endend-codedecode-tree:any/cbs:bytes?end-bit-index : exact-nonnegative-integer?handle-error :any )
Note that if handle-error returns normally, its result is discarded, so it is recommended that handle-error escape (for example, by raising an exception).
procedure
bs[ start-bit-indexend-bit-indexmsf?#:endend-code])decode-tree:any/cbs:bytes?end-bit-index : exact-nonnegative-integer?
(values 'oknext-bit-indexvalue) — The bits from start-bit-index (inclusive) to next-bit-index (exclusive) represent the code for value.
(values 'badnext-bit-indexbad-code) — The bits from start-bit-index to next-bit-index do not represent a valid code or its prefix. The bad-code result contains those bits as a bitvector.
(values 'incompletenext-bit-indexincomplete-code) — The bits from start-bit-index to next-bit-index represent an incomplete code, but it is not a prefix of end-code. The incomplete-code result contains those bits as a bitvector.
(values 'endnext-bit-indexfinal-code) — The bits from start-bit-index to next-bit-index represent a prefix of end-code — possibly all of end-code, possibly empty (if start-bit-index equals end-bit-index). The final-code result contains those bits as a bitvector.
'ok
6
104
'ok
11
101
#"he"