def compress(self, data, windowLength=None):
if windowLength == None:<EOL><INDENT>windowLength = self.defaultWindowLength<EOL><DEDENT>compressed = "<STR_LIT>"<EOL>pos = <NUM_LIT:0><EOL>lastPos = len(data) - self.minStringLength<EOL>while pos < lastPos:<EOL><INDENT>searchStart = max(pos - windowLength, <NUM_LIT:0>)<EOL>matchLength = self.minStringLength<EOL>foundMatch = False<EOL>bestMatchDistance = self.maxStringDistance<EOL>bestMatchLength = <NUM_LIT:0><EOL>newCompressed = None<EOL>while (searchStart + matchLength) < pos:<EOL><INDENT>m1 = data[searchStart: searchStart + matchLength]<EOL>m2 = data[pos: pos + matchLength]<EOL>isValidMatch = (m1 == m2 and matchLength <<EOL>self.maxStringLength)<EOL>if isValidMatch:<EOL><INDENT>matchLength += <NUM_LIT:1><EOL>foundMatch = True<EOL><DEDENT>else:<EOL><INDENT>realMatchLength = matchLength - <NUM_LIT:1><EOL>if foundMatch and realMatchLength > bestMatchLength:<EOL><INDENT>bestMatchDistance = pos - searchStart - realMatchLength<EOL>bestMatchLength = realMatchLength<EOL><DEDENT>matchLength = self.minStringLength<EOL>searchStart += <NUM_LIT:1><EOL>foundMatch = False<EOL><DEDENT><DEDENT>if bestMatchLength:<EOL><INDENT>newCompressed = (self.referencePrefix + self.__encodeReferenceInt(<EOL>bestMatchDistance, <NUM_LIT:2>) + self.__encodeReferenceLength(bestMatchLength))<EOL>pos += bestMatchLength<EOL><DEDENT>else:<EOL><INDENT>if data[pos] != self.referencePrefix:<EOL><INDENT>newCompressed = data[pos]<EOL><DEDENT>else:<EOL><INDENT>newCompressed = self.referencePrefix + self.referencePrefix<EOL><DEDENT>pos += <NUM_LIT:1><EOL><DEDENT>compressed += newCompressed<EOL><DEDENT>return compressed + data[pos:].replace("<STR_LIT>", "<STR_LIT>")<EOL>
Compresses text data using the LZ77 algorithm.