Skip to content

Commit 0e3f00f

Browse files
committed
fix LZW decode error
1 parent fefc6bd commit 0e3f00f

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Lempel_Ziv_Welch/Lua/Yonaba/lzw.lua

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
local function lzw_encode(str)
55
local w = ''
66
local result = {}
7-
local dict_size = 255
7+
local dict_size = 256
88

99
-- Builds the dictionnary
1010
local dict = {}
11-
for i = 0, dict_size do
11+
for i = 0, dict_size-1 do
1212
dict[string.char(i)] = i
1313
end
1414

@@ -23,8 +23,8 @@ local function lzw_encode(str)
2323
else
2424
-- Add the match to the dictionary
2525
table.insert(result, dict[w])
26-
i = i + 1
2726
dict[wc] = i
27+
i = i + 1
2828
w = char
2929
end
3030
end
@@ -35,11 +35,11 @@ local function lzw_encode(str)
3535
end
3636

3737
local function lzw_decode(str)
38-
local dict_size = 255
38+
local dict_size = 256
3939

4040
-- Builds the dictionary
4141
local dict = {}
42-
for i = 0, dict_size do
42+
for i = 0, dict_size-1 do
4343
dict[i] = string.char(i)
4444
end
4545

@@ -56,8 +56,8 @@ local function lzw_decode(str)
5656
return nil -- No match found, decoding error
5757
end
5858
result = result .. entry
59-
dict_size = dict_size + 1
6059
dict[dict_size] = w .. entry:sub(1,1)
60+
dict_size = dict_size + 1
6161
w = entry
6262
end
6363
return result

0 commit comments

Comments
 (0)