LZW compression: Difference between revisions

Content added Content deleted
m (→‎version 2: changed whitespace and comments, simplified some code.)
Line 4,898: Line 4,898:
This REXX version can execute on   '''ASCII'''   or   '''EBCDIC'''   systems.
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.*/
<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.*/
parse arg x; if x='' then x= , /*get an optional argument from the CL.*/
x= '"There is nothing permanent except change." ─── Heraclitus [540 ── 475 BCE]'
'"There is nothing permanent except change." ─── Heraclitus [540 ── 475 BCE]'
say 'original text=' x /* [↑] Not specified? Then use default*/
say 'original text=' x /* [↑] Not specified? Then use default*/
cypher= LZWc(x) /*compress text using the LZW algorithm*/
cypher= LZWc(x) /*compress text using the LZW algorithm*/
say 'reconstituted=' LZWd(cypher) /*display the reconstituted string. */
say 'reconstituted=' LZWd(cypher) /*display the reconstituted string. */
say ' LZW integers=' cypher /* " " LZW integers used. */
say ' LZW integers=' cypher /* " " LZW integers used. */
exit /*stick a fork in it, we're all done. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
LZWc: procedure; parse arg y,,w $ @.; #= 256 /*LZW compress algorithm.*/
LZWi: arg i,@.; #=256; do j=0 for #; _=d2c(j); if i then @.j=_; else @._=j; end; return
do j=0 for #; _= d2c(j); @._= j; end /*j*/
do k=1 for length(y)+1; z= w || substr(y, k, 1)
if @.z=='' then do; $= $ @.w; @.z= #; #= # + 1; w= substr(y, k, 1); end
else w= z /*#: the dictionary size.*/
end /*k*/; return substr($, 2) /*elide a leading blank. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
LZWd: procedure; parse arg x y,,@.; #= 256 /*LZW decompress algorithm.*/
LZWc: procedure; parse arg y,,$; call LZWi 0; w= /*LZW compress.*/
do j=0 for #; @.j= d2c(j); end /*j*/
do k=1 for length(y)+1; z= w || substr(y, k, 1)
$= @.x; w= $ /*#: the dictionary size.*/
if @.z=='' then do; $= $ @.w; @.z= #; #= # + 1; w= substr(y, k, 1); end
do k=1 for words(y); z= word(y, k)
else w= z /*#: the dictionary size.*/
if @.z\=='' | @.k==" " then ?= @.z
end /*k*/; return substr($, 2) /*elide a leading blank. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
else if z==# then ?= w || left(w, 1)
$= $ || ?
LZWd: procedure; parse arg x y; call LZWi 1; $= @.x; w= $ /*LZW decompress.*/
@.#= w || left(?, 1); w= ?; #= # + 1 /*bump dict. size.*/
do k=1 for words(y); z= word(y, k)
end /*k*/; return $</lang>
if @.z\=='' | @.k==" " then ?= @.z
else if z==# then ?= w || left(w, 1)
$= $ || ?
@.#= w || left(?, 1); w= ?; #= # + 1 /*bump dict. size*/
end /*k*/; return $</lang>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>