|
Revision 292, 0.8 kB
(checked in by JarrettBillingsley, 7 months ago)
|
Closes #65. Also fixed a bug in the regexp.email predefined regexp, and a small bug in dumpVal.
|
| Line | |
|---|
| 1 |
module lzw |
|---|
| 2 |
|
|---|
| 3 |
function main(vararg) |
|---|
| 4 |
{ |
|---|
| 5 |
if(#vararg < 2) |
|---|
| 6 |
{ |
|---|
| 7 |
writefln("Usage: mdcl lzw.md inputFile outputFile") |
|---|
| 8 |
return |
|---|
| 9 |
} |
|---|
| 10 |
|
|---|
| 11 |
local input = io.File(vararg[0], io.FileMode.In) |
|---|
| 12 |
local output = io.File(vararg[1], io.FileMode.OutNew) |
|---|
| 13 |
local dict = {} |
|---|
| 14 |
local w = "" |
|---|
| 15 |
local code |
|---|
| 16 |
|
|---|
| 17 |
for(code = 0; code < 128; code++) |
|---|
| 18 |
dict[toString(toChar(code))] = code |
|---|
| 19 |
|
|---|
| 20 |
local shortCount = 0 |
|---|
| 21 |
output.writeInt(0) |
|---|
| 22 |
|
|---|
| 23 |
for(local i = 0, local size = input.size(); i < size; i++) |
|---|
| 24 |
{ |
|---|
| 25 |
local k = input.readChar() |
|---|
| 26 |
|
|---|
| 27 |
local wk = w ~ k |
|---|
| 28 |
|
|---|
| 29 |
if(wk in dict) |
|---|
| 30 |
w = wk |
|---|
| 31 |
else |
|---|
| 32 |
{ |
|---|
| 33 |
output.writeShort(dict[w]) |
|---|
| 34 |
shortCount++ |
|---|
| 35 |
dict[wk] = code |
|---|
| 36 |
code++ |
|---|
| 37 |
w = toString(k) |
|---|
| 38 |
} |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
output.flush() |
|---|
| 42 |
output.position(0) |
|---|
| 43 |
output.writeInt(shortCount) |
|---|
| 44 |
|
|---|
| 45 |
input.close() |
|---|
| 46 |
output.close() |
|---|
| 47 |
} |
|---|