[comp.sys.handhelds] Oooh... Aaah... LCD string compression!

bgribble@jarthur.Claremont.EDU (Bill Gribble) (02/15/90)

Hi.  This is a pair of programs I wrote today while staving off sleep in 
a lecture class.   The idea is to compress LCD strings by tokenizing 
the omnipresent CHR(0) characters.  Useful?  Maybe.  My idea is to 
eventually write an integral-table displayer, with an interface 
something like UNITS and CATALOG, using full-screen graphics rather
than algebraic objects, which are pretty hard to see when they get 
complex.  Anyway, that will take a lot of memory, and, inspired by 
the handy ARC set of programs posted a while back, I thought of this.

CHR(128) is the tokenization symbol; any occurrence of CHR(0) will be 
replaced by one CHR(128) followed by a character whose NUM value 
indicates how many CHR(0) were replaced.  A natural occurrence of 
CHR(128) in the LCD string is replaced by TWO chr(128)'s, signalling 
UNCOMPRESS that there really was one there.  Don't ask me why I picked
128; it just occurred to me that 255 would have allowed for more compression.
Oh well.  Next version.

The big problem right now is that both programs are pig-slow, and ugly 
to boot.  I would appreciate any ideas for optimization, as I'm not really
friends with local variables at this point.  

To use COMPRESS and UNCOMPRESS, first run MCHRST to create a string of
CHR(0) to be used in reconstituting strings.  

COMPRESS [9307]  usage: 1: LCD string   returns: 1: compressed string

<< DUP SIZE -> str sz
   << "" 1 sz FOR i
       i i SUB -> ch
      << IF ch 0 CHR SAME
            THEN 1 -> ct
               << WHILE str i ct + DUP SUB 0 CHR SAME ct 127 < AND
                    REPEAT ct 1 + 'ct' STO
                  END
                  128 CHR ct CHR + + 
                  i ct 1 - + 'i' STO
               >>
            ELSE 
               IF ch NUM 128 SAME 
                  THEN 128 CHR + 128 CHR +
                  ELSE ch + 
               END
       >>
    NEXT
>>

UNCOMPRESS [A4B3]  usage: 1: compressed string   returns: 1: LCD string

<< DUP SIZE -> str sz 
   << "" 1 sz FOR i
      str i i SUB -> ch
      << IF ch 128 CHR <> 
            THEN ch +
         ELSE
            IF 128 CHR str i 1 + DUP SUB DUP 3 ROLL SAME
               THEN 128 CHR + i 1 + 'i' STO DROP
               ELSE CHRST SWAP NUM 1 SWAP SUB + i 1 + 'i' STO
            END
         END
       >>
       NEXT
   >>
>>

MCHRST  (makes a string of CHR(0) to uncompress faster)

<< "" 1 128 FOR X 0 CHR + NEXT 'CHRST' STO 'MCHRST' PURGE >>


Have fun!   

                           Bill Gribble