LZW compression: Difference between revisions
Content added Content deleted
m (→version 2: corrected a misspelling.) |
m (→version 2: change type of compare (for input), added/changed comments and whitespace.) |
||
Line 4,415: | Line 4,415: | ||
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 |
parse arg x; if x='' then /*get an optional argument from the CL.*/ |
||
x= '"There is nothing permanent except change." ─── Heraclitus [540-475 BC]' |
x= '"There is nothing permanent except change." ─── Heraclitus [540-475 BC]' |
||
say 'original text=' x /* [↑] Not specified? Then use default*/ |
say 'original text=' x /* [↑] Not specified? Then use default*/ |
||
Line 4,427: | Line 4,427: | ||
do k=1 for length(y)+1; z= w || substr(y, k, 1) |
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 |
if @.z=='' then do; $= $ @.w; @.z= #; #= # + 1; w= substr(y, k, 1); end |
||
else w= z |
else w= z /*#: the dictionary size.*/ |
||
end /*k*/; |
end /*k*/; return substr($, 2) /*elide a leading blank. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
LZWd: procedure; parse arg x y,,@.; #= 256 /*LZW decompress algorithm.*/ |
LZWd: procedure; parse arg x y,,@.; #= 256 /*LZW decompress algorithm.*/ |
||
do j=0 for #; @.j= d2c(j); end /*j*/ |
do j=0 for #; @.j= d2c(j); end /*j*/ |
||
$= @.x; w= $ |
$= @.x; w= $ /*#: the dictionary size.*/ |
||
do k=1 for words(y); z= word(y, k) |
do k=1 for words(y); z= word(y, k) |
||
if @.z\=='' | @.k==" " then ?= @.z |
if @.z\=='' | @.k==" " then ?= @.z |
||
else if z==# then ?= w || left(w, 1) |
else if z==# then ?= w || left(w, 1) |
||
$= $ || ? |
$= $ || ? |
||
@.#= w || left(?, 1); #= # + 1; w= ? |
@.#= w || left(?, 1); #= # + 1; w= ? |
||
end /*k*/; return $</lang> |
end /*k*/; return $</lang> |
||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |