USING: accessors combinators io io.encodings.binary io.files kernel math ranges sbufs sequences strings ; IN: ulz : copy-forward ( buffer to from -- ) [a..b) dupd [ swap nth swap push ] 2with each ; : cpy-start ( decoder offset -- i ) neg swap length + ; : cpy ( buffer length offset -- ) swap [ dup ] [ cpy-start ] [ over + ] tri* copy-forward ; : lit ( buffer op -- buffer ) dupd 1 + [ read1 suffix! ] times drop ; : cpy1 ( buffer op -- buffer ) dupd 0x3F bitand 4 + read1 1 + cpy ; : cpy2 ( buffer op -- buffer ) dupd 8 shift 0x3FFF bitand 4 + read1 1 + cpy ; : interpret ( buffer -- buffer ) read1 [ dup 0xc0 bitand { { 0x00 [ lit ] } { 0x80 [ cpy1 ] } { 0xc0 [ cpy2 ] } } case interpret ] when* ; recursive : decode ( file -- string ) binary [ 0 interpret ] with-input-stream >string ;