From eb81146e2acdeda2769833beafe6aa1d928cfd0b Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Sun, 5 Feb 2017 20:47:42 +0300 Subject: [PATCH] =?UTF-8?q?lengthEncode=20replaced=20with=20version=20whic?= =?UTF-8?q?h=20doesn=E2=80=99t=20perform=20any=20compression,=20test=20now?= =?UTF-8?q?=20compares=20before=20and=20after=20comp/decomp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Deflate.swift | 65 ++++------------------------------------ Tests/DeflateTests.swift | 4 ++- 2 files changed, 9 insertions(+), 60 deletions(-) diff --git a/Sources/Deflate.swift b/Sources/Deflate.swift index 4a988bb4..c70aa573 100644 --- a/Sources/Deflate.swift +++ b/Sources/Deflate.swift @@ -310,8 +310,7 @@ public final class Deflate: DecompressionAlgorithm { return bitWriter.buffer } - // TODO: Remove public when release. - public enum BLDCode: CustomStringConvertible { + private enum BLDCode: CustomStringConvertible { case byte(UInt8) case lengthDistance(Int, Int) @@ -326,67 +325,15 @@ public final class Deflate: DecompressionAlgorithm { } // TODO: Expand dictionary size. - private static func lengthEncode(_ rawBytes: [UInt8], _ dictSize: Int = 1944) -> [BLDCode] { + private static func lengthEncode(_ rawBytes: [UInt8]) -> [BLDCode] { // TODO: We shouldn't discard arrays shorter than 3 elements. Or maybe we should? precondition(rawBytes.count >= 3, "Too small array!") - var dictionary: [UInt8] = Array(repeating: 0, count: dictSize) - var buffer: [BLDCode] = [] - var dictPos = 0 - - var inputIndex = 0 - - while inputIndex < rawBytes.count { - let byte = rawBytes[inputIndex] - - if let matchStartIndex = dictionary.index(of: byte) { - if dictPos != 0 { - var matchEndIndex = matchStartIndex + 1 - - let distance = dictPos - matchStartIndex // FIXME: This is the problem. - if distance <= 32768 { - while inputIndex + matchEndIndex - matchStartIndex < rawBytes.count { - if rawBytes[inputIndex + matchEndIndex - matchStartIndex] != dictionary[matchEndIndex % dictPos] || - matchEndIndex - matchStartIndex >= 258 { - break - } - matchEndIndex += 1 - } - } - - let matchLength = matchEndIndex - matchStartIndex - if matchLength >= 3 { - buffer.append(BLDCode.lengthDistance(matchLength, distance)) - inputIndex += matchLength - } else { - dictionary[dictPos] = byte - dictPos += 1 - // TODO: This is probably is not efficient. - if dictPos >= dictSize { - dictionary.removeFirst() - dictionary.append(0) - dictPos -= 1 - } - - buffer.append(BLDCode.byte(byte)) - inputIndex += 1 - } - } - } else { - dictionary[dictPos] = byte - dictPos += 1 - // TODO: This is probably is not efficient. - if dictPos >= dictSize { - dictionary.removeFirst() - dictionary.append(0) - dictPos -= 1 - } - - buffer.append(BLDCode.byte(byte)) - inputIndex += 1 - } + var buffer = [BLDCode]() + for byte in rawBytes { + buffer.append(.byte(byte)) } - + return buffer } diff --git a/Tests/DeflateTests.swift b/Tests/DeflateTests.swift index cffed929..73732ae2 100644 --- a/Tests/DeflateTests.swift +++ b/Tests/DeflateTests.swift @@ -80,8 +80,10 @@ class DeflateTests: XCTestCase { XCTFail("Unable to deflate data") return } + let reUncompData = try! Deflate.decompress(compressedData: deflatedData) - print(String(data: reUncompData, encoding: .utf8)!) + XCTAssertEqual(answerData, reUncompData, + "Data before compression and after decompression of compressed data aren't equal") } }