def do(self): self.write_byte(self.__in[self.__offset]) self.__offset += 1 while self.__offset < self.__length: offset, length = find_longest_match(self.__in[:self.__offset], self.__in[self.__offset:]) if offset >= 1 and length >= 4: self.__dictcopy(offset, length) else: self.__literal() return self.getdata()
def compress(data_to_compress): offset = 0 compressed_data = [] length = len(data_to_compress) while offset < length: #find the longest match for the next word to encode match_offset, match_length = find_longest_match(data_to_compress[:offset], data_to_compress[offset:]) #get the next symbol, if there is something else to compress symbol = data_to_compress[offset + match_length] if offset + match_length < length else '' compressed_data.append({"offset": match_offset, "length":match_length, "symbol":symbol}) offset += match_length + 1 # +1 since the next char is included in symbol return compressed_data
def compress(data_to_compress): offset = 0 compressed_data = [] length = len(data_to_compress) while offset < length: match_offset, match_length = find_longest_match(data_to_compress[:offset], data_to_compress[offset:]) if match_length == 0: symbol = data_to_compress[offset] compressed_data.append({"literal":True, "symbol":symbol}) offset += 1 else: compressed_data.append({"literal":False, "offset":match_offset, "length":match_length}) offset += match_length return compressed_data
def do(self): self.__literal(False) while self.__offset < self.__length: offset, length = find_longest_match(self.__in[: self.__offset], self.__in[self.__offset :]) if length == 0: c = self.__in[self.__offset] if c == "\x00": self.__singlebyte(0) else: self.__literal() elif length == 1 and 0 <= offset < 16: self.__singlebyte(offset) elif 2 <= length <= 3 and 0 < offset <= 127: self.__shortblock(offset, length) elif 3 <= length and 2 <= offset: self.__block(offset, length) else: self.__literal() # raise ValueError("no parsing found", offset, length) self.__end() return self.getdata()
def do(self): self.__literal(False) while self.__offset < self.__length: offset, length = find_longest_match(self.__in[:self.__offset], self.__in[self.__offset:]) if length == 0: c = self.__in[self.__offset] if c == "\x00": self.__singlebyte(0) else: self.__literal() elif length == 1 and 0 <= offset < 16: self.__singlebyte(offset) elif 2 <= length <= 3 and 0 < offset <= 127: self.__shortblock(offset, length) elif 3 <= length and 2 <= offset: self.__block(offset, length) else: self.__literal() #raise ValueError("no parsing found", offset, length) self.__end() return self.getdata()
def compress(data_to_compress): offset = 0 compressed_data = [] length = len(data_to_compress) while offset < length: match_offset, match_length = find_longest_match( data_to_compress[:offset], data_to_compress[offset:]) if match_length == 0: symbol = data_to_compress[offset] compressed_data.append({"literal": True, "symbol": symbol}) offset += 1 else: compressed_data.append({ "literal": False, "offset": match_offset, "length": match_length }) offset += match_length return compressed_data
# #Kabopan - Readable Algorithms. Public Domain, 2007-2009 from kbp.comp._lz77 import find_longest_match, back_copy assert find_longest_match("abc","a") == (3, 1) assert find_longest_match("abc","d") == (0, 0) assert find_longest_match("abcab","ab") == (2, 2) assert back_copy("a", 1, 1) == "aa" assert back_copy("ab", 2, 2) == "abab" assert back_copy("duplicate me please", 9, 19) == "duplicate me pleaseduplicate"