Open main menu
Posts
Gists
Guilds
Users
Decipher
Docs
Open user menu
Log in
Sign up
Create a new gist
Posts
Gists
Guilds
Users
Decipher
Docs
Files
encoder.src
test.src
encoder.src
// require lst
Encoder = {}
Encoder.CELL_LEN = 14
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, 193)) // extended
char_set = char_set.flat // <- 7 bit char set
Encoder.CHAR_SET = char_set
// UTILS ------------------------------------------------------
Encoder.Utils = {}
Encoder.Utils.to_bin = function(n)
if n == 0 then return "0"
if n == 1 then return "1"
return self.to_bin(floor(n / 2)) + str(n % 2)
end function
Encoder.Utils.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
Encoder.Utils.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 self.divide(s[n:], n).insert(0, s[0:n])
end function
// -------------------------------------------------------------
Encoder.encode = function(lzw)
l = lzw[0:]
CELL_SIZE = 0
for i in l.indexes
if l[i] isa string then l[i] = l[i].code
l[i] = self.Utils.to_bin(l[i])
if l[i].len > CELL_SIZE then CELL_SIZE = l[i].len
end for
for i in l.indexes
l[i] = "0" * (CELL_SIZE - l[i].len) + l[i]
end for
l = l.join("")
FAT_ADDED = l.len % 7
if FAT_ADDED > 0 then FAT_ADDED = 7 - FAT_ADDED
l = l + "0" * FAT_ADDED
l = self.Utils.divide(l, 7)
fat_bin = self.Utils.to_bin(FAT_ADDED)
l.insert(0, "0" * (
6
- fat_bin.len) + fat_bin)
l.insert(0, "0" * (
7
- fat_bin.len) + fat_bin)
cell_bin = self.Utils.to_bin(CELL_SIZE)
l.insert(0, "0" * (
6
- cell_bin.len) + cell_bin)
l.insert(0, "0" * (
7
- cell_bin.len) + cell_bin)
l = self.Utils.divide(l.join(""), 7)
//print l
for i in l.indexes
l[i] = char(self.CHAR_SET[self.Utils.to_int(l[i])])
end for
return l.join("")
end function
Encoder.decode = function(string)
l = string.values
for i in l.indexes
l[i] = self.Utils.to_bin(self.CHAR_SET.indexOf(l[i].code))
l[i] = "0" * (7 - l[i].len) + l[i]
end for
l[-1] = l[-1][2:]
//
l[-1] = l[-1][2:]
l = l.join("")
CELL_SIZE = self.Utils.to_int(l[0:
6
])
FAT_ADDED = self.Utils.to_int(l[
6: 12
])
CELL_SIZE = self.Utils.to_int(l[0:
7
])
FAT_ADDED = self.Utils.to_int(l[
7: 14
])
l = self.Utils.divide(l[1
2
:], 7)
l[-1] = l[-1][0:FAT_ADDED * -1]
l = self.Utils.divide(l[1
4
:], 7)
//print l
if FAT_ADDED > 0 then
l[-1] = l[-1][0:FAT_ADDED * -1]
//print l
l = self.Utils.divide(l.join(""), CELL_SIZE)
for i in l.indexes
l[i] = self.Utils.to_int(l[i])
if l[i] < 256 then l[i] = char(l[i])
end for
return l
end function
test.src
import_code("/home/me/projects/lzw/libs/lst.src")
import_code("/home/me/projects/lzw/lzw.src")
import_code("/home/me/projects/lzw/encoder.src")
input = user_input("in: ")
compressed = Lzw.compress(input)
//compressed = Lzw.compress("TOBEORNOTTOBEORTOBEORNOT")
print "compressed: " + compressed
//compressed = Lzw.compress("ffffffffffffffffffffffff")
//
print "compressed: " + compressed
e = Encoder.encode(compressed)
print "encoded: " + e
dd = Lzw.decompress(Encoder.decode(e))
print "decoded and decompressed: " + dd
//
print "decoded and decompressed: " + dd
if input != dd then exit("<color=red>ERROR</color>")
print "input len: " + input.len
print "encoded len: " + e.len
print input
print dd
print input == dd
print
"text reduced by: " + round(100 - (e.len / input.len * 100), 2) + "%"
//print
input
//
print dd
//
print input == dd