Skip to content

Commit

Permalink
Updated comments and added some missing credits.
Browse files Browse the repository at this point in the history
  • Loading branch information
olle committed Feb 25, 2009
1 parent 115140c commit b237aea
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/main/java/LZ77.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*
* Copyright (c) 2008-2009 Olle Törnström studiomediatech.com
* The MIT License
*
* Copyright (c) 2009 Olle Törnström studiomediatech.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 4 additions & 7 deletions src/main/js/lz77.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

/*
* Copyright (c) 2008-2009 Olle Törnström studiomediatech.com
* The MIT License
*
* Copyright (c) 2009 Olle Törnström studiomediatech.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,12 +23,9 @@
* THE SOFTWARE.
*
* CREDIT: Initially implemented by Diogo Kollross and made publicly available
* on the website http://www.geocities.com/diogok_br/lz77. Edited here
* to provide two flavours for JavaScript usage, either as standalone
* compressor/decompressor or as class for copy/paste use.
* on the website http://www.geocities.com/diogok_br/lz77.
*/

// ------[class copy start]------
/**
* This class provides simple LZ77 compression and decompression.
*
Expand Down Expand Up @@ -171,5 +170,3 @@ var LZ77 = function (settings) {
return decompressed;
};
};
// ------[end of class copy]-----

4 changes: 3 additions & 1 deletion src/main/php/lz77.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


/*
* Copyright (c) 2008-2009 Olle Törnström studiomediatech.com
* The MIT License
*
* Copyright (c) 2009 Olle Törnström studiomediatech.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
38 changes: 34 additions & 4 deletions src/main/python/lz77.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
#
# The MIT License
#
# Copyright (c) 2009 Olle Törnström studiomediatech.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# CREDIT: From an initial implementation in Python by Diogo Kollross, made
# publicly available on http://www.geocities.com/diogok_br/lz77.
#
class Compressor:

def __init__(self):
Expand All @@ -13,6 +39,8 @@ def __init__(self):
self.defaultWindowLength = 144

def compress(self, data, windowLength = None):
"""Compresses text data using the LZ77 algorithm."""

if windowLength == None:
windowLength = self.defaultWindowLength

Expand All @@ -31,8 +59,8 @@ def compress(self, data, windowLength = None):

while (searchStart + matchLength) < pos:

m1 = data[searchStart:searchStart + matchLength]
m2 = data[pos:pos + matchLength]
m1 = data[searchStart : searchStart + matchLength]
m2 = data[pos : pos + matchLength]
isValidMatch = (m1 == m2 and matchLength < self.maxStringLength)

if isValidMatch:
Expand Down Expand Up @@ -64,6 +92,8 @@ def compress(self, data, windowLength = None):
return compressed + data[pos:].replace("`", "``")

def decompress(self, data):
"""Decompresses LZ77 compressed text data"""

decompressed = ""
pos = 0
while pos < len(data):
Expand All @@ -74,11 +104,11 @@ def decompress(self, data):
else:
nextChar = data[pos + 1]
if nextChar != self.referencePrefix:
distance = self.__decodeReferenceInt(data[pos + 1:pos + 3], 2)
distance = self.__decodeReferenceInt(data[pos + 1 : pos + 3], 2)
length = self.__decodeReferenceLength(data[pos + 3])
start = len(decompressed) - distance - length
end = start + length
decompressed += decompressed[start:end]
decompressed += decompressed[start : end]
pos += self.minStringLength - 1
else:
decompressed += self.referencePrefix
Expand Down

0 comments on commit b237aea

Please sign in to comment.