Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function e_char(str1, str2)
- local l
- if #str1 < #str2 then
- l = #str1
- else
- l = #str2
- end
- local i = 1
- while i <= l and str1:sub(i, i) == str2:sub(i, i) do
- i = i + 1
- end
- if i <= l then
- return i - 1
- else
- return l
- end
- end
- local function lz77(text, sw_l)
- sw_l = sw_l or 6
- local o = 1
- local eof = false
- while not eof do
- local sw_o = o - sw_l
- if sw_o < 1 then sw_o = 1 end
- local sw = text:sub(sw_o, o - 1)
- local o_text = text:sub(o)
- local lg = 0
- local lg_pos = 0
- for i = 1, #sw do
- local n = e_char(sw:sub(i), o_text)
- if n > lg then
- lg = n
- lg_pos = i
- end
- end
- local f_pos = o + lg
- local f
- if f_pos <= #text then
- f = text:sub(f_pos, f_pos)
- else
- f = "]"
- eof = true
- end
- io.write(string.char(lg_pos)..string.char(lg)..f)
- --print(string.format("(%d, %d, %s)", lg_pos, lg, f))
- o = f_pos + 1
- end
- end
- lz77("blablablabla", 30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement