Removed append(contentsOf:) where possible and performed small cleanup.

This commit is contained in:
Timofey Solomko
2016-12-24 22:26:55 +03:00
parent 56a3ae9f02
commit c8a5813730
6 changed files with 30 additions and 19 deletions
+7 -2
View File
@@ -80,6 +80,7 @@ public class BZip2: DecompressionAlgorithm {
while true {
let blockType: Int64 = Int64(pointerData.intFromBits(count: 48))
// TODO: Add CRC check.
// Next 32 bits are crc (which currently is not checked).
let _ = pointerData.intFromBits(count: 32)
@@ -114,7 +115,9 @@ public class BZip2: DecompressionAlgorithm {
bitMask >>= 1
}
} else {
used.append(contentsOf: Array(repeating: false, count: 16))
for _ in 0..<16 {
used.append(false)
}
}
mapMask >>= 1
}
@@ -209,7 +212,9 @@ public class BZip2: DecompressionAlgorithm {
repeatPower <<= 1
continue
} else if repeat_ > 0 {
buffer.append(contentsOf: Array(repeating: favourites[0], count: repeat_))
for _ in 0..<repeat_ {
buffer.append(favourites[0])
}
repeat_ = 0
}
if symbol == symbolsInUse - 1 {
+10 -8
View File
@@ -72,11 +72,8 @@ public class Deflate: DecompressionAlgorithm {
// Process uncompressed data into the output
// TODO: Replace precondition with guard and error throwing.
precondition(pointerData.bitMask == 1, "Misaligned byte.")
var ind = out.count
out.append(contentsOf: Array(repeating: 0, count: length))
for _ in 0..<length {
out[ind] = pointerData.alignedByte()
ind += 1
out.append(pointerData.alignedByte())
}
} else if blockType == [1, 0] || blockType == [0, 1] {
// Block with Huffman coding (either static or dynamic)
@@ -149,7 +146,9 @@ public class Deflate: DecompressionAlgorithm {
} else {
throw DeflateError.HuffmanTableError
}
codeLengths.append(contentsOf: Array(repeating: what, count: count))
for _ in 0..<count {
codeLengths.append(what)
}
n += count
}
// We have read codeLengths for both tables at once.
@@ -197,9 +196,12 @@ public class Deflate: DecompressionAlgorithm {
// The amount of times we do this is round(length / distance).
// length actually indicates the amount of data we get from this nextSymbol.
let repeatCount: Int = length / distance
let arrayToRepeat = Array(repeating: out[out.count - distance..<out.count],
count: repeatCount).flatMap { $0 }
out.append(contentsOf: arrayToRepeat)
let count = out.count
for _ in 0..<repeatCount {
for i in count - distance..<count {
out.append(out[i])
}
}
// Now we deal with the remainings.
if length - distance * repeatCount == distance {
for i in out.count - distance..<out.count {
+3 -2
View File
@@ -45,8 +45,9 @@ class HuffmanTree: CustomStringConvertible {
let finish = pair[0]
let endbits = pair[1]
if bits > 0 {
lengths.append(contentsOf:
(start..<finish).map { [$0, bits] })
for i in start..<finish{
lengths.append([i, bits])
}
}
start = finish
bits = endbits
+2 -3
View File
@@ -97,7 +97,6 @@ public class ZlibArchive: Archive {
if fdict == 1 {
pointerData.index += 4
}
// TODO: Add parsing of preset dictionary
return info
}
@@ -108,8 +107,8 @@ public class ZlibArchive: Archive {
If data passed is not actually a zlib archive, `ZlibError` will be thrown.
If data inside the archive is not actually compressed with DEFLATE algorithm, `DeflateError` will be thrown.
- Note: This function is NOT specification compliant because it does not checks ADLER-32 checksum and preset dicitionaries.
- Note: This function is specification compliant.
- Parameter archiveData: Data compressed with zlib.
+4 -2
View File
@@ -14,7 +14,8 @@ class GzipTests: XCTestCase {
static let testType: String = "gz"
func perform(test testName: String, answer answerServiceInfo: GzipArchive.ServiceInfo) {
guard let testData = try? Data(contentsOf: Constants.url(forTest: testName, withType: GzipTests.testType)) else {
guard let testData = try? Data(contentsOf: Constants.url(forTest: testName,
withType: GzipTests.testType)) else {
XCTFail("Failed to load test archive")
return
}
@@ -112,7 +113,8 @@ class GzipTests: XCTestCase {
}
func testGzipFull() {
guard let testData = try? Data(contentsOf: Constants.url(forTest: "random_file", withType: GzipTests.testType)) else {
guard let testData = try? Data(contentsOf: Constants.url(forTest: "random_file",
withType: GzipTests.testType)) else {
XCTFail("Failed to load test archive")
return
}
+4 -2
View File
@@ -15,7 +15,8 @@ class ZlibTests: XCTestCase {
func testZlib() {
let testName = "test"
guard let testData = try? Data(contentsOf: Constants.url(forTest: testName, withType: ZlibTests.testType)) else {
guard let testData = try? Data(contentsOf: Constants.url(forTest: testName,
withType: ZlibTests.testType)) else {
XCTFail("Failed to load test archive")
return
}
@@ -33,7 +34,8 @@ class ZlibTests: XCTestCase {
}
func testZlibFull() {
guard let testData = try? Data(contentsOf: Constants.url(forTest: "random_file", withType: ZlibTests.testType)) else {
guard let testData = try? Data(contentsOf: Constants.url(forTest: "random_file",
withType: ZlibTests.testType)) else {
XCTFail("Failed to load test archive")
return
}