Paste: ulz attempt

Author: razetime
Mode: factor
Date: Fri, 17 Nov 2023 01:41:27
Plain Text |
! Copyright (C) 2023 Raghu Ranganathan.                                         
! See https://factorcode.org/license.txt for BSD license.                       
USING: io byte-arrays math io.encodings.binary io.streams.duplex                
  kernel namespaces sbufs sequences sequences.repeating ;                       
IN: compression.ulz                                                             
                                                                                
: cpy ( sbuf len -- sbuf )                                                      
  dup 0x40 bitand 0 = [ 0x3f bitand ] dip                                       
  [ 8 shift read1 bitor ] unless                                                
  read1 swap [ dupd 1 + tail-slice* ] dip                                       
  4 + <cycles> append                                                           
;                                                                               
                                                                                
: ulz-decode ( istream -- sbuf )                                                
  256 <sbuf> swap [                                                             
    [ read1 ] [                                                                 
      dup 0x80 bitand 0 =                                                       
      [ 1 + read append ] [ cpy ] if                                            
    ] while*                                                                    
  ] with-input-stream ;   

New Annotation

Summary:
Author:
Mode:
Body: