Files
encoder.src
CELL_LEN = 14
// UTILS ------------------------------------------------------
to_bin = function(n)
if n == 0 then return "0"
if n == 1 then return "1"
return to_bin(floor(n / 2)) + str(n % 2)
end function
to_int = function(n)
n = n.values
n.reverse
r = 0
for i in n.indexes
if n[i] == "1" then
r = r + 2 ^ i
end if
end for
return r
end function
divide = function(s, n)
if s.len == 0 then
return []
end if
if s.len < n then
//prepend = "0" * (n - s.len)
//s = prepend + s
return [s]
end if
return divide(s[n:], n).insert(0, s[0:n])
end function
char_set = []
char_set.push(range(48, 57)) // numbers
char_set.push(range(65, 90)) // capital_letters
char_set.push(range(97, 122)) // letters
char_set.push(range(128, 192)) // extended
char_set.push("@".code)
char_set = char_set.flat // <- 7 bit char set
// -------------------------------------------------------------
lzw_table_to_bin = function(table)
f = function(c)
r = c
if c isa string then r = c.code
return "0" * (CELL_LEN - to_bin(r).len) + to_bin(r)
end function
return table.map(@f).join("")
end function
bin_to_lzw_table = function(stream)
f = function(s)
n = to_int(s)
if n < 256 then return char(n)
return n
return
end function
return divide(stream, CELL_LEN).map(@f)
end function
stream_to_string = function(bits_stream)
f = function(s)
return char(char_set[to_int(s)])
end function
return divide(bits_stream, 7).map(@f).join("")
end function
string_to_stream = function(string)
f = function(s)
bin = to_bin(char_set.indexOf(s.code))
return "0" * (7 - bin.len) + bin
end function
return string.values.map(@f).join("")
end function
encode_lzw_table = function(table)
return stream_to_string(lzw_table_to_bin(table))
end function
decode_lzw_table = function(string)
end function