LZW compression: Difference between revisions

m
→‎version 2: changed data era to be more PC, added/changed comments and whitespace, added continuation indicator.
(Added a Fortran implementation of lzw)
m (→‎version 2: changed data era to be more PC, added/changed comments and whitespace, added continuation indicator.)
Line 4,848:
This REXX version can execute on   '''ASCII'''   or   '''EBCDIC'''   systems.
<lang rexx>/*REXX program compresses text using the LZW (Lempel─Ziv─Welch), and reconstitutes it.*/
parse arg x; if x='' then , /*get an optional argument from the CL.*/
x= '"There is nothing permanent except change." ─── Heraclitus [540- ── 475 BCBCE]'
say 'original text=' x /* [↑] Not specified? Then use default*/
cypher= LZWc(x) /*compress text using the LZW algorithm*/
say 'reconstituted=' LZWd(cypher) /*display the reconstituted string. */
say ' LZW integers=' cypher /* " " LZW integers used. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 4,863:
end /*k*/; return substr($, 2) /*elide a leading blank. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
LZWd: procedure; parse arg x y,,@.; #= 256 #= 256 /*LZW decompress algorithm.*/
do j=0 for #; @.j= d2c(j); end /*j*/
$= @.x; w= $ /*#: the dictionary size.*/
do k=1 for words(y); z= word(y, k)
if @.z\=='' | @.k==" " then ?= @.z
else if z==# then ?= w || left(w, 1)
$= $ || ?
@.#= w || left(?, 1); #w= #?; + 1; #= # + w=1 ?/*bump dict. size.*/
end /*k*/; return $</lang>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
original text= "There is nothing permanent except change." ─── Heraclitus [540- ── 475 BCBCE]
reconstituted= "There is nothing permanent except change." ─── Heraclitus [540- ── 475 BCBCE]
LZW integers= 34 84 104 101 114 101 32 105 115 32 110 111 116 104 105 110 103 32 112 259 109 97 110 101 110 116 32 101 120 99 101 112 281 99 104 277 103 101 46 34 32 296 196 298 296 32 72 259 97 99 108 105 116 117 264 32 91 53 52 48 4532 299 52 55 53 32 66 67 69 93
</pre>