1414from ..__common__ import *
1515
1616
17- __examples__ = {'enc-dec(lz78)' : ["test" , "This is a test" , "@random{1024}" ]}
17+ __examples__ = {'enc-dec(lz78)' : ["test" , "This is a test" , "@random{512, 1024,2048 }" ]}
1818
1919
2020def lz78_compress (input , errors = "strict" ):
2121 """ Compresses the given data by applying LZ78 compression algorithm. """
2222 data = tuple (c if isinstance (c , int ) else ord (c ) for c in input )
23+ if len (data ) == 0 :
24+ return "" , 0
2325 out = (data [0 ], )
2426 d = {tuple (): (0 , ), (data [0 ], ): (1 , )}
2527 a , b , ctr = 1 , 1 , [2 ]
@@ -41,14 +43,16 @@ def lz78_compress(input, errors="strict"):
4143 if data [a :b ] in d and a != b :
4244 w = tuple (d [data [a :b ]])
4345 out += w + tuple (0 for i in range (len (ctr ) - len (w )))
44- return bytes ( out ), len (out )
46+ return "" . join ( chr ( i ) for i in out ), len (out )
4547
4648
4749def lz78_decompress (input , errors = "strict" ):
4850 """ Decompresses the given data. """
4951 data = tuple (c if isinstance (c , int ) else ord (c ) for c in input )
52+ if len (data ) == 0 :
53+ return "" , 0
5054 out = (data [0 ], )
51- l = [tuple (), ( data [ 0 ], ) ]
55+ l = [tuple (), out ]
5256 a , b , c , i , char = 1 , 1 , 256 , 0 , False
5357 try :
5458 while a < len (data ):
@@ -69,8 +73,8 @@ def lz78_decompress(input, errors="strict"):
6973 a += b
7074 char = True
7175 except :
72- return handle_error ("lz78" , errors , decode = True )(data [a ], a ), len (input )
73- return bytes ( out ), len (out )
76+ return handle_error ("lz78" , errors , decode = True )(chr ( data [a ]) , a ), len (input )
77+ return "" . join ( chr ( i ) for i in out ), len (out )
7478
7579
7680add ("lz78" , lz78_compress , lz78_decompress )
0 commit comments